jaho
jaho

Reputation: 5002

In general, how to chose between a struct and a class in C++

So a pretty simple question, but I can't seem to find a general rule of choosing one over the other in some cases.

Let's say I have a simple Point class, like this:

class Point
{
public:
    Point();
    Point(double, double, double);

    Point(const Point& other);
    Point& operator=(const Point& other);

    bool operator==(Point& lhs, Point& rhs);

    void translate(double, double, double);

    double getX() const;
    double getY() const;    
    double getZ() const;

    void setX(const double);
    void setY(const double);
    void setZ(const double);

private:
    double x_, y_, z_;
}

All well, but why not make it a struct with public x, y, z and save half of the code?

Another example lets say I have a Header struct like this:

struct Header
{
    short int id;
    short int version;        
    size_t indexOffset;
    size_t indexSize;
}

Under what circumstances I'd want to make it a class? Also is there any difference between the above and something I've also seen in a quality code like this:

class Header
{
public:
    short int id;
    short int version;        
    size_t indexOffset;
    size_t indexSize;
}

So I guess a sub-question to this is how do I decide when to make member variables private. I know OO purists will probably say always, but I'm not sure about the benefit.

Many thanks.

Upvotes: 2

Views: 278

Answers (2)

lenik
lenik

Reputation: 23556

regarding class vs. struct :

use struct if you don't need member functions and class otherwise.

regarding getters and setters :

if you need direct access to the members -- make them public. the only reason to make them private and access through getters/setters is if you need to perform some kind of processing when accessing them, like checking for validity or recalculate other dependent members.

Upvotes: 2

jaho
jaho

Reputation: 5002

Thanks to the link provided by Bo Persson I've found the following to be a relevant answer:

The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.

and

Which style you use depends on circumstances and taste. I usually prefer to use struct for classes that have all data public. I think of such classes as "not quite proper types, just data structures.

Source:
http://www.parashift.com/c++-faq/struct-vs-class.html
http://www.amazon.com/dp/0201700735/?tag=stackoverfl08-20

Upvotes: 0

Related Questions