开发者

c++ forward referencing

开发者 https://www.devze.com 2023-02-14 16:26 出处:网络
I know this is probably really stupid but how does one do forward reference of inherited classes Like class parent

I know this is probably really stupid but how does one do forward reference of inherited classes

Like

class parent
{
...stuff in here
}


class child1 : public parent
{
....
}

class child2 : public parent
{
....
}

All the classes have to be in the same file and I'm using instances of child2 inside child1 and vice verse. How do I do the forward referencing here? If I just say

class child2;

at the beginning of the code the compiler does not recognize that it is a subclass of parent when I use it in child1 (and hence the virtual functions don't开发者_如何学编程 work). Please help.


Adding up all of the advice into one compilable example, we have:

#include <iostream>


class parent {
private:
  int m_baseData;
public:
  parent(int myData) : m_baseData(myData) {}
  int GetData() { return m_baseData; }
};

class child2;

class child1 : public parent {
private:
  child2 *m_pChild2;
public:
  int GetSiblingData(); /* { return m_pChild2->m_baseData; } */
  child1(int myData, child2& c2) : parent(myData), m_pChild2(&c2) {}
};

class child2 : public parent {
public:
  child2(int myData) : parent(myData) {}
};

inline
int child1::GetSiblingData() {
  return m_pChild2->GetData();
}


int main() {
  child2 c2(56);
  child1 c1(42, c2);
  std::cout << c1.GetSiblingData() << "\n";
}

Please note several things:

  • First, the forward declaration of class2. This declares an incomplete type. Incomplete types may be used in pointers and references, but those pointers may not be dereferenced unless the complete type is declared.
  • Next, notice that I did not include the function body for child1::GetSiblingData() in the child1 class definition. This is because child2 is still an incomplete type. As you correctly observed, the compile does not yet know what inheritance of child2 is yet.
  • Note that the definition of child2 may occur even when a previous forward declaration of child2 exists. This definition means that child2 is no longer an incomplete type.
  • Finally, notice that definition of child1::GetSiblingData follows the definition of both child1 and child2. By this point in the program, child2 is no longer incomplete, so you can mention its members.


Try

class child1
{
    ...
    class child2 *c;
    ...
};

class child2
{
    ...
    class child1 *c;
    ...
};


You're talking about forward declarations, not forward references.

All the classes have to be in the same file and I'm using instances of child2 inside child1 and vice verse. How do I do the forward referencing here? If I just say

class child2;

at the beginning of the code the compiler does not recognize that it is a subclass of parent when I use it in child1 (and hence the virtual functions don't work). Please help.

You can't "use" it at all through a forward declaration. Use the forward declaration as you suggest:

class child2;

so that you can declare functions the classes, then define the function bodies in some .cpp file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号