Is it possible to use structs in classes and if so, how? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to use structs in classes and if so, how?

In case anyone was wondering, structs are basically like classes but they can only hold data types and variables. I tried putting a struct into a class but I could not make use of it. So is this possible, or is it all just a pipe dream.

29th Jul 2019, 8:55 PM
Mike The great Sage
Mike The great Sage - avatar
10 Answers
+ 2
If you're talking about C++, struct behaves exactly like class, you can use inheritance, declare functions, etc. The only difference is that struct default access modifier is public, while class is private. Though, I'm not sure I understand your question, whether you're talking about defining a struct inside a class, or having a struct as one of the class member. Either way, both are possible
29th Jul 2019, 9:21 PM
Agent_I
Agent_I - avatar
+ 2
Mike The great Sage Like I said, it's possible and it's the same like having class as member in another class Example: struct SomeStruct { ... }; class SomeClass { ... private: SomeStruct str; };
29th Jul 2019, 9:31 PM
Agent_I
Agent_I - avatar
+ 2
Will try this thank you
29th Jul 2019, 9:56 PM
Mike The great Sage
Mike The great Sage - avatar
+ 2
Thank you swim , that really helped a lot.
29th Jul 2019, 10:13 PM
Mike The great Sage
Mike The great Sage - avatar
+ 1
Thank you for clarifying the abilities of structs, my main question is how would someone go about making and using structs as a member of a class. Would it be the same as making a class a member or would it be different from a class.
29th Jul 2019, 9:26 PM
Mike The great Sage
Mike The great Sage - avatar
+ 1
Ok, understood. Thank you for really clearing things up. Much appreciated.
29th Jul 2019, 9:33 PM
Mike The great Sage
Mike The great Sage - avatar
+ 1
Struct could be a class member but not inherited. Right?
29th Jul 2019, 11:23 PM
Geral
+ 1
Found this, but most of the content has already been expressed already in this thread. https://stackoverflow.com/questions/3574040/c-can-a-struct-inherit-from-a-class
30th Jul 2019, 1:09 AM
Sonic
Sonic - avatar
+ 1
Thanks for your input sonic
30th Jul 2019, 1:38 AM
Mike The great Sage
Mike The great Sage - avatar
+ 1
Sonic Thanks!
30th Jul 2019, 2:40 AM
Geral