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?
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?
putting it in code
Code:class One { public: int x; Two two; }; class Two { public: Two() { One one; one.x = 5; } };
I have a question on top of that, how can you get all the
function of a certain class to use the same instance of another
class?
You can always prototype the second class before the first one. I.E.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?
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.Code:class Two; // Prototype class One { // blah // blah };
@Travis Dane
Can you reword your question? Do you mean inheritance? I'm not quite sure what you are asking sorry.
Hi, thanks MrWizard. My classes in the question were more of an example.
I have a class called ABC and a class called DEF wich is a child
of ABC, now i want to use the same object of the class DEF in
ABC's functions.
[edit]
I could solve this by creating a global object of DEF but that's
what im trying to avoid
If that worked, it would lead to infinite recursion. Do you see why C++ has its rules?Originally posted by class question
putting it in code
Code:class One { public: int x; Two two; }; class Two { public: Two() { One one; one.x = 5; } };
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling