-
March 23rd, 2009, 04:24 AM
#1
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
-
March 23rd, 2009, 04:48 AM
#2
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
{
...
};
-
March 24th, 2009, 02:14 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|