-2

what I am trying to do is:

  • Have a Sub class object in Base class.
  • Make Sub class access Base class'es variables

Base.h

#include "Sub.h"
class Base
{

Sub subobject
int x;
}

Sub.h

#include Base // to acces x from .cpp file
class Sub: public Base
{
void changevar();
}

Sub.cpp

#include "Sub.h"
// I tried to include base in here but that did not work either
void Sub::changevar()
{
x++;
}

But I keep getting undefines base class and undefined undeclared x error. How can I solve this?

8
  • You can't do this. Dec 2, 2018 at 14:08
  • @πάνταῥεῖ Isn't there any other way?
    – Stel Team
    Dec 2, 2018 at 14:08
  • 1
    What's the sizeof(Base) - the size, in bytes, of Base class? It would be large enough to contain an int and a Sub. The latter, being derived from Base, should contain a subobject of Base. Which in turn contains x and subobject, which in turn contains Base... It's turtles all the way down. As to "any other way" - it rather depends on what you are trying to achieve. What's the ultimate goal of the exercise? Suppose you somehow managed to implement what you want - how would you use it? Show the example of calling code. Dec 2, 2018 at 14:33
  • @IgorTandetnik Base class is Game class and Sub class is Logic class. Game class needs some functions from logic class, and logic class needs some variables from game class. That's the problem. I can't find a solution to this
    – Stel Team
    Dec 2, 2018 at 14:37
  • Why is Logic derived from Game? It doesn't sound like Logic is-a Game. Probably Logic just needs a pointer to Game. Dec 2, 2018 at 14:38

2 Answers 2

1

What if you store the suboject as a pointer instead?

You will need to also

  • Forward declare your Sub class in Base.h before your Base class
  • Include the Sub.h in your Base.cpp file
  • Call new for it to have something pointing to it and obviously a corresponding delete (note: not in the constructor as that will create a loop where a Base creates a Sub that creates a Base that creates a Sub)

However it seems a bit odd a base have a reference to a subclass, it breaks the whole point of inheritance and if you are needing to do this, then you should reconsider if what you are doing is correct.

Also note, the Sub will have its own Base as part of it so the Base part of the subobject will not be the same as the outer Base.

e.g. if Base itself had an integer called Y, then we would have the Y of Base, but a separate Y for subobject also.

Maybe explain a bit better why the base class needs a copy of a subclass?

Rough code sketch:

Base.h

#include "Base.h"

class Sub;

class Base
{
public:
    Sub* subobject;
    Base();
    ~Base();

    void createSub();
};

Base.cpp

#include "Base.h"
#include "Sub.h"

Base::Base()
{
    subobject = new Sub();
}

Base::~Base()
{
    delete subobject;
}

void Base::createSub()
{
    if (subobject)
        return;

    subobject = new Sub();
}

Sub.h

#include "Base.h"

class Sub : public Base
{
    void changevar();
    int x = 0;
};

Sub.cpp

void Sub::changevar()
{
    x++;
}
2
  • I have added the code and refined a few things, note this is not a good idea to do and if you can clarify what it is you actually are trying to do then it can probably be answered in a different way Dec 2, 2018 at 15:34
  • Thanks I made it!
    – Stel Team
    Dec 2, 2018 at 15:36
0

You can't do exactly that, but you should be able to do something similar using CRTP.

#include <iostream>

using namespace std;

template <typename CRTP>
class Base
{
public:
    CRTP* subobject{ static_cast<CRTP*>(this) };
    int x = 0;
    
    void foo()
    {
        cout << x << endl;
    }
};

class Derived : public Base<Derived>
{
public:
    void changevar()
    {
        x++;
    }
};

int main()
{
    Derived d;
    d.foo();
    d.changevar();
    d.foo();
    d.subobject->changevar();
    d.foo();

    return 0;
}

Output:

0 1 2

The reason you can't declare a member variable of Derived class type in Base is because it would require circular includes. Base would need to include Derived to know how much memory to allocate for the member variable, while Derived would need to include Base because it inherits it. But if you stick to a pointer which always takes up the same memory and use CRTP to pass the type up the hierarchy, it can be done. Additionally, as the pointer is initialized from this, it should never be nullptr as long as its containing class (and its subclass) exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.