You are here:

C++/C Nestled Classes

Advertisement


Question
Hey, I was wondering if it is possible, in any way, to create nestled C   classes. I'm probably using the wrong wording, so I'll just put it out in code what I want done :P

class A
{
  int a;
  int b;
  void c(int input, C *classpointer);
};
class B
{
   A a;
};
class C
{
   C data;
}

So, A has a function which needs C as an input, where C acts as a database of B classes (so it has an array of them.) B contains one or several A classes, and C also contains several B classes.
Any way to make this possible? Or do I just need to have a global function which takes A and C as inputs?

Answer
First yes it is possible to have nested (not nestled) classes.

Second, yes you are using the term incorrectly!

I think this may be due to a confusion between a class, which is a type, and instances of a class which are objects.

A nested class refers to the practice of defining one class inside the definition of another (that is an inner class definition is nested inside an outer class definition), thus:

   class Outer
   {
   // ...

       class Inner
       {
       // ...
       };

   // ...
   };

However, you are asking if one class can have data members that are objects of other classes.

The answer is yes. However, just as with C structs, there is a gotcha. For a class to use a type that is a class object (rather than a pointer or reference to a class object) the compiler has to have seen (that is successfully compiled) the full definition of the class. This is so it can, among other things, calculate the amount of space required for the member.

For example the following is OK:

   class A
   {
   // ...
   };


   class B
   {
   // ...

       A iA;

   // ...
   };


   class C
   {
   // ...

       B iB;

   // ...
   };

This is because when the definition of class B is compiled the definition of class A has been compiled and when the definition of class C is compiled the definition of class B has been compiled.

On the other hand the following is not allowed:

   class A
   {
   // ...

       C iC;

   // ...
   };

   class C
   {
   // ...
   };

This is because when the compiler compiles the definition for class A it has no knowledge of the structure and form of class C. In fact it has no knowledge of the name of the class C!

We can declare that there is something called class C before we refer to it in class A. The compiler will then know the name of the class but not its definition. This allows us to refer to pointers or references to objects of class C:

   class C;      // declaration that there is a class called C

   class A
   {
   // ...

       C * iCPtr;

   // ...
   };

Such class declarations are sometimes called forward declarations.

A similar situation exists for classes that refer to themselves. In this case however the start of the class definition is also a declaration that such a class exists, so we can do the following:


   class SelfRef
   {
   // ...

       SelfRef * iMoreMePtr;

   // ...
   };

In your example then all you need to do is add a declaration for class C so that the name is known when class A is defined, so the compiler accepts the classpointer parameter to the c member function:

   class C;

   class A
   {
      int a;

      int b;

      void c(int input, C *classpointer);
   };

   class B
   {
       A a;
   };

   class C
   {
       B data;
   };

Note that in your example you had the C::data member as a C type. This is not allowed as the compiler cannot know all the details of class C while class C is still being defined. You would have to use a pointer or reference to a C object. I have modified the definition of C to match what you implied in your following explanatory text.

Again note that class B does not contain any A classes; it has a member that is an object of type A. Likewise C does not contain any B classes; it has a member that is an object of type B. This means that objects of type B contain an object of type A and objects of type C contain an object of type B.

I leave it to you to expand on this scheme to add in the multiplicity of objects required.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.