0

I am fairly new to c++ and now trying to solve the following problem: There are two user defined classes in separate files. I want to access the members of the instantiating class (Master) from one of its members (MyObject). So in this example I want to access the other_member_ variable from the my_object instance:

Master class definition:

//master.h
#include "myobject.h"
class Master
{
  // ...
  public:
    MyObject* my_object_ = new MyObject(this);
    int other_member_;
  // ...
}

And "member class" definition:

//myobject.h
#include "master.h"
class MyObject
{
  // ...
  public:
    MyObject(Master* parent);
  // ...
}

How can I do this without having the include loop etc.?

Chris
  • 35
  • 5
  • Why do you want to do this? What is the actual relationship between these two classes? Can you elaborate on what you want? As it is, this sounds like an XY problem. – scohe001 Oct 28 '20 at 17:43
  • A forward declaration of one or other of the classes is needed to avoid the 'include loop' – john Oct 28 '20 at 17:46
  • You should avoid owning bare pointers. – eerorika Oct 28 '20 at 17:55

2 Answers2

2

Use a forward declaration header:

master.fwd.h

class Master;

master.h should then #include "master.fwd.h" to make sure they stay in sync.

myobject.h should also #include "master.fwd.h", which means it will know there's a class called Master, and you can define the constructor to take an argument of that type.

You can then have myobject.cpp #include "master.h" to get the full definition of Master so the constructor can access the Master object through the pointer.

Putting the class Master; directly into myobject.h is bad practice if you're working in a larger code base and Master and MyObject are maintained independently, so I recommend getting used to using forward declaration headers. The reasons to do so are explained in detail in this answer.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • A forward declaration is clearly the answer, but why do you suggest doing it via an include? why not just put class Master; directly into the MyObject.h ? (I'm not saying I think its wrong, it's a genuine question as to why you suggest this) – ROX Oct 28 '20 at 17:50
  • 1
    @ROX: please see the (new) last paragraph. Cheers – Tony Delroy Oct 28 '20 at 17:52
  • 1
    I like the "if you're working on a larger code base" on your bad practice statement (too many people throw comments like bad practice without those sort of caveats). Interesting other answer - I'd say that its not a problem I've encountered often, but given your point in that other answer about include analysis, I think I've probably encountered it a couple more times than I've identified it. – ROX Oct 28 '20 at 18:14
1
//master.h
#pragma once;

class MyObject;  // declaration (incomplete type, but ok)

class Master
{
  // ...
  public:
    Master();

    MyObject* my_object_;
    int other_member_;
  // ...
};

// Master.cpp
#include "master.h"
#include "MyObject.h"

Master::Master() : my_Object_(new MyObject(this)) { }

You probably want private data, need to worry about copying the pointer, deleting it, and all the usual concerns. But breaking the cycle with a forward declaration in the header, and move the use into a cpp file.

Chris Uzdavinis
  • 6,022
  • 9
  • 16