[英]How to change class member value in vector
#include <iostream>
#include <string>
using namespace std;
class Owner{
protected:
string ownerName;
int balance;
int numPetsCheckin;
string pet[100];
public:
Owner();
Owner(const string& ownerName, int balance);
string getOwnerName(){return ownerName;}
int getBalance(){return balance;}
int getNumPetsCheckin(){return numPetsCheckin;}
void pushListPet(string a){
pet[numPetsCheckin] = a;
numPetsCheckin++;
//cout << numPetsCheckin;
}
void showOwners(){
cout << "Owners { name: " << ownerName << ", balance: " << balance << ", numPetsCheckin: " << numPetsCheckin << "}\n";
}
};
Owner:: Owner()
: ownerName("No name"), balance(0), numPetsCheckin(0){}
Owner:: Owner(const string& theName, int theBalance)
: ownerName(theName), balance(theBalance), numPetsCheckin(0){}
int main(){
int people = 0;
Owner owner[100];
Owner A( "A", 10);
owner[people] = A;
people++;
Owner B("B", 20);
owner[people] = B;
people++;
A.showOwners();
A.pushListPet("momo");
A.showOwners();
cout << "owner[0].getOwnerName: " << owner[0].getOwnerName() << endl;
cout << "owner[0].getNumPetsCheckin: " << owner[0].getNumPetsCheckin() << endl;
cout << "A.getNumPetsCheckin: " << A.getNumPetsCheckin() << endl;
}
Owners { name: A, balance: 10, numPetsCheckin: 0}
Owners { name: A, balance: 10, numPetsCheckin: 1}
owner[0].getOwnerName: A
owner[0].getNumPetsCheckin: 0
A.getNumPetsCheckin: 1
问:为什么owner[0].getNumPetsCheckin()
和A.getNumPetsCheckin()
不同?
我在A.pushListPet("momo"){(numPetsCheckin++;)}
中设置了 A 的 numPetsCheckin 加 1,但owner[0].getNumPetsCheckin()
仍然为 0...
A
和owner[0]
不同?
问:怎么解决???????????????
正如人们在评论中解释的那样,代表 A 和 owner[0] 的字节是不同的。 可以存储指向同一个 memory 的指针,但其目的通常只是存储在一些连续的 memory 中并访问其中的特定点。
考虑将 A 和 B 作为引用(在某些情况下编译器可能将其实现为指针),而不是创建一个指针数组。
Owner& A = owner[people];
A = Owner("A",10);
people++;
...
问:为什么 owner[0].getNumPetsCheckin() 和 A.getNumPetsCheckin() 不同?
它们是不同的对象,在状态owner[people] = A;
, object A
被复制到owner[people]
。 您可以通过打印地址来验证它:
std::cout << &owner[people] << ' ' << &A << std::endl;
在我的机器上得到了 output,它们是不同的对象。 所以你改变 object A 不会影响owner[people]
0x7ffd10410f90 0x7ffd1040f530
问:怎么解决?
使用参考:
auto& Aref = owner[0];
Aref.showOwners();
Aref.pushListPet("momo");
Aref.showOwners();
或者直接修改object owner[0]
owner[0].showOwners();
owner[0].pushListPet("momo");
owner[0].showOwners();
如评论所述,您的代码没有很好地利用现代 c++ ,我只是用c++17
对其进行了改进以使其更清晰:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Owner {
protected:
// default value
std::string ownerName = "No name";
int balance = 0;
// use vector instead of fixed sized array
std::vector<std::string> pet;
public:
// the default construct is enough
Owner() = default;
Owner(const string &ownerName, int balance);
string getOwnerName() { return ownerName; }
int getBalance() { return balance; }
// just return the size of vector
int getNumPetsCheckin() { return pet.size(); }
// move the temporary argument
void pushListPet(string a) { pet.push_back(std::move(a)); }
void showOwners() {
cout << "Owners { name: " << ownerName << ", balance: " << balance
<< ", numPetsCheckin: " << getNumPetsCheckin() << "}\n";
}
};
Owner::Owner(const string &theName, int theBalance)
: ownerName(theName), balance(theBalance) {}
int main() {
// use vector instead of fixed sized array
std::vector<Owner> owner;
// emplace back will construct a new object at the end of vector, and return
// the reference
auto &a = owner.emplace_back("A", 10);
a.showOwners();
a.pushListPet("momo");
a.showOwners();
// construct a new object at the end of vector, again
owner.emplace_back("B", 20);
cout << "owner[0].getOwnerName: " << owner[0].getOwnerName() << endl;
cout << "owner[0].getNumPetsCheckin: " << owner[0].getNumPetsCheckin()
<< endl;
// The reference object a is invalidated, since the vector have growed size,
// now we can only reference it with only `owner[0]` or owner.front()
cout << "A.getNumPetsCheckin: " << owner.front().getNumPetsCheckin() << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.