Are struct in c++ similar to enum or classes? - c++

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.

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.

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)
{}
};

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.

Related

C++: writing a function for a struct outside that struct? [duplicate]

This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
For classes, you could just say:
class Test{
int a;
Test(int a);
}
Test::Test(int a) {
this->a=a;
}
Function names get "classname::" in front of them when declared outside of class.
How would I do this for structs?
struct Test {
int a;
Test(int a);
}
How would I write the function for this struct Test outside of struct declaration so that it can be only be called by a Test struct?
Same way. Difference between struct and class in C++ is only default visibility of members (private for class, public for struct).
Actually, it's not just function, it's constructor of class/struct Test.
In C++, structs are essentially the same as classes except for their default protection levels: classes default to private, structs to public. To define that function outside of the struct so that it can only be called from a member, declare it as private, then define it as normal:
struct Test {
private:
int a;
Test(int a);
};
Test::Test(int a) {
this->a=a;
}
Additionally, instead of modifying the a member in the constructor body like that, you should use an initializer list. This sets the value of the member before the instance is fully constructed. It's not so important with just an int, but it's a good practice to get in to.
struct Test {
private:
Test(int a) : a(a) {}
int a;
};
How would I write the function for this struct Test outside of struct declaration
Do exactly what you did for the first one. Both are class types, whether you use the class or struct keyword to introduce them.
The only difference is the default accessibility of members and base classes: private if you use class, and public if you use struct.
so that it can be only be called by a Test struct?
If you mean that you want it to be private (as it is in the first example), then you'll have to do so explicitly, since accessibility defaults to public:
struct Test {
int a;
private:
Test(int a);
};
Personally, I'd use the more conventional class if there's anything non-public.
ForEveR is right. Just like in the question you can have a structure member defined like:
struct Test{
int a;
Test(int a);
};
Test::Test(int a) {
this->a=a;
}
point to note, struct members are public by default. class memebers are private by default.

anonymous struct in class with linkage external

If you have a anonymous struct in a class with external linkage(under public access). Will that struct be a different entity in each file?
same goes for const data members?
class k{
public:
struct {int u;} o;
}a;
I am not sure if I get your question (also I strongly discourage you to use something like that in your class)
Anyway each instance of class k will get their own copy of your struct o.
Also, your struct is not anonymous, but it's unnamed #DyP. You usually want to use anonymous struct/union when they are nested (and more specifically you are supposed to use anonymous union).
Example1:
struct T {
int tag;
union { float x; int n; };
};

When is using a struct more reasonable than using a class in C++? [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
Closed 9 years ago.
I have been reading Qt Creator's source code.
When is using a struct more appropriate than using a class in C++?
It is purely a matter of personal preference or coding conventions. struct and class are essentially the same in C++, with the difference being the default access specifiers and base classes being public for a struct and private for a class.
For example, here two definitions of Bar are equivalent:
class Foo {};
struct Bar : Foo {};
class Bar : public Foo {};
Here too:
class Foo {};
struct Bar : private Foo {};
class Bar : Foo {};
and here too:
class Bar
{
int n; // private
public:
double x; // public
};
struct Bar
{
private:
int n;
public:
double x;
};
Furthermore, you could forward declare Bar as class Bar or struct Bar interchangeably.
There's no difference aside from default access (struct => public, class => private). For readability, I prefer using struct when I'm defining a plain-old C struct (just data), or when the majority of my members/methods are public.
I think of struct as a record. They bundle together variables and you want to access and modify struct members without indirection, such as a function call. For example, when you want to write together groups of data items to dump them into a file or send them over network then using class may even make the code harder to read and understand.
It might be more suitable when it's Plain Old Data without any functionality on it. In C++, the only difference between class and struct is that struct members are public by default, as opposed to private for class.

Difference between struct and class when Template meta-programming

template<int n>
struct Numberim{
enum{ value = Numberim<n-1>::value + n };
};
template<>
struct Numberim<0>{
enum{ value = 0 };
};
this is a simple tmp example,and it's ok;
template<int n>
class Numberim{
enum{ value = Numberim<n-1>::value + n };
};
template<>
class Numberim<0>{
enum{ value = 0 };
};
I use g++ to compile,and it complains...however, as far as I know, struct and class is treated nearly in the same way.just like this"In C++, the only difference between a struct and a class is that struct members are public by default, and class members are private by default."
So, what's the difference between them here in earth?
The difference will be same as it would be for typically class vs struct. Your "value" will be public for your first example (using struct) and private for your second example (using class).
For a reference on the difference between a class and a struct, please see What are the differences between struct and class in C++.
Concrete class Numberim<1> is not related to concrete class Numberim<0>.
Thus, having one class refer to the other’s definition of value works when value is public, which it is for the struct, but not when value is private, which it is for the class.
You can use the friend mechanism, or you can make value public, or you can, much more simply, do this:
template<int n>
class Numberim{
enum{ value = n*(n+1)/2 };
// And whatever else you want in here.
};
The difference is still the same, when it tries to compile Numberim<n-1>::value with n=1 it tries to use the template specialization. However, since value is private member of Numberim<0> (as the class member variables are private by default) it gives the compiler error.

Inheriting from a template class, using a type defined in the derived class

I'm trying to inherit from a template class, using a type defined in the derived class.
I have tried this, but it doesn't work.
class A : std::vector<A::B>
{
enum B { foo, bar };
};
Is there an elegant way of doing this ?
Edit : I know that it works if B is defined earlier. But i'm looking for a solution that allows encapsulating the type B inside the A class.
You can't forward declare an enum in C++03. Just use normal composition by having a the vector as a member and forwarding by hand.
You will have to define enum B before it's use to inherit.
Also, you're not inheriting from the Standard vector, are you?
In my view, the best (admittedly indirect) solution is to use composition rather than inheritance:
class A
{
enum B { foo, bar };
std::vector<B> bs;
};
If for some reason you need (or really want) to use private inheritance to embed the vector in your object, then the type will need to be defined before the class, at namespace scope, since types cannot be used before they are declared. If they are not indended to be accessed by users of the class, and you don't want to pollute the namespace containing your class, then you could put them inside a namespace to indicate that they are implementation details:
namespace details
{
enum B { foo, bar };
}
class A : std::vector<details::B>
{
typedef details::B B; // if you don't want to write "details::B" everywhere
static const B foo = details::foo; // if you don't want to write "details::foo" everywhere
// and so on.
};
Thanks everyone for your answers, but I just found a (edit: bad) solution :
namespace
{
enum B_type { foo, bar };
}
class A : std::vector<B_type>
{
typedef value_type B;
};