Solved

forward declare classes?

Posted on 2004-10-25
183 Views
Last Modified: 2010-05-18
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?
0
Question by:rfterdarc
    1 Comment
     
    LVL 3

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    A short article about a problem I had getting the GPS LocationListener working.
    This is an explanation of a simple data model to help parse a JSON feed
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now