View Full Version : friend class in TC++ book
mickey
21st August 2008, 23:38
hello,
I read TC++ vol2. (html ed 2004) at page 456 (chap design patterns) "the inner class idiom".
In the class "Outer" it writes:
class Outer {
.....
class Inner1;
friend class Outer::Inner1;
...........
};
I tried to get out those two lines and it compiles; why? What sense have they, please?
thanks,
wysota
22nd August 2008, 01:16
It means oobjects of the Outer::Inner1 class can access private members of Outer class instances.
mickey
22nd August 2008, 11:30
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:
class Shape {
class Type;
friend class Shape::Type;
int _number;
double _x, _y;
class Type {
Shape* _parent;
public:
Type(Shape* sh) : _parent(sh) {
double d = _parent->compute();
double d2 = _x; //is this an access ? It works only with "parent->_x"
}
};
Type _type;
public:
Shape(double x = 0, double y = 0) : _type(this), _x(x), _y(y) { }
double compute() { return _x * _y; }
};
Where can I access to provate member of Shape ? (and how?)
wysota
22nd August 2008, 12:05
Instances of Shape::Type can access _number, _x, _y and _type from instances of Shape.
mickey
22nd August 2008, 15:17
hello,
I don't find a way to access those private members; from where? from main?
//main.cpp
Shape s;
Shape::Type t(&s);
Or where, please?
jacek
24th August 2008, 01:17
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.
Shape::Type::Type(Shape* sh) : _parent(sh) {
...
double d2 = _parent->_x; // "this" is an object of Shape::Type class, so it can access
// _x in _parent which points to an instance of Shape.
}
mickey
24th August 2008, 11:55
hello,
i don't understand:
class Shape {
//class Type;
//friend class Shape::Type;
int _number;
double _x, _y;
class Type {
Shape* _parent;
public:
Type(Shape* sh) : _parent(sh) {
double d = _parent->compute();
double d2 = _parent->_x; //it compiles....
}
};
public:
Type _type;
Shape(double x = 0, double y = 0) : _type(this), _x(x), _y(y) { }
double compute() {
return _x * _y; }
};
if i comment those lines, it complies anyway...
wysota
24th August 2008, 22:26
Could you prepare a minimal compilable example of the above?
jacek
25th August 2008, 00:01
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.
mickey
25th August 2008, 20:14
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) ???
jacek
25th August 2008, 20:36
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.
wysota
25th August 2008, 23:08
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".
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.