Connecting Tech Pros Worldwide Forums | Help | Site Map

Class defined Inside a Class

Pallav singh
Guest
 
Posts: n/a
#1: Nov 20 '08
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

mail.dsp@gmail.com
Guest
 
Posts: n/a
#2: Nov 20 '08

re: Class defined Inside a Class


On Nov 20, 12:22 pm, Pallav singh <singh.pal...@gmail.comwrote:
Quote:
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
anon
Guest
 
Posts: n/a
#3: Nov 21 '08

re: Class defined Inside a Class


Pallav singh wrote:
Quote:
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
Rolf Magnus
Guest
 
Posts: n/a
#4: Nov 21 '08

re: Class defined Inside a Class


anon wrote:
Quote:
Pallav singh wrote:
Quote:
>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?

anon
Guest
 
Posts: n/a
#5: Nov 21 '08

re: Class defined Inside a Class


Rolf Magnus wrote:
Quote:
anon wrote:
>
Quote:
>Pallav singh wrote:
Quote:
>>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
?
Rolf Magnus
Guest
 
Posts: n/a
#6: Nov 21 '08

re: Class defined Inside a Class


anon wrote:
Quote:
Rolf Magnus wrote:
Quote:
>anon wrote:
>>
Quote:
>>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?
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.

anon
Guest
 
Posts: n/a
#7: Nov 21 '08

re: Class defined Inside a Class


Rolf Magnus wrote:
Quote:
anon wrote:
Quote:
Quote:
>>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?

Quote:
>
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 :)
Quote:
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.
Closed Thread