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

  • C/C++
  • Thread starter Vanadium 50
  • Start date
  • Tags
    Classes
In summary, the conversation focused on the use of copy constructors in C and C++, and the differences between structs and classes in terms of inheritance and access modifiers. The experts also discussed the practical uses of structs and classes, with structs being commonly used for collections of plain data and classes being used for more complex and potentially changeable data. The conversation also briefly touched on the use of designated initializers in C++.
  • #1
Vanadium 50
Staff Emeritus
Science Advisor
Education Advisor
2023 Award
33,310
19,828
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
  • #2
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.
 
  • #3
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?
 
  • #4
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
  • #5
I continue to be impressed by the power of struct.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

1. What is the main difference between a struct and a class?

The main difference between a struct and a class is that a struct is a value type, while a class is a reference type. This means that when a variable of a struct type is created, its value is stored directly, whereas a variable of a class type is a reference to an object in memory.

2. Can methods be defined in a struct?

Yes, methods can be defined in a struct just like in a class. However, in a struct, all methods are implicitly public and cannot be explicitly defined as private or protected.

3. How are structs and classes used differently in programming?

Structs are often used for lightweight data structures that contain a few simple variables, while classes are used for more complex data structures that may contain methods, properties, and other members.

4. Can a struct inherit from another struct?

No, a struct cannot inherit from another struct. Structs do not support inheritance in C#.

5. When should I use a struct instead of a class?

In general, structs should be used when you need a small, simple data structure that will be frequently copied, and when you want to avoid the performance overhead of using a class. It is also recommended to use structs for immutable data types.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
23
Views
3K
  • Programming and Computer Science
Replies
4
Views
342
  • Programming and Computer Science
Replies
1
Views
945
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
321
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top