检索和编辑向量中对象的私有成员
我有一些代码将一些对象添加到向量中。然后,我希望从向量中检索特定对象,并能够写出和编辑其私有成员变量。
这是我当前拥有的代码:
class Product {
public:
Product(int n, const string& na, int p)
: number(n), name(na), price(p) {};
void info() const;
private:
string name;
int number, price;
};
成员函数如下所示:
void Product::info() const {
cout << number << ". " << name << " " price << endl;
}
然后我创建一个向量并向其中推入一些对象,如下所示:
vector<Product> range;
range.push_back(Product(1, "Bagpipe", 25));
要检索并列出有关所有对象的信息,我有以下函数:
void listProducts (const vector<Product>& range1) {
for_each (range1.begin(), range1.end(), mem_fun_ref(&Product::info));
}
但这就是我被卡住了。
归结为我的问题:我不知道如何从向量中检索单个对象并编辑它们。我需要能够在向量中搜索包含特定数字或名称的对象,并且能够检索有关其所有私有成员的信息,并且还能够编辑所有成员。
到目前为止我对解决方案的想法是:
创建额外的成员函数 可以返回单个成员
创建函数,该函数与我上面已有的函数类似,可以搜索向量中的每个对象,并使用这些附加成员函数的返回值与我正在寻找的内容进行比较
我不太清楚知道我将如何编辑对象的私有成员,但我目前的猜测是我也需要为此的成员函数,以及与这些函数相关的函数
任何帮助将不胜感激!甚至模糊地推动正确的方向!
I have some code which adds a few objects to a vector. I then wish to retrieve a specific object from the vector and be able to both write out and edit its private member variables.
This is the code I currently have:
class Product {
public:
Product(int n, const string& na, int p)
: number(n), name(na), price(p) {};
void info() const;
private:
string name;
int number, price;
};
The member function looks like this:
void Product::info() const {
cout << number << ". " << name << " " price << endl;
}
I then create a vector and push into it some objects, like so:
vector<Product> range;
range.push_back(Product(1, "Bagpipe", 25));
To retrieve and list the information about all objects, I have the following function:
void listProducts (const vector<Product>& range1) {
for_each (range1.begin(), range1.end(), mem_fun_ref(&Product::info));
}
But this is where I get stuck.
To boil my problem down: I have no idea how to retrieve individual objects from the vector and edit them. I need to be able to search my vector for objects containing a specific number or name, and be able to retrieve either the information about all its private members, and also be able to edit all members.
Ideas I have about solutions thusfar are:
to create additional member functions
that can return individual membersto create functions that, similar to the function I already have above, can search through each object in the vector and use the return from these additional member functions to compare with what I'm looking for
I don't quite know how I would go about editing an objects private members, but my current guess is that I would need members functions for this as well, and functions that tie in to those
Any help would be greatly appreciated! Even vague nudges in the right direction!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果成员变量是私有的,那么根据定义,您无法从外部世界访问它们!您需要执行以下操作之一:
public
。int MyClass::getFoo() const
和void MyClass::setFoo(int)
)。友元
。这与存储在
向量
中无关。If the member variables are private, then by definition, you cannot access them from the outside world! You will need to do one of the following:
public
.int MyClass::getFoo() const
andvoid MyClass::setFoo(int)
).friend
of the class.This is nothing to do with being stored in a
vector
.您可以使用
std:find_if
根据您希望的任何条件在向量中查找项目。然后,您可以向
Product
添加公共接口,以允许您根据需要更新其状态。请注意,此接口不必是直接“将此项目设置为此值”映射。You can use
std:find_if
to find items in a vector based on any criteria you wish.Then you can add a public interface to
Product
to allow you to update its state as needed. Note that this interface need not be a direct "set this item to this value" mapping.Boost.MultiIndex,每个可搜索的索引都有一个字段可能比使用带有您自己的搜索代码的
向量
更容易。我还在其他答案中回应了有关私人成员访问的评论。
Boost.MultiIndex with an index for each searchable field might be easier than using a
vector
with your own search code.I also echo comments about private member access in other answers.
您需要为 Product 类提供允许更改 Product 属性的成员函数,或者更好地将向量中的 Product 对象替换为编辑现有对象的结果,但要做到这一点,您需要提供访问器函数,或者合适的Product 类中的构造函数。
You need to either provide member functions for your Product class that allow the Product properties to be changed OR BETTER replace the Product object in the vector with the results of editing an existing one, but to do that you need to provide accessor functions, or suitable constructors in your Product class.