tags:

views:

198

answers:

4

to create an instance of another class from a class even if the class I want to have an instance of is declared next of the class I am creating an instance from. Just like in C# and Java.

Thank you

+1  A: 

I guess you mean to do something like this:

class B; //Forward declaration

class A
{
  private:
   B b;
};


class B
{

};

This is not possible in C++ as the compiler needs to know the sizeof(B) when compiling class A.

As a solution what you can do is:

  class B; //Forward declaration

        class A
        {

          public:
          A();
          ~A();

          private:
           B* m_pB; //Store a pointer to B

        };


        class B
        {

        };

A::A() 
{
  m_pB = new B; //Create an instance of B
}

A::~A()
{
  delete m_pB; //Explictly deallocate the memory.
  m_pB = NULL;
}
Naveen
yes i know that. but what if I want to access a public variable or function of that class?
But the opposite (with A just below B) will compile fine.
David Rodríguez - dribeas
@maxx: Still that will not be possible as the compiler just knows that there is something called class B using the forward declaration, but it has no idea about its contents.
Naveen
@Naveen: not true, you just need to put the member function declarations for A in a .cpp file, and #include the declaration of B.
rlbond
A: 

hey maxx, once you forward declare B in the header you can use it in the .cpp file without trouble to access public variables and methods.

~jewels

Jewel S
can you please give me a working C++ example. Thank you
+2  A: 

You can use a forward declaration for some purposes, but you will need to have the complete type whenever you actually make use of it. It could be advisable to make the question more specific as to what you really want to achieve, as there might be simpler approaches to your problem (if order is important it might indicate a circular dependency between classes A and B, and that is not usually good).

class B;
class A
{
public:
   A( B b, B* bp, B& br ) : bp_(bp), br_(br) {} // ok, incomplete types can be used as argument types
   B f()
// { return B(); } // nok: cannot create objects of incomplete type
   ;
   B* f2() { return bp_; } // ok
   B& f3() { return br_; }; // ok
   void g() 
// { bp_->g(); br_.g(); } // nok: cannot call methods of an incomplete type
   ;
   void g( B const & b )
// { b.g(); } // nok: cannot call methods of an incomplete type
private:
   B * bp_; // ok (also const version)
   B & br_; // ok (also const version)
// B b_; // nok: B must be a complete type here
};
class B { // A is complete, any use is correct here
   void g() {}
};
// From here on, B is a complete type and can be used in any possible way
B A::f() {
   return B(); // ok, now B is complete
}
void A::g() {
   br_.g(); // ok, B is complete here
   bp_->g(); // ok, B is complete here
}
void A::g( B const & b ) {
   b.g(); 
}
David Rodríguez - dribeas
+1  A: 

Sure. You just need a forward declaration. Something like this works just fine:

#include <iostream>

class B;

class A {
public:
    void hello( B &b );
    void meow();
};

class B {
public:
    void hello( A &a );
    void woof();
};

void A::hello( B &b ) { b.woof(); }
void A::meow() { std::cout << "Meow!" << std::endl; }

void B::hello( A &a ) { a.meow(); }
void B::woof() { std::cout << "Woof!" << std::endl; }

int main() { A a; B b; a.hello( b ); b.hello( a ); return 0; }

The key here is that you can only use pointers or references to a class until it has been fully defined. So in the example I gave, the hello() method in A can be declared to take a reference to B, even though we haven't defined B at the point. After the definition of B, however, the definition of the method A::hello() is free to use B as it pleases.

Boojum
hey this really helped! Thank you
"you can only use pointers or references to a class until it has been fully defined". As far as I know, you can also pass classes by value to and from the functions...
SadSido