using a class as a data member before you initialize the class?
Just say I have two classes set up like this:
class One
{
public:
int x;
Two two;
};
class Two
{
public:
Two()
{
One one;
one.x = 5;
}
};
It won't compile because I can't access a "Two" type before I initialize it.
What should I do?
Re: using a class as a data member before you initialize the class?
Quote:
Originally posted by class question
Just say I have two classes set up like this:
class One
{
public:
int x;
Two two;
};
class Two
{
public:
Two()
{
One one;
one.x = 5;
}
};
It won't compile because I can't access a "Two" type before I initialize it.
What should I do?
You can always prototype the second class before the first one. I.E.
Code:
class Two; // Prototype
class One
{
// blah
// blah
};
However looking over your code I think you are going about it wrong. In your Two class constructor you are making a temporary instance of the first class then assigning x the value of 5. Then what happens? That instance goes out of scope and is never heard from again. Re-think what you are trying to do.
@Travis Dane
Can you reword your question? Do you mean inheritance? I'm not quite sure what you are asking sorry.