Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25

Thread: Struct into a C++ class

  1. #21
    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

    We have shown you two different approaches to the same problem. In tbscope's code MyStruct is just a public structure, in my code it is defined in the scope of the MyClass class, so its full name is MyClass::MyStruct. My code is better encapsulated, his is more reusable.
    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.


  2. #22
    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
    We have shown you two different approaches to the same problem. In tbscope's code MyStruct is just a public structure, in my code it is defined in the scope of the MyClass class, so its full name is MyClass::MyStruct. My code is better encapsulated, his is more reusable.
    Ok thank you.
    Franco Amato

  3. #23
    Join Date
    Feb 2009
    Posts
    20

    Default Re: Struct into a C++ class

    there are of course use cases for a struct in a class.
    When the struct is in the private section you can bundle some internal data.
    And the struct can't be created from outside.

    e.g. when I have to store more then two variables in one vector I use a struct for it, thats better then a boost::tuple, because when you acces the data you can better read your code.

  4. #24
    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

    You could put a class in the private section of a class and bundle your internal data there too, and it couldn't be created from outside.

    The only real difference betwen struct and class is that a struct is by default public, whereas a class is by default private.

  5. #25
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Struct into a C++ class

    Along with PIMPL, there are other patterns that make use of this construct. Consider the following:
    Qt Code:
    1. class X
    2. {
    3. /*singleton factory*/
    4. friend class XFactory;
    5. protected:
    6. X();
    7. X(X const&);
    8. X& operator=(X const&);
    9. }
    To copy to clipboard, switch view to plain text mode 

    It is now impossible to gain an instance of X without using XFactory. This also provides an easy answer to "who should parent an instance of X?" which is "XFactory until someone else claims it". This is fairly trivial to implement and very useful.

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.