Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Struct into a C++ class

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Struct into a C++ class

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Struct into a C++ class

    Qt Code:
    1. class MyClass
    2. {
    3. public:
    4. MyClass();
    5. ~MyClass();
    6.  
    7. struct MyStruct
    8. {
    9. MyStruct() : a(0), b(0) {}
    10. int a;
    11. int b;
    12. };
    13.  
    14. MyStruct *getMyStruct();
    15.  
    16. private:
    17. MyStruct *mystruct;
    18. };
    19.  
    20. MyClass::MyClass()
    21. {
    22. mystruct = new MyStruct;
    23. }
    24.  
    25. MyClass::~MyClass()
    26. {
    27. delete mystruct;
    28. }
    29.  
    30. MyStruct *MyClass::getMyStruct()
    31. {
    32. return mystruct;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Not checked for syntax errors.

  3. The following user says thank you to tbscope for this useful post:

    Zlatomir (20th September 2010)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Struct into a C++ class

    @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:
    Qt Code:
    1. struct MyStruct
    2. {
    3. //...
    4. };
    5.  
    6. class MyClass
    7. {
    8. public:
    9. //...
    10. private:
    11. MyStruct *mystruct; //or MyStruct myStructInstance;
    12. };
    To copy to clipboard, switch view to plain text mode 
    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)
    Last edited by Zlatomir; 20th September 2010 at 19:42.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struct into a C++ class

    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:
    1. MyClass::MyStruct *MyClass::getMyStruct()
    2. {
    3. return mystruct;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    Zlatomir (20th September 2010)

  7. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Struct into a C++ class

    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.

  8. #6
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    How can I use the famous typedef?

  9. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Struct into a C++ class

    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.

  10. #8
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ 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

  11. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Struct into a C++ class

    Quote Originally Posted by franco.amato View Post
    So I create an instance of the struct as it was a class?
    Yes:
    Qt Code:
    1. class MyClass {
    2. int member; //by "default" member is private
    3. };
    4. struct MyStruct{
    5. int member; //by "default" member is public
    6. };
    To copy to clipboard, switch view to plain text mode 
    This is the only difference between class and struct
    Quote Originally Posted by franco.amato View Post
    Should I use the new operator? ( sorry for the foo questions )
    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.

  12. #10
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Thank you very much for the explanation.
    A last question: how the compiler know if is a C++ or C struct?
    Franco Amato

  13. #11
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Quote Originally Posted by Zlatomir View Post
    Yes:
    Qt Code:
    1. class MyClass {
    2. int member; //by "default" member is private
    3. };
    4. struct MyStruct{
    5. int member; //by "default" member is public
    6. };
    To copy to clipboard, switch view to plain text mode 
    This is the only difference between class and struct

    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.
    In my old 'C language' course I remember that when I worked with c struct I had to create it with a malloc, so in C++ I imagine I have to do the same with the new, but seems I'm wrong
    Franco Amato

  14. #12
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Struct into a C++ class

    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:
    1. class MyClass {
    2. int member; //by "default" member is private
    3. };
    4. struct MyStruct{
    5. int member; //by "default" member is public
    6. };
    7. MyClass classInstance; //both are compiling ok
    8. MyStruct structInstance;
    9.  
    10. classInstance.member = 0; //error because class "member" is private
    11. structInstance.member = 10; //ok because struct "member" is public
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struct into a C++ class

    Quote Originally Posted by franco.amato View Post
    A last question: how the compiler know if is a C++ or C struct?
    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:
    1. // class, private by default:
    2. class SomeClass; // forward declare in header file
    3.  
    4. // struct, public by default:
    5. struct SomeClass {
    6. //...
    7. }; // define in implementation file
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    A last question: how the compiler know if is a C++ or C struct?
    From the file extension I guess.. try saving the file as .c and see if it compiles..
    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 )

  17. #15
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Quote Originally Posted by wysota View Post
    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:
    1. // class, private by default:
    2. class SomeClass; // forward declare in header file
    3.  
    4. // struct, public by default:
    5. struct SomeClass {
    6. //...
    7. }; // define in implementation file
    To copy to clipboard, switch view to plain text mode 
    So my question comes naturally:
    Why does exist in C++ the struct if it has no difference with a class?
    Franco Amato

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struct into a C++ class

    Quote Originally Posted by franco.amato View Post
    Why does exist in C++ the struct if it has no difference with a class?
    So that you can take C code and compile it with a C++ compiler retaining the same functionality as if you compiled it with a C compiler.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Quote Originally Posted by wysota View Post
    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:
    1. MyClass::MyStruct *MyClass::getMyStruct()
    2. {
    3. return mystruct;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Wysota but if I declare the struct in a .h file outside the class, how I have to change the code of the getMyStruct?

    Regards,
    Franco
    Franco Amato

  20. #18
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Struct into a C++ class

    Quote Originally Posted by franco.amato View Post
    Wysota but if I declare the struct in a .h file outside the class, how I have to change the code of the getMyStruct?
    Try to find some good books about c++ and object oriented programming.

    Qt Code:
    1. struct MyStruct
    2. {
    3. MyStruct() : a(0), b(0) {}
    4. int a;
    5. int b;
    6. };
    7.  
    8. class MyClass
    9. {
    10. public:
    11. MyClass();
    12. ~MyClass();
    13.  
    14. MyStruct *getMyStruct();
    15.  
    16. private:
    17. MyStruct *mystruct;
    18. };
    19.  
    20. MyClass::MyClass()
    21. {
    22. mystruct = new MyStruct;
    23. }
    24.  
    25. MyClass::~MyClass()
    26. {
    27. delete mystruct;
    28. }
    29.  
    30. MyStruct *MyClass::getMyStruct()
    31. {
    32. return mystruct;
    33. }
    To copy to clipboard, switch view to plain text mode 

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Struct into a C++ class

    Qt Code:
    1. // h
    2. class MyClass {
    3. public:
    4. struct MyStruct {
    5. int a, b;
    6. MyStruct() : a(0), b(0){}
    7. };
    8.  
    9. MyStruct* getPtr() const;
    10. MyStruct getObj() const;
    11. MyClass();
    12. ~MyClass();
    13. private:
    14. MyStruct *structPtr;
    15. MyStruct structObj;
    16. };
    17.  
    18. // cpp
    19.  
    20. MyClass::MyClass() { structPtr = new MyStruct; }
    21. MyClass::~MyClass() { delete structPtr; }
    22.  
    23. MyClass::MyStruct* MyClass::getPtr() const { return structPtr; }
    24. MyClass::MyStruct MyClass::getObj() const { return structObj; }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  22. #20
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    691
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Quote Originally Posted by wysota View Post
    Qt Code:
    1. // h
    2. class MyClass {
    3. public:
    4. struct MyStruct {
    5. int a, b;
    6. MyStruct() : a(0), b(0){}
    7. };
    8.  
    9. MyStruct* getPtr() const;
    10. MyStruct getObj() const;
    11. MyClass();
    12. ~MyClass();
    13. private:
    14. MyStruct *structPtr;
    15. MyStruct structObj;
    16. };
    17.  
    18. // cpp
    19.  
    20. MyClass::MyClass() { structPtr = new MyStruct; }
    21. MyClass::~MyClass() { delete structPtr; }
    22.  
    23. MyClass::MyStruct* MyClass::getPtr() const { return structPtr; }
    24. MyClass::MyStruct MyClass::getObj() const { return structObj; }
    To copy to clipboard, switch view to plain text mode 
    Well I still don't understand as the code of wysota is different from the code of tbscope.
    Meanwhile thankx to all for the precious help.
    Best Regards,
    Franco
    Last edited by franco.amato; 21st September 2010 at 22:41.
    Franco Amato

Similar Threads

  1. QList<struct>
    By Axsis in forum Newbie
    Replies: 11
    Last Post: 12th October 2015, 08:48
  2. struct QbufferPrivate error!!
    By chochatown in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 16:37
  3. QDataStream class/struct & stream operators
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 20:40
  4. Struct in network
    By sribalaji in forum Qt Programming
    Replies: 7
    Last Post: 26th March 2008, 11:38
  5. struct problem...
    By hiuao in forum General Programming
    Replies: 3
    Last Post: 5th April 2007, 08:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.