problems with mutual dependence
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: problems with mutual dependence

  1. #1
    Join Date
    Jul 2007
    Posts
    9

    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){};
    };

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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.

  3. #3
    Join Date
    Jul 2007
    Posts
    9

    Re: problems with mutual dependence

    Thank You

  4. #4
    Join Date
    Sep 2006
    Location
    Sunshine State
    Posts
    517

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center