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
34,507
21,269
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.
 

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

1. What is the difference between structs and classes in C++?

In C++, structs and classes are very similar, with the main difference being that members of a struct are public by default, while members of a class are private by default. This means that structs are typically used for simple data structures, while classes are used for more complex data types with encapsulation and inheritance.

2. Which is more powerful, structs or classes in C++?

Neither structs nor classes are inherently more powerful in C++. The choice between using a struct or a class depends on the specific requirements of your program. If you need encapsulation and inheritance, you should use a class. If you are working with simple data structures, a struct may be more appropriate.

3. Can structs have member functions in C++?

Yes, structs can have member functions in C++. While structs are traditionally used for simple data structures, they can also have member functions just like classes. The main difference is the default access level of members in a struct, which is public.

4. When should I use a struct instead of a class in C++?

You should use a struct in C++ when you are working with simple data structures that do not require encapsulation or inheritance. Structs are a good choice for representing data in a straightforward manner, without the need for complex functionality or privacy.

5. Can I use structs and classes interchangeably in C++?

Yes, in C++ you can use structs and classes interchangeably in many cases. Both structs and classes can have member variables, member functions, constructors, and destructors. The main difference is the default access level of members, so it is important to choose the appropriate keyword based on your specific needs.

Similar threads

Replies
23
Views
2K
Replies
19
Views
3K
Replies
89
Views
4K
Replies
17
Views
1K
Replies
15
Views
2K
Replies
2
Views
3K
Replies
3
Views
591
Back
Top