Link to home
Start Free TrialLog in
Avatar of cwgues
cwgues

asked on

Class Design Problem

Does anyone know a way in which I can achieve this?  

Class A
{
    B *classb;

    void somefunc(void)
    {
       classb = new B(this);    
    }
};

Class B
{
    B(A &a);
};

I seem to have got myself into "a which came first" scenario....
Avatar of cwgues
cwgues

ASKER

Edited text of question.
This compiles ok.  Your constructor B::B was private and had no body.

#include <iostream>

class A;

class B
{
public:
   B(A *a) {};
};

class A
{
   B *b;

   void func(void)
   {
      b = new B(this);
   }
};

void main(void);

void main()
{
   A a;
   B b(&a);
}
ASKER CERTIFIED SOLUTION
Avatar of sureshkumar
sureshkumar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need to do something called forward declararion of class B; you are telling the compiler that a class named B will exist. It is allowed to use pointers and refrences to a forward declaraed class.

class B;
class A
{
    B *classb;
  public:
    void somefunc(void);
};

class B
{
  public: // putting this public is a good idea
    B(A &a);
};

void A::somefunc(void)
{
   classb = new B(this);      
}

class B; // Forward declaration

class A
{
    B *classb;
    // You can not define this function here as B class is not yet declared

    void A::somefunc(void);
};

class B
{
public: // Make the constructor public
    B(A &a){} // Define it here or outside the class
};


void A::somefunc(void)
{
  classb = new B(*this);
}

void main()
{
    A a;
    B b(a);
}
Avatar of cwgues

ASKER

Cheers lads/lasses....

I knew there would be a simple answer......
cwgues,

I answered first, how come I don't get the points?
Maybe it wasn't completelely correct. The best answer was forward declararion of class B
On the other hand, it is more polite from the questioneer if he requests for clarification if a given answer seems to be incomplete.
Too bad the accepted answer isn't completely correct either ;)
The code in my answer contains the forward declaration.  I provided the code that solved cwgues' problem plus pointed out two more issues that kept it from compiling.

You're right though, I guess I should have pointed out the forward declaration explicitly since it was the main point.  However the complete answer was there.
Avatar of cwgues

ASKER

I just needed to know how to do the forward declaration the other corrections are superficial  

You are all right, however sureshkumar was the first person to "propose the answer" I don't know if you can give points to a "Comment"... using this system..... I would if I could.....

If you really want some points I will construct a nonsense question for you to answer with equivalent points...  
If you want me to do this then say so by putting another comment here....  

I just want to get back to my coding...


It should be possible to grade a comment as an answer. The blue title bar of a comment is supposed to have a tiny link to the right, saying something like 'grade this comment' (I'm not sure). This is off course only possible (and visible) if a question hasn't been answered yet.