所以我做了两个类:
class Husky{
private:
int age;
int weight;
public:
//Usual Constructor, set, get functions here
//......
void foo(); //<-----This is the problem function. In here, I change the weight variable.
}
这是第二类:
class Dog{
private:
vector<Husky> h;
public:
Husky getHusky(int index); //Returns the Husky element at the index given.
void setHusky(Husky x){h.push_back(x);} //Adds a Husky element to the 'h' array.
}
在我的主要:
int main(){
//Note: the initialized value of `weight` is 0.
Husky p;
p.foo();
cout << p.getWeight() << endl; //This couts 5. foo() DID change 'weight'.
Dog g;
Husky s;
g.push_back(s);
g.getHusky(0).foo();
cout << g.getHusky(0).getWeight() << endl; //This couts 0. foo() DID NOT change 'weight'.
// Why did this not print out 5?
}
请给我一个线索或一些东西来指引我正确的方向。