Class defined Inside a Class

P

Pallav singh

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( ){}
};

Thanks
Pallav
 
M

mail.dsp

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( ){}

};

Thanks
Pallav

It is matter of object properties, abstraction and encapsulation.

Like in case of iterator design, iterator class is usually defined
inside container class because this iterator specific to container
class object. This is very general example. There are so many complex
example. I suggest you to study OO design patterns.
 
A

anon

Pallav said:
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
 
R

Rolf Magnus

anon said:
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?
 
R

Rolf Magnus

anon said:
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?

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.
 
A

anon

Rolf said:
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?

This is from the gcc "vector" header:

*************************************************************
namespace __gnu_debug_def
{
template<typename _Tp,
typename _Allocator = std::allocator<_Tp> >
class vector
: public _GLIBCXX_STD::vector<_Tp, _Allocator>,
public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
{
// some stuff

public:

typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,vector>
iterator;
typedef __gnu_debug::_Safe_iterator<typename
_Base::const_iterator,vector>
const_iterator;
*************************************************************

So, why are you saying the iterator is defined within the vector class?

Well, that's an iterator for simple arrays, but usually, an iterator type
belongs to a specific class.

Do you have an example? Explaining how the iterator is defined within
the vector class would do :)
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.

Correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top