forward declare classes?

if i have:

class first
{
  first()
  {
     second _s = second();
  }
};

class second
{
  second()
  {
   
  }
};

//

_s cannot be made, because 'class second' hasnt been looked at yet?....
apart from reordering them, how can i make this work?
rfterdarcAsked:
Who is Participating?
 
HendrikTYRConnect With a Mentor Commented:
You may do it like this:

 class first
 {
 public:
     first();
 };

class second
{
public:
  second()
  {
   
  }
};

first::first() {
    second _s = second();
}

ie. You may still first declare "first", but make sure that your declaration of second is then "included" before you use "second".

You may also do this:

class second; // The forward reference.

 class first
 {
 public:
     second *s;
     first();
 };

class second
{
public:
  second()
  {
   
  }
};

Here you declare that second will be a class.  Because you only declare a "second" pointer in class "first", the compiler does not need to know the exact size of "second" when it compiles the implementation of class "first"

Hope a combination of these will solve your problems

Regards
Hendrik
0
All Courses

From novice to tech pro — start learning today.