VVI C And C 15 VVI C And C 15 - 10 months ago 68
C++ Question

C++ I want to do something like Class1.Class2.Fct()

I am new to C++ programming and I need your help:

Let's say I have a class - Class1. Is it possible to have another class (Class2) with (at least) one function that returns (for example) an integer? Here is an example with I want to achive:

std::cout << "I did it: " << Class1.Class2.ReturnINTEGER() << std::endl;

I need the code to be compatible with GNU G++ compiler. Thanks :).

Answer Source

Yes, it can be done. Let's dissect or rebuild. First to get Class1.Class2, we need to have a class member called Class2. And Class2 needs to have a member called Fct.

Here is one implementation:

struct Class2_Object
  void Fct(void);

struct Class1_Object
  Class2_Object Class2;

Class1_Object Class1;