Hi, I have a question about classes:
this is hypothetical so the code may not be 100% correct, i'm just using it as reference.
Lets say I have two classes:
class A constructs an array of four numbers.
1 2 3 4 5 6 7
class A{
int list[4];
public:
A(){
list={1,2,3,4};
};
};
and class B constructs a list of two numbers taken from the list in class A.
1 2 3 4 5 6 7 8 9 10
class B{
public:
B(???);
};
B::B(???){
int twonumbers[2];
????;
}
now in my main code I want to call an instance of class A and two instances of class B. The first time I call class B it takes the first two numbers of the list from class A and uses them in its constructor. The second time I call class B it takes the next two numbers from Class A's list and uses them.
What would code would I have to add to class B in order to make this work?
The first time I call class B it takes the first two numbers of the list from class A and uses them in its constructor. The second time I call class B it takes the next two numbers from Class A's list and uses them.
That would be an ill advised approach. It's best if objects don't rely on the initialization order of other objects.
You could do something like this instead:
1 2 3 4 5 6 7 8 9 10
class B{
public:
B(const A& a,bool first)
{
if(first)
{ /* use first two a.list members*/ }
else
{ /* use next two a.list members */ }
}
};
You could also get fancy and have a static B function that creates 2 B's objects based on a single A object. But that'd be a bit more work.