[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Class defined Inside a Class

Pallav singh

11/20/2008 7:22:00 AM

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
7 Answers

Angela Gupta

4/3/2008 12:08:00 AM

0

In article <fsutf50eph@drn.newsguy.com>, Trudy Johnson says...
>
>In article <fsug8q024vn@drn.newsguy.com>, Angela Gupta says...
>>
>>In article <fss6h60ldf@drn.newsguy.com>, Trudy Johnson says...
>>>
>>>Faster approval of drugs, of course, is a very good thing if you need a
>>>lifesaving medicine. Many patients are clamoring for that speed. Review times
>>>have been cut from 27 months to less than a year. Vioxx was fast-tracked in just
>>>six months. But some argue that the pendulum has swung too far. "A lifesaving
>>>drug should be sped along," says Steven Nissen, MD, chair of the department of
>>>cardiovascular medicine at the Cleveland Clinic and a frequent advisor to the
>>>FDA. "But with user fees, we've pressed the accelerator on all drugs, and that's
>>>a mistake." Readers Digest, April 2008
>>>
>>>Let's not fool ourselves, folks, antidepressants are NOT life saving drugs, yet
>>>they are being pushed through and they ARE VERY dangerous to the health of
>>>patients who take them, and even the health of those who don't take them but
>>>know someone who does.
>>>
>>>Trudy
>>>
>>Hey, isn't Vioxx the anti-inflammatory that ended up causing heart attacks? The
>>FDA fast-track strikes again!
>>Angela
>>
>You got it! FDA has been very negligent and many have been "purchased" by big
>pharma
>
>Trudy
>
Bought and paid for by the pharmaceutical companies, no question.
Angela

mail.dsp

11/20/2008 9:18:00 AM

0

On Nov 20, 12:22 pm, Pallav singh <singh.pal...@gmail.com> 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( ){}
>
> };
>
> 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

11/21/2008 9:47:00 AM

0

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...), 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

11/21/2008 10:03:00 AM

0

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...), 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

11/21/2008 10:36:00 AM

0

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...), 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/misc/iterator/ite...
?

Rolf Magnus

11/21/2008 11:05:00 AM

0

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...), 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/ite...
> ?

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

11/21/2008 11:47:00 AM

0

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/misc/iterator/ite...
>> ?
>
> 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.