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:
Code:
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,
Re: friend class in TC++ book
It means oobjects of the Outer::Inner1 class can access private members of Outer class instances.
Re: friend class in TC++ book
Quote:
Originally Posted by
wysota
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:
Code:
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?)
Re: friend class in TC++ book
Instances of Shape::Type can access _number, _x, _y and _type from instances of Shape.
Re: friend class in TC++ book
hello,
I don't find a way to access those private members; from where? from main?
Code:
//main.cpp
Shape s;
Shape::Type t(&s);
Or where, please?
Re: friend class in TC++ book
Quote:
Originally Posted by
mickey
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.
Code:
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.
}
Re: friend class in TC++ book
hello,
i don't understand:
Code:
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...
Re: friend class in TC++ book
Could you prepare a minimal compilable example of the above?
Re: friend class in TC++ book
Quote:
Originally Posted by
mickey
double d2 = _parent->_x; //it compiles....
The standard says:
Quote:
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.
Re: friend class in TC++ book
Quote:
Originally Posted by
wysota
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) ???
Re: friend class in TC++ book
Quote:
Originally Posted by
mickey
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.
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".