Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将接口对象传递给方法?_C++_Dictionary_Methods_Interface - Fatal编程技术网

C++ 如何将接口对象传递给方法?

C++ 如何将接口对象传递给方法?,c++,dictionary,methods,interface,C++,Dictionary,Methods,Interface,我需要将一个对象(定义为接口)传递给一个方法,但我不知道该怎么做。 该方法必须将此对象添加到地图。显然,传递的对象将是接口IFigure的子对象 void Drawing::addFigure(string id, IFigure figure) { drawings.insert(id, figure); } 地图的定义如下(在课堂绘图中): 地图制图; 谢谢你的时间 不能创建抽象类的实例 因此宣布: IFigure figure; 这将构造一个新的IFigure。你不能那样做

我需要将一个对象(定义为接口)传递给一个方法,但我不知道该怎么做。 该方法必须将此对象添加到地图。显然,传递的对象将是接口IFigure的子对象

void Drawing::addFigure(string id, IFigure figure) {
    drawings.insert(id, figure);
}

地图的定义如下(在课堂绘图中):

地图制图;

谢谢你的时间

不能创建抽象类的实例

因此宣布:

IFigure figure;
这将构造一个新的
IFigure
。你不能那样做

IFigure figure = Implementation{};
这也不行。通过复制切片的
实现
,您正在创建一个新的
IFigure
。你也不能那样做

变量是值,就像
int
一样
IFigure图
表示类型为
IFigure
的值,而不是对实现的引用。它永远不会工作

切片有点像这样问:

int i = double{1.6};
std::cout << i;
// Why does it prints '1'?? I assigned '1.6', it should refer to it!
std::map<string,std::unique_ptr<IFigure>> figures;
figures.emplace("id1", std::make_unique<Implementation>());
// or
figures["id2"] = std::make_unique<Implementation>();

// or even
void Drawing::addFigure(std::string id, std::unique_ptr<IFigure> figure) {
    // move ownership from 'figure' into 'drawings' so
    // 'drawing' becomes the owner and 'figure' becomes null.
    drawings.emplace(id, std:move(figure));
}
当使用像
std::map
这样的容器时,还可以使用如下指针:

int i = double{1.6};
std::cout << i;
// Why does it prints '1'?? I assigned '1.6', it should refer to it!
std::map<string,std::unique_ptr<IFigure>> figures;
figures.emplace("id1", std::make_unique<Implementation>());
// or
figures["id2"] = std::make_unique<Implementation>();

// or even
void Drawing::addFigure(std::string id, std::unique_ptr<IFigure> figure) {
    // move ownership from 'figure' into 'drawings' so
    // 'drawing' becomes the owner and 'figure' becomes null.
    drawings.emplace(id, std:move(figure));
}
使用指针:

      +------------------+
      |  Implementation  |
      +------------------+
            ^
            |
+--------+--|---+
| string | ptr  |
+--------+------+
然后像这样使用它:

int i = double{1.6};
std::cout << i;
// Why does it prints '1'?? I assigned '1.6', it should refer to it!
std::map<string,std::unique_ptr<IFigure>> figures;
figures.emplace("id1", std::make_unique<Implementation>());
// or
figures["id2"] = std::make_unique<Implementation>();

// or even
void Drawing::addFigure(std::string id, std::unique_ptr<IFigure> figure) {
    // move ownership from 'figure' into 'drawings' so
    // 'drawing' becomes the owner and 'figure' becomes null.
    drawings.emplace(id, std:move(figure));
}
figures.emplace(“id1”,std::make_unique());
//或
图[“id2”]=std::make_unique();
//甚至
无效图形::添加图形(标准::字符串id,标准::唯一\u ptr图形){
//将所有权从“图”移到“图纸”,以便
//“drawing”成为所有者,“figure”变为空。
图纸.安放位置(id,std:move(图));
}
为什么不
std::map
?它看起来更简单


如果您希望容器成为所有数据的所有者(当容器死亡时,所有元素都会被销毁),那么指针将成为所有者。使用原始指针会带来很多问题,初学者很难处理这些问题。如果您使用
new
,可能有错误,或者您有非常特殊的需求。

您的代码发生了什么错误?查找“对象切片”@dededecos它将要切片。您可能需要一张
地图图纸或<代码>地图图纸或<代码>地图图纸(或
地图图形;
无效图形::添加图形(字符串id,IFigure*图形)