Question

I'm definitely in a desperate case... Using C++, I have defined those two classes:

// Console.hpp
#ifndef CLASS_Console
#define CLASS_Console
#include <iostream>
#include "Window.hpp"

class Console
{
public:
    Window *Win;
    Console(Windows *Win); // Which then does "this->Win = Win;"
    void AppendText(std::string);
    // And some more dozens functions
};
#endif

// Window.hpp
#ifndef CLASS_Window
#define CLASS_Window
#include <iostream>
#include "Size.hpp"

class Window
{
private:
    Console *m_Console;

public:
    Window(); // Which then does "m_Console = new Console(this); m_Console->AppendText("YEAH");"
    Console *getConsole(); // For use by another classes
    Size getSize();
    // And some more Tens and Tens of functions
};
#endif

At first, one will say: "Use forward declaration!"

But given I need to access a number of those dozens of functions (In one way and in the another), is there any way I can avoid using forward declaration?

Thanks for your (future) answers!

Was it helpful?

Solution

But given I need to access a number of those dozens of functions (In one way and in the another), is there any way I can avoid using forward declaration?

I am not sure you understand what the forward declaration does. You only need to provide one forward declaration in window.hpp for Console, and then include console.hpp from window.cpp.

OTHER TIPS

No, there's no way to avoid (at least a) forward declaration.

But given I need to access a number of those dozens of functions

Why would this matter? Performance? Have you measured anything yet? If not, you can't talk about performance.

The normal structure is:

  1. declare the first class: class Console;
  2. define the second class: class Window { Console *c; ...};
  3. define the first class: class Console { Window *w; ... };
  4. define the member functions Console::Console() { ... } ...

The first class declaration is enough to let you write the definition of the other class (without the definitions of the member functions).

Then after the definitions of both classes, the member functions are declared, so the code for the member functions can use the members of the other class as needed.

If only one of these class need to be accessed from "outside" by design you could consider nesting Console inside Window (or the other way around)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top