相关疑难解决方法(0)

emplace_back和继承

我想知道你是否可以使用emplace_back将项目存储到一个向量中,这是一种从向量所期望的类派生的类型.

例如:

struct fruit
{
    std::string name;
    std::string color;
};

struct apple : fruit
{
    apple() : fruit("Apple", "Red") { }
};
Run Code Online (Sandbox Code Playgroud)

别的地方:

std::vector<fruit> fruits;
Run Code Online (Sandbox Code Playgroud)

我想在向量中存储apple类型的对象.这可能吗?

c++ inheritance vector c++11

4
推荐指数
1
解决办法
1696
查看次数

标签 统计

c++ ×1

c++11 ×1

inheritance ×1

vector ×1