473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem inheriting nested class..

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

Nov 11 '07 #1
2 2632
Wolfgang wrote:
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>
Nov 11 '07 #2
On Nov 11, 11:50 am, Barry <dhb2...@gmail.comwrote:
Wolfgang wrote:
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>
Thanks a lot,

-Wg

Nov 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
7
by: Ryan Shaw | last post by:
I’m having a small problem with inheritance with a hierarchy of classes The example is Class Class Private m_classB as Class Class Class End Clas End Clas
2
by: Richard Myers | last post by:
Howdy, I haven't struck this before.... I am visually inheriting several forms from another. All works well but then i thought i'd create an enum that is used only by this form - not even its...
3
by: guy | last post by:
I have a problem with a class hierarchy. The inheriting and inherited class are in different solutions. In the base class, (which inherits from System.Windows.Forms.UserControl) i have a method...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
3
by: Buddy Ackerman | last post by:
I bought the source code for an app and was modifying it however instead of modifying the code directly I renamed a class and then created a new class with the old name and inhertied the odl (now...
8
by: JDavis | last post by:
I am using System.Net.Sockets to connect a client socket to a server that requires three inputs when I connect: host, port and an identification number that identifies the person connecting. ...
7
by: Joe Strout | last post by:
I have a function that takes a reference to a class, and then instantiates that class (and then does several other things with the new instance). This is easy enough: item = cls(self,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.