假设我存储了一个对象向量.该对象有一个属性,如int age,它是私有的,我们需要通过getAge()访问它.
让我们说我们把对象称为"人"
我将对象"people"存储在一个向量中... soo ...
vector<people> vectorOfPeople
Run Code Online (Sandbox Code Playgroud)
我的问题是......是否有可能通过矢量vectorOfPeople直接访问'age'?也许是类似的vectorOfPeople[0].getAge()东西......(我知道这不起作用).
谢谢!
做你说的应该工作:
// Make a vector of people
vector<people> pVec;
// Stick a people (person?) in it
pVec.push_back(people(someAge, someName)); // Or whatever constructor you have
// Show the age
cout << pVec[0].getAge(); // This should print out the age assigned in the c'tor
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您选择的名称有点不直观,但否则您应该没有问题.
| 归档时间: |
|
| 查看次数: |
8618 次 |
| 最近记录: |