Results 1 to 5 of 5

Thread: C++ friend classes, with structs and enums

  1. #1
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default C++ friend classes, with structs and enums

    Hey all,

    I ran into a bit of an issue using Qt with nested C++ classes. Namely, moc doesn't support nested classes, and I need to restructure some of my code to reflect this.

    Since friend classes and nested classes are pretty similar I wanted to convert my nested classes into friend classes. The problem is that my "outer" nested classes often have enums or structs that are accessible to the "inner" nested classes. I don't quite get how to access these typedefs using friend classes without making them global.

    ex:
    Qt Code:
    1. class Temp
    2. {
    3. friend class FriendOfTemp;
    4. public:
    5. struct infoRow
    6. {
    7. int blah;
    8. int blah2;
    9. };
    10.  
    11. enum osTypes
    12. { win, osx, linux, unix, beos, qnx};
    13. };
    14.  
    15. class FriendOfTemp
    16. { ... };
    17.  
    18. FriendOfTemp::FriendOfTemp
    19. {
    20. os_type = linux // wouldn't make sense
    21. infoRow newRow; // wouldn't work
    22. }
    To copy to clipboard, switch view to plain text mode 

    How would I go about doing this?

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

    Default Re: C++ friend classes, with structs and enums

    How about:

    Qt Code:
    1. FriendOfTemp::FriendOfTemp()
    2. {
    3. Temp::osTypes os_Type = Temp::linux;
    4. Temp::infoRow newRow;
    5. }
    To copy to clipboard, switch view to plain text mode 

    I've never used a friend class, but this is how I access enums in other classes (so you could drop the "friend" part of your class, and this would still work)
    Last edited by squidge; 2nd December 2009 at 21:31.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: C++ friend classes, with structs and enums

    and in this particular case a friend declaration is nonsense, since all members of the class Temp are public...

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: C++ friend classes, with structs and enums

    And one more question, are you sure you need struct infoRow to be located inside Temp class?
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: C++ friend classes, with structs and enums

    Thanks for the replies.

    * InfoRow doesn't have to be inside the Temp class, but it logically makes sense for it to be there. I could just make it a global, but Temp and its friend class are the only classes that will use InfoRow.

    * I realize Temp doesn't have any private members. I just wrote out some code in the reply message box to demonstrate what I meant. In actuality, my class has a bunch of private members that needs to be accessed by "FriendOfTemp".

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.