Problem inheriting nested class..

W

Wolfgang

Hi,

I'm trying to understand how could I inherit a nested class. Say for
instance - I've four classes namely class A, class B, class C, class
D.

[ 1 ] class B is nested inside class A
[ 2 ] class D is nested inside class C
[ 3 ] I'm trying to inherit class D from class B.
[ 4 ] Instantiating 'class A::B' as 'abObj' and want to understand
which all constructors get called.

But the code seems to throw compilation error and any help is
appreciated. Pasting below sample code:

#include <iostream>
using namespace std;

class A{

public:
A(){
cout<<"A::A"<<endl;
}

class B{
public:

B(){
cout<<"B::B"<<endl;
}
~B(){
cout<<"B::~B"<<endl;
}

};

~A(){
cout<<"A::~A"<<endl;
}
};
class C{

public:
C(){
cout<<"C::C"<<endl;
}
class D{
public:
D(){
cout<<"D::D"<<endl;
}

~D(){
cout<<"D::~D"<<endl;
}
};
~C(){
cout<<"C::~C"<<endl;
}

};

class A::B : public C::D{

public:

A::B() {

cout<<"A::B::A::B"<<endl;
}


~A::B(){
cout<<"A::B:~A::B"<<endl;
}




};

int main(){

A::B abObj;
return 0;
}

Pls guide !

- Wg
 
B

Barry

Wolfgang said:
Hi,

I'm trying to understand how could I inherit a nested class. Say for
instance - I've four classes namely class A, class B, class C, class
D.

[ 1 ] class B is nested inside class A
[ 2 ] class D is nested inside class C
[ 3 ] I'm trying to inherit class D from class B.
[ 4 ] Instantiating 'class A::B' as 'abObj' and want to understand
which all constructors get called.

But the code seems to throw compilation error and any help is
appreciated. Pasting below sample code:

#include <iostream>
using namespace std;

class A{

public:
A(){
cout<<"A::A"<<endl;
}

class B{
public:

B(){
cout<<"B::B"<<endl;
}
~B(){
cout<<"B::~B"<<endl;
}

};

~A(){
cout<<"A::~A"<<endl;
}
};
class C{

public:
C(){
cout<<"C::C"<<endl;
}
class D{
public:
D(){
cout<<"D::D"<<endl;
}

~D(){
cout<<"D::~D"<<endl;
}
};
~C(){
cout<<"C::~C"<<endl;
}

};

class A::B : public C::D{

public:

A::B() {

cout<<"A::B::A::B"<<endl;
}


~A::B(){
cout<<"A::B:~A::B"<<endl;
}




};

int main(){

A::B abObj;
return 0;
}

Pls guide !

My first suggestion is format your code using spaces, instead of TABs.

Since you've already define A::B in the A class body, you violate ODR
when you define class A::B again in the enclosing namespace.

Solution 1:

<code>
class A {
public:
class B; // declare nested class B
};

class C {
public:
class D {};
};

class A::B : public C::D
{
public:
B() {} // not A::B
~B() {} // not ~A::B
};


....
</code>

Solution 2, let C and C::D goes first:

<code>
class C {
public:
class D {};
};

class A {
public:
class B : public C::D {};
};

....
</code>
 
W

Wolfgang

Wolfgang said:
I'm trying to understand how could I inherit a nested class. Say for
instance - I've four classes namely class A, class B, class C, class
D.
[ 1 ] class B is nested inside class A
[ 2 ] class D is nested inside class C
[ 3 ] I'm trying to inherit class D from class B.
[ 4 ] Instantiating 'class A::B' as 'abObj' and want to understand
which all constructors get called.
But the code seems to throw compilation error and any help is
appreciated. Pasting below sample code:
#include <iostream>
using namespace std;
class A{

class B{
public:


~A(){
cout<<"A::~A"<<endl;
}
};
class C{
public:
C(){
cout<<"C::C"<<endl;
}
class D{
public:
D(){
cout<<"D::D"<<endl;
}


class A::B : public C::D{

A::B() {



int main(){
A::B abObj;
return 0;
}
Pls guide !

My first suggestion is format your code using spaces, instead of TABs.

Since you've already define A::B in the A class body, you violate ODR
when you define class A::B again in the enclosing namespace.

Solution 1:

<code>
class A {
public:
class B; // declare nested class B

};

class C {
public:
class D {};

};

class A::B : public C::D
{
public:
B() {} // not A::B
~B() {} // not ~A::B

};

...
</code>

Solution 2, let C and C::D goes first:

<code>
class C {
public:
class D {};

};

class A {
public:
class B : public C::D {};

};

...
</code>

Thanks a lot,

-Wg
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top