Good morning,
I'm not a very C++ expert.
What I need is a class containing a struct. I tried to write it but I get linker errors.
Can I have a little example of a Class containing a struct?
Best Regards,
Franco
Good morning,
I'm not a very C++ expert.
What I need is a class containing a struct. I tried to write it but I get linker errors.
Can I have a little example of a Class containing a struct?
Best Regards,
Franco
Franco Amato
Qt Code:
class MyClass { public: MyClass(); ~MyClass(); struct MyStruct { MyStruct() : a(0), b(0) {} int a; int b; }; MyStruct *getMyStruct(); private: MyStruct *mystruct; }; MyClass::MyClass() { mystruct = new MyStruct; } MyClass::~MyClass() { delete mystruct; } MyStruct *MyClass::getMyStruct() { return mystruct; }To copy to clipboard, switch view to plain text mode
Not checked for syntax errors.
Zlatomir (20th September 2010)
@tbscope: should that code compile?
!!!i thought not!!!
EDITED:
I was wrong
It compiles... I didn't knew that it is possible to have a class declaration inside another class
As far as i know this is not possible, you should declare the struct separately, and use the pointer (or instance) in the class:
A better design is to code the struct in it's own .h, .cpp files and include MyStruct.h in the MyClass.cpp file (of course with the use of a pointer and forward declaration in MyClass.h)Qt Code:
struct MyStruct { //... }; class MyClass { public: //... private: MyStruct *mystruct; //or MyStruct myStructInstance; };To copy to clipboard, switch view to plain text mode
Last edited by Zlatomir; 20th September 2010 at 18:42.
And just for clarification - a "struct" in C++ is also a class so instances of it can be treated as any other C++/Qt object (i.e. like QString or QObejct).
The code needs one correction, you have to return properly prefixed type:
Qt Code:
MyClass::MyStruct *MyClass::getMyStruct() { return mystruct; }To copy to clipboard, switch view to plain text mode
Zlatomir (20th September 2010)
Oops, thanks for the correction.
I never had a use for a class in a class. I don't think it makes sense to do this.
How can I use the famous typedef?
As far I remember, the only difference between a struct and a class in C++ is that a struct defaults to public, where a class defaults to non-public (not sure if it's protected or private).
There's no need to typedef a struct in C++. You can use it's name directly, without the "struct" keyword.
tbscope: It makes sense when you want a class for encapsulation purposes, but don't want it visible from outside of the main class.
So I create an instance of the struct as it was a class? Should I use the new operator? ( sorry for the foo questions )
Franco Amato
Yes:
This is the only difference between class and structQt Code:
class MyClass { int member; //by "default" member is private }; struct MyStruct{ int member; //by "default" member is public };To copy to clipboard, switch view to plain text mode
Only if you want to allocate on the heap (just like you do when you are creating your own types by using the "class" keyword.
Thank you very much for the explanation.
A last question: how the compiler know if is a C++ or C struct?
Franco Amato
You don't have to use new (you use it only if you really need to allocate on the heap)
You can create an instance of a struct just like the one of a class:
Qt Code:
class MyClass { int member; //by "default" member is private }; struct MyStruct{ int member; //by "default" member is public }; MyClass classInstance; //both are compiling ok MyStruct structInstance; classInstance.member = 0; //error because class "member" is private structInstance.member = 10; //ok because struct "member" is publicTo copy to clipboard, switch view to plain text mode
If you use a C++ compiler then every struct is a class for it. In other words in C++ there is no difference between a class and a struct, you can even do this (although msvc issues a warning for it):
Qt Code:
// class, private by default: class SomeClass; // forward declare in header file // struct, public by default: struct SomeClass { //... }; // define in implementation fileTo copy to clipboard, switch view to plain text mode
From the file extension I guess.. try saving the file as .c and see if it compiles..A last question: how the compiler know if is a C++ or C struct?
Also there might be some option to the compiler to tell if its C or C++.. but am not sure of such setting.
Another difference of struct in C and C++ is that in C++ its same as class and you can even inherit it. While in C you cannot have inheritance as far as I know. ( One can easily verify )
Try to find some good books about c++ and object oriented programming.
Qt Code:
struct MyStruct { MyStruct() : a(0), b(0) {} int a; int b; }; class MyClass { public: MyClass(); ~MyClass(); MyStruct *getMyStruct(); private: MyStruct *mystruct; }; MyClass::MyClass() { mystruct = new MyStruct; } MyClass::~MyClass() { delete mystruct; } MyStruct *MyClass::getMyStruct() { return mystruct; }To copy to clipboard, switch view to plain text mode
Qt Code:
// h class MyClass { public: struct MyStruct { int a, b; MyStruct() : a(0), b(0){} }; MyStruct* getPtr() const; MyStruct getObj() const; MyClass(); ~MyClass(); private: MyStruct *structPtr; MyStruct structObj; }; // cpp MyClass::MyClass() { structPtr = new MyStruct; } MyClass::~MyClass() { delete structPtr; } MyClass::MyStruct* MyClass::getPtr() const { return structPtr; } MyClass::MyStruct MyClass::getObj() const { return structObj; }To copy to clipboard, switch view to plain text mode
Bookmarks