dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Using a class before its declaration in same file

  1. #1
    Join Date
    Mar 2009
    Posts
    29

    Using a class before its declaration in same file

    Hi,
    this is my first post here although i 've been reading for a while the various threads.
    I am a novice programmer and i'm having some issues lately.
    My current problem is :

    I declare a class A
    one of its members is an array of class B objects
    class B is then defined as a class derived from A.
    when i compile this won't work. I hope there is some command i can use so class A sees all the file so it can recognize B as a valid member.
    i.e.

    class A
    {
    ...
    B myArray[100] //!error! (is it because B is not yet declared?)
    ...
    }

    class B: public class A
    {
    ...
    }

    Thanx in advance ,
    Stakon

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Using a class before its declaration in same file

    Hi,

    Well, you can use forward declaration if you want to refer to the class before defining it. You can't get the compiler to read all the way to the end, so it won't actually know how class B is defined, so you can just add a reference to it, not an actual object.

    So, you could use:
    Code:
    class B;
    class A
    {
    ...
    B *myArray[100]; 
    ...
    };
    
    class B: public A
    {
    ...
    };
    but can't use
    Code:
    class B;
    class A
    {
    ...
    B myArray[100]; //!error! 
    ...
    };
    
    class B: public A
    {
    ...
    };

  3. #3
    Join Date
    Mar 2009
    Posts
    29

    Re: Using a class before its declaration in same file

    Thanks Alan

Tags for this Thread

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




On-Demand Webinars (sponsored)