簡體   English   中英

如何更改向量中的 class 成員值

[英]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...

Aowner[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM