Connecting Tech Pros Worldwide Help | Site Map

Class defined Inside a Class

  #1  
Old November 20th, 2008, 07:25 AM
Pallav singh
Guest
 
Posts: 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
  #2  
Old November 20th, 2008, 09:25 AM
mail.dsp@gmail.com
Guest
 
Posts: n/a

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
  #3  
Old November 21st, 2008, 09:55 AM
anon
Guest
 
Posts: n/a

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
  #4  
Old November 21st, 2008, 10:05 AM
Rolf Magnus
Guest
 
Posts: n/a

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?

  #5  
Old November 21st, 2008, 10:45 AM
anon
Guest
 
Posts: n/a

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
?
  #6  
Old November 21st, 2008, 11:15 AM
Rolf Magnus
Guest
 
Posts: n/a

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.

  #7  
Old November 21st, 2008, 11:55 AM
anon
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define operator "const char *" of enumerated type defined inside a class Bill Davy answers 7 September 25th, 2008 10:35 AM
How to call module functions inside class instance functions? beginner answers 7 August 19th, 2007 06:55 AM
Do functions defined inside a class need inline keyword ? ganesh.gella@gmail.com answers 5 July 23rd, 2005 07:17 AM
Types defined inside templates Ruben Campos answers 3 July 22nd, 2005 10:32 PM