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

  • Context: C/C++ 
  • Thread starter Thread starter Vanadium 50
  • Start date Start date
  • Tags Tags
    Classes
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 2K views
Messages
35,014
Reaction score
21,722
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:
Physics 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.
 
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.
 
Reply
  • Informative
  • Like
Likes   Reactions: berkeman and Vanadium 50
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.