C++ typedef struct vs class

14,080

Solution 1

Niether are more efficient. On a technical level, there is no difference between a class and a struct aside from default visibility of members (public in struct, private in class) and default inheritance model (public in struct, private in class).

They typedef struct {} name model is not idiomatic in C++. In fact, it's an abomination -- a holdover from C. Don't use this model. Use this struct name {}; instead. Using the typedef struct {} name; model doesn't gain you anything in C++ (it was needed in C), and may cost you sojmething in terms of maintainability. For instance, it might be harder to grep for typedef struct declarations. But since it doesn't gain you anything by doing this, there's no compelling reason not to simply do struct name {}; in C++.

Aside from technical issues, the differences between struct and class are semantic ones. It is traditional and expected that objects declared as structs are simple objects which consist of only public: data members (so-called PODs). If it has private or protected data, is expected to be derived from, or has any methods, it is declared as a class.

This guideline is open to interpretation, and is just that -- a guideline. There is nothing to prevent you from declaring an abstract base class as a struct, for example. However you may want to consider following this guideline in order to follow the Principle of Least Surprise, making your code easier to understand and maintain.

Solution 2

Both are nearly equivalent. More precisely, struct { is the same as class {public:

An optimizing compiler would probably generate exactly the same code. Use MELT or simply pass -fdump-tree-all (beware, that option produces hundreds of dump files) to g++ (assuming you use a recent GCC compiler) -preferably with some optimization like -O - to find out (or look at the produced assembler code with g++ -O -fverbose-asm -S ...)

Solution 3

typedef struct is actually the C way to do this. In C++ the two versions would look very similar: Your class as written, and the struct as follows:

struct sPoint
{
 int X;
 int Y;
};

The two forms are functionally identical but you can provide your future maintainers with significant information by picking and sticking to some convention about how they're used. For example one approach is that if you intend to make the data elements private and give it useful methods (for example if you use inline accessors you can insert print calls every time the methods are used) then by all means make it a class. If you intend to have the data be public and access them as members then make it a struct.

Solution 4

There's no performance difference between a class and a struct

A class defaults to private access, whilst a struct defaults to public access. If interoperability with C is an issue for you then you'll have to use struct, and obviously it can't have any member functions.

As an aside, there's no std::is_struct in the standard library. Instead the std::is_class method returns true if the type is a class or a structure.

Solution 5

Simply put, the first way is more C++, and the second way is more C. Both work, while the first way is more 'standard' now.

Share:
14,080
Sagi2313
Author by

Sagi2313

I come from low-level programming and hardware design. Most of the time I write plain C and in the past some assembly. Also have some experience with VisualBasic6. OOP is "new" to me and fascinating, but it is a bit hard to teach an old dog new tricks... so I need help from you all in basic things, since I can't find a rigid "bridge" in my head between the C way and the OOP way. I have worked in commercial projects, and have few products on the streets, but I also do a lot for private R&D, learn-enjoy-create. Alex

Updated on June 04, 2022

Comments

  • Sagi2313
    Sagi2313 almost 2 years

    I am not very familiar with C++ , and while I am trying some test programms I came to a question regarding the best if I may say so way to define some primitive elements in C++ code.

    Let's take a class that describes rectangles. It would create them, draw them , rotate, resize, etc... now in most cases we have to deal with points on the canvas. The rectangle its self is described by 2 points: Upper Left and Lower Right corner. Also in order to Rotate it, you need an angle, and a point(anchor point). Or maybe to move it you need a new anchor point for the given rectangle. I guess I made my point in using points . So what is more efficient ? to define this primitive point as a class or as a struct?

    class cPoint
    {
    public: 
     int X;
     int Y;
    };
    

    or

    typedef struct
    {
     int X;
     int Y;
    }sPoint;