By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,167 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,167 IT Pros & Developers. It's quick & easy.

Class defined Inside a Class

P: n/a
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
Nov 20 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 20, 12:22 pm, Pallav singh <singh.pal...@gmail.comwrote:
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.

--
Daya
Nov 20 '08 #2

P: n/a
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
Nov 21 '08 #3

P: n/a
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?

Nov 21 '08 #4

P: n/a
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?

Is this ok :
http://www.cplusplus.com/reference/m.../iterator.html
?
Nov 21 '08 #5

P: n/a
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/m.../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.

Nov 21 '08 #6

P: n/a
Rolf Magnus wrote:
anon wrote:
>>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?
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?

>Is this ok :
http://www.cplusplus.com/reference/m.../iterator.html
?

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.
Nov 21 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.