Reputation: 41
I am wondering if a struct
has the same idea as enum
in c++? If someone can explain similarities/differences I will be grateful I am trying to learn.
Upvotes: 0
Views: 158
Reputation: 31204
An enum is for abstracting away magic numbers.
A struct is for holding a collection of different variables.
You can almost think of an enum as a stand-in for an int or char to make things more readable.
Upvotes: 1
Reputation: 1655
struct is basically a class with all members public.
For instance:
struct MyNewStruct {
int myNewInt;
double myNewDouble;
};
is equivalent to:
class MyNewClass {
public:
int myNewInt;
double myNewDouble;
};
Hence, you can create a struct with constructor:
struct MyNewStruct {
int myNewInt;
double myNewDouble;
MyNewStruct(int i, double d)
: myNewInt(i), myNewDouble(d)
{}
};
Upvotes: 1
Reputation: 564451
Are struct in c++ similar to enum or classes?
In C++, a struct is essentially the same as a class, except that the default access modifiers for member variables, methods, and for base classes are all public, where in a class, the default access modifier is private.
Upvotes: 8