在其他对象中传递对象实例时遇到麻烦。在主应用程序的结尾,我希望iDogClassOne和iDogClassTwo引用同一对象,并且我希望该对象包含放置在variableA中的数字。我在这里做错了什么? tempDogClass,iDogClassOne和iDOgClassTwo的地址都不同,所以我可以肯定,我当前正在创建3个我不想做的dog类实例。..任何帮助将不胜感激。
class dogClass{
public:
void setVariableA(std::vector<double> newNum) {
variableA.push_back(newNum);
}
void printVariableA() {
if (variableA.size() != 0) {
std::cout << variableA[0][0] << std::endl;
std::cout << variableA[0][1] << std::endl;
}
}
dogClass &dogClass::operator=(const dogClass &src) {
variableA = src.variableA;
return *this;
}
private:
std::vector<std::vector<double>> variableA;
};
class animalClass{
public:
void addNumberToDog(std::vector<double> num) {
dogClass tempDogClass;
getDogOne(tempDogClass);
tempDogClass.setVariableA(num);
std::cout << "Address of temp Dog: " << &tempDogClass << std::endl;
std::cout << "Variables in tempDog:\n";
tempDogClass.printVariableA();
std::cout << std::endl;
}
void getDogOne(dogClass &dogOne) {
dogOne = dogs[0];
}
void setDogOne(dogClass &dogOne) {
dogs.push_back(dogOne);
}
private:
std::vector<dogClass> dogs;
};
int main() {
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne);
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum);
dogClass iDogClassTwo;
iAnimalClass.getDogOne(iDogClassTwo);
//Why are these not equal. Also why does dog Two not contain the values in dog One?
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << std::endl;
std::cout << "Address of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
std::cout << std::endl;
return 0;
}
控制台应用程序的输出(为什么iDogClassOne和iDogClassTwo不包含数字?为什么tempDog,iDogClassOne和iDogClassTwo不是同一对象?)
Address of temp Dog: 00000000002AF8A0
Variables in tempDog:
12
15
Address of dog One: 00000000002AF938
Variables in dog One:
Address of dog Two: 00000000002AF920
Variables in dog Two:
答案 0 :(得分:3)
因为您正在许多地方制作dogClass
对象的副本。
请考虑值,引用和指针的区别。
例如,在main()
函数中,您调用iAnimalClass.setDogOne(iDogClassOne);
,该参数作为引用传递,可以。但是随后您将其复制到向量中。这有点棘手,因为您不能在向量中传递引用。在这种情况下,您将必须使用指向dogClass
的指针向量。
现在您已经有两个dogClass
的实例。主要功能中的一个,animalClass
向量中的另一个。
下一个问题是在main()
函数中,您可以实例化 iDogClassTwo
。
现在您有三个dogClass
的实例。
然后调用iAnimalClass.getDogOne(iDogClassTwo);
,它将狗从向量复制到主函数中的狗。
这是更正的版本:
#include <vector>
#include <iostream>
class dogClass{
public:
void setVariableA(std::vector<double> newNum) {
variableA.push_back(newNum);
}
void printVariableA() {
if (variableA.size() != 0) {
std::cout << variableA[0][0] << std::endl;
std::cout << variableA[0][1] << std::endl;
}
}
dogClass & operator=(const dogClass &src) {
variableA = src.variableA;
return *this;
}
private:
std::vector<std::vector<double>> variableA;
};
class animalClass{
public:
void addNumberToDog(std::vector<double> num) {
dogClass& tempDogClass = getDogOne();
tempDogClass.setVariableA(num);
std::cout << "Address of temp Dog: " << &tempDogClass << std::endl;
std::cout << "Variables in tempDog:\n";
tempDogClass.printVariableA();
std::cout << std::endl;
}
dogClass& getDogOne() {
return *dogs[0];
}
void setDogOne(dogClass &dogOne) {
dogs.push_back(&dogOne);
}
private:
std::vector<dogClass*> dogs;
};
int main() {
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne);
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum);
dogClass& iDogClassTwo = iAnimalClass.getDogOne();
//Why are these not equal. Also why does dog Two not contain the values in dog One?
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << std::endl;
std::cout << "Address of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
std::cout << std::endl;
return 0;
}
答案 1 :(得分:0)
为什么iDogClassOne和iDogClassTwo不包含数字?
int main()
{
animalClass iAnimalClass;
dogClass iDogClassOne;
iAnimalClass.setDogOne(iDogClassOne); // pushes a copy of iDogClassOne
// into a vector animalClass::dogs
std::vector<double> newNum;
newNum.push_back(12);
newNum.push_back(15);
iAnimalClass.addNumberToDog(newNum); // Only operates on function local variable
// variable tempDogClass without having
// any effect on the outside world.
dogClass iDogClassTwo;
iAnimalClass.getDogOne(iDogClassTwo); // copies the copy of iDogClassOne earlier pushed
// into the vector animalClass::dogs to iDogClassTwo
std::cout << "Address of dog One: " << &iDogClassOne << std::endl;
std::cout << "Variables in dog One:\n";
iDogClassOne.printVariableA();
std::cout << "\nAddress of dog Two: " << &iDogClassTwo << std::endl;
std::cout << "Variables in dog Two:\n";
iDogClassTwo.printVariableA();
}
为什么tempDog,iDogClassOne和iDogClassTwo不是同一对象?
在下面的示例中,为什么a,b和c不是同一对象?
void func()
{
int c;
}
int main()
{
int a;
int b;
func();
}