-
November 12th, 2007, 09:43 AM
#1
problems with mutual dependence
Maybe my c++ is not strong enough, but can't make the compiler pass my code.
I need a base class that is a template, and two derived classes having member fields of the other derived class. Not just pointers to the other class.
The simplified code is below. Here, members are pointers. It compiles, but when I try to use classB or classA members in the other class instead of just pointers, the compilation failes whatever I try. How can I set up the structure to compile correctly?
Thx in advance,
George
//==== classBase.h ======================
template <typename T>
class classBase{
T m_b;
public:
classBase(T i){m_b=i;}
};
//------classA.h ---------------
#pragma once;
#include "classBase.h"
#include "ClassB.h"
class classB;
class classA : public classBase<int>{
classB* m_B; // //I want: classB m_B
public:
classA(int i): classBase(i){};
};
//-------classB.h ----------------
#pragma once;
#include "classBase.h"
#include "classA.h"
class classA;
class classB : public classBase<double>
{
classA* m_A; //I want: classA m_A
public:
classB(double i): classBase(i){};
};
-
November 12th, 2007, 09:54 AM
#2
Re: problems with mutual dependence
It´s impossible to do what you´re trying to do, the compiler needs to know class B´s layout to create class A´s layout, which, in turn, needs to know class B´s layout. You can either use pointers or references, but you can´t use objects.
- Guido
-
November 12th, 2007, 10:19 AM
#3
Re: problems with mutual dependence
-
November 12th, 2007, 08:37 PM
#4
Re: problems with mutual dependence
Maybe use multiple inhertiance in your structure.
I.e.
Code:
class Base
{ ... };
class A
{ .... };
class B : public Base, public A
{ ... };
Otherwise, your solution to use pointers is the only solution, and is not neccessarily "dirty".
-Andy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|