Results 1 to 12 of 12

Thread: friend class in TC++ book

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default friend class in TC++ book

    hello,

    I read TC++ vol2. (html ed 2004) at page 456 (chap design patterns) "the inner class idiom".
    In the class "Outer" it writes:
    Qt Code:
    1. class Outer {
    2. .....
    3. class Inner1;
    4. friend class Outer::Inner1;
    5. ...........
    6. };
    To copy to clipboard, switch view to plain text mode 
    I tried to get out those two lines and it compiles; why? What sense have they, please?

    thanks,
    Regards

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

    Default Re: friend class in TC++ book

    It means oobjects of the Outer::Inner1 class can access private members of Outer class instances.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: friend class in TC++ book

    Quote Originally Posted by wysota View Post
    It means oobjects of the Outer::Inner1 class can access private members of Outer class instances.
    Sorry, I don't understand how expolit this two lines; see this ,please:
    Qt Code:
    1. class Shape {
    2. class Type;
    3. friend class Shape::Type;
    4.  
    5. int _number;
    6. double _x, _y;
    7. class Type {
    8. Shape* _parent;
    9. public:
    10. Type(Shape* sh) : _parent(sh) {
    11. double d = _parent->compute();
    12. double d2 = _x; //is this an access ? It works only with "parent->_x"
    13. }
    14. };
    15. Type _type;
    16. public:
    17. Shape(double x = 0, double y = 0) : _type(this), _x(x), _y(y) { }
    18. double compute() { return _x * _y; }
    19. };
    To copy to clipboard, switch view to plain text mode 
    Where can I access to provate member of Shape ? (and how?)
    Regards

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

    Default Re: friend class in TC++ book

    Instances of Shape::Type can access _number, _x, _y and _type from instances of Shape.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: friend class in TC++ book

    hello,
    I don't find a way to access those private members; from where? from main?
    Qt Code:
    1. //main.cpp
    2. Shape s;
    3. Shape::Type t(&s);
    To copy to clipboard, switch view to plain text mode 
    Or where, please?
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: friend class in TC++ book

    Quote Originally Posted by mickey View Post
    I don't find a way to access those private members; from where? from main?
    Since Shape::Type is a friend of Shape (it works only one way), then any object of Shape::Type can access member variables of any Shape object.
    Qt Code:
    1. Shape::Type::Type(Shape* sh) : _parent(sh) {
    2. ...
    3. double d2 = _parent->_x; // "this" is an object of Shape::Type class, so it can access
    4. // _x in _parent which points to an instance of Shape.
    5. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: friend class in TC++ book

    hello,
    i don't understand:
    Qt Code:
    1. class Shape {
    2. //class Type;
    3. //friend class Shape::Type;
    4. int _number;
    5. double _x, _y;
    6.  
    7. class Type {
    8. Shape* _parent;
    9. public:
    10. Type(Shape* sh) : _parent(sh) {
    11. double d = _parent->compute();
    12. double d2 = _parent->_x; //it compiles....
    13. }
    14. };
    15. public:
    16. Type _type;
    17. Shape(double x = 0, double y = 0) : _type(this), _x(x), _y(y) { }
    18. double compute() {
    19. return _x * _y; }
    20. };
    To copy to clipboard, switch view to plain text mode 
    if i comment those lines, it complies anyway...
    Regards

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

    Default Re: friend class in TC++ book

    Could you prepare a minimal compilable example of the above?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: friend class in TC++ book

    Quote Originally Posted by mickey View Post
    double d2 = _parent->_x; //it compiles....
    The standard says:
    A nested class is a member and as such has the same access rights as any other member.
    Which means that nested classes behave as if they were friends of nesting class.

  10. #10
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: friend class in TC++ book

    Quote Originally Posted by wysota View Post
    Could you prepare a minimal compilable example of the above?
    the example is compilable;
    Thw question was: how come TC++ vol2 write those 2 lined commented in the post #7 (if they are unuseful) ???
    Regards

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: friend class in TC++ book

    Quote Originally Posted by mickey View Post
    how come TC++ vol2 write those 2 lined commented in the post #7 (if they are unuseful) ???
    Not everything what's written in a book is correct.

  12. The following user says thank you to jacek for this useful post:

    mickey (26th August 2008)

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

    Default Re: friend class in TC++ book

    I'd say this code in the book might be an explicit declaration of an implicit feature of the standard. Especially that we know some compilers tend to create their own "standards".

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

    mickey (26th August 2008)

Similar Threads

  1. class in the class
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 23rd July 2008, 07:01
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  4. QTextBrowser and friend function
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2006, 17:03
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.