Saturday, 10 August 2013

Using an instance of a class inside another class

Using an instance of a class inside another class

Here is a simplified version of what I'm trying to do
class Firstclass
{
public:
Firstclass(int x)
{
//do things with x;
}
};
class Secondclass
{
public:
Secondclass()
{
Firstclass a(10);
}
void func()
{
//Do things with a
}
private:
Firstclass a;
};
So I have a class (Firstclass) with a constructor that takes an int
argument. Now I'd like to create an instance of that class inside the
constructor of another class (Secondclass).
The lines
private:
Firstclass a;
if what I'd do if a were just a variable instead of a class: mention it
first such that I can use it elsewhere (in the function func() for
instance). This doesn't seem to work with classes, because the compiler
doesn't understand what the constructor of Secondclass is supposed to do.
How do I do this correctly?

No comments:

Post a Comment