Re: Class defined Inside a Class
anon wrote:
Rolf Magnus wrote:
anon wrote:
Pallav singh wrote:
Hi
when we should have Class defined Inside a Class ? can any one give me
explanation for it ?
Does it is used to Hide some information of Class Data-Member and
Function from friend class?
class A : public B
{
private:
class C : public D::C1
{
DataType data1;
Function_1( ){ }
};
private:
friend class E;
DataType data2;
Function_2( ){}
};
IMHO it is better to make interfaces simple (see
http://en.wikipedia.org/wiki/KISS_principle), and not do this.
If you have such class in the private part, better move it to the
anonymous namespace (off course in the cpp file).
If you have it in the public part, create a new namespace, and move it
there
To pick up the iterator example, how would you suggest to do that?
Whats wrong with it?
I'm thinking e.g. about the iterator for std::vector, which is defined within
std::vector, so it's then std::vector::iterator. How would you name the
iterator type belonging to std::vector instead? And how would that
additional namepace come into play?
Is this ok :
http://www.cplusplus.com/reference/misc/iterator/iterator.html
?
Well, that's an iterator for simple arrays, but usually, an iterator type
belongs to a specific class.
Btw: The example on that page seems to contain an error:
myiterator& operator++() {++p;return *this;}
myiterator& operator++(int) {p++;return *this;}
That doesn't look right. The second operator++ is supposed to return the
previous value of *this, not the new one.