C/C++ Structs vs. Classes: Which is More Powerful in C++?

  • Thread starter Thread starter Vanadium 50
  • Start date Start date
  • Tags Tags
    Classes
AI Thread Summary
The discussion centers on the nuances of C and C++ programming, particularly regarding copy constructors and the use of structs versus classes. It highlights that copy constructors are specific to C++, not C, and emphasizes that both languages utilize call-by-value, even when simulating call-by-reference through pointers. The conversation also touches on the inheritance capabilities of structs in C++, clarifying that structs can inherit from one another, with a provided code example demonstrating this feature. Structs are noted for their simplicity and are often used for collections of plain old data (POD), while classes are preferred for more complex data structures that may require internal modifications. The introduction of designated initializers in C++20 has enhanced the usability of structs, making them more appealing for certain applications. Overall, the thread underscores the importance of understanding language features to write effective and efficient code.
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
Gold Member
Messages
35,003
Reaction score
21,702
Moderator's note: thread split off from https://www.physicsforums.com/threa...cept-its-parameter-by-reference-in-c.1049624/

Mark44 said:
Minor point, but copy constructors aren't relevant to C. They are relevant in C++, so I've edited your thread title.
That is true, but I am continually surprised and impressed about how much C++ is in C if you just know where to look, "struct" has almost as much power as "class".

So long as we are niggling, C/C++ is always call by value. When you call "by reference" you are actually calling with the value of that reference. But f(x) will not change x, and f(&x) will not change &x - but can change x.
 
Last edited by a moderator:
Technology news on Phys.org
Vanadium 50 said:
That is true, but I am continually surprised and impressed about how much C++ is in C if you just know where to look, "struct" has almost as much power as "class".
Right, with the only difference being that struct members are public by default, while class members are private by default.
Vanadium 50 said:
So log as we are niggling, C/C++ is always call by value. When you call "by reference" you are actually calling with the value of that reference. But f(x) will not change x, and f(&x) will but change &x - but can change x.
Yes, I'm being fast and loose by talking about "reference" parameters. C, C++, etc. simulate reference parameters via pointers. In f(&x), the address of x doesn't change, but what's at that address can change.
 
Mark44 said:
the only difference being that struct members are public by default, while class members are private by default.
Is that true? Can structs inherit?
 
Vanadium 50 said:
Is that true? Can structs inherit?
Yes.
Here's some example code. The two structs represent two-dimensional and three-dimensional points, respectively. The ThreeDPt struct inherits the TwoDPt struct. I could have added some struct methods, but didn't do so.
C++:
struct TwoDPt
{
private:
    int x, y;    

public:
    TwoDPt()
    {
        x = y = 0;
    }

    TwoDPt(int xi, int yi)
    {
        x = xi;
        y = yi;
    }    
};

struct ThreeDPt: TwoDPt
{
private:
    int z;
public:
    ThreeDPt() :TwoDPt()
    {
        z = 0;
    }

    ThreeDPt(int xi, int yi, int zi) :TwoDPt(xi, yi)
    {
        z = zi;
    }
};
int main()
{
    TwoDPt P;
    ThreeDPt Q;

    TwoDPt P1(1, 2);
    ThreeDPt Q1(2, 4, 6);    
}
After execution, P and Q hold (0, 0) and (0, 0, 0), respectively.
P1 and Q1 hold (1, 2) and (2, 4, 6), respectively.
 
  • Informative
  • Like
Likes berkeman and Vanadium 50
I continue to be impressed by the power of struct.
 
There was a thread about structs vs. classes, maybe a year ago. Apparently structs are used most often for collections of POD (plain old data), even though they could also include their own struct methods.
 
That's pretty much how I use structs and classes. Structs are just for data that "goes together". but classes are smarter. I also use classes if I might want to change the internal storage: e.g. a Vector might be store internally in a rectangular basis and I might want to change it to cylindrical.
 
Structs are especially nice as POD when kept as simple aggregates which gives a nice set of standard functionality with minimum code. For instance, after we got designated initializers in C++ 20, which fixed the main code smell inherent with anonymous positional initialization, I find myself using them more and more.
 
Arguing about the thread split may be a meta-meta-meta issue but I would argue that what features of the language should be used is at least as important as being able to cobble something up that doesn't throw a syntax error.
 

Similar threads

Back
Top