- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这个问题已经回答了。但我只是想确认一下我的理解。
这是我的代码片段。来自this .
#include <iostream>
using namespace std;
class Base
{
void a() { cout << "a "; }
void c() { cout << "c "; }
void e() { cout << "e "; }
// 2. Steps requiring peculiar implementations are "placeholders" in base class
virtual void ph1() = 0;
virtual void ph2() = 0;
public:
// 1. Standardize the skeleton of an algorithm in a base class "template method"
virtual ~Base() = default;
void execute()
{
a();
ph1();
c();
ph2();
e();
}
};
class One: public Base
{
// 3. Derived classes implement placeholder methods
/*virtual*/ void ph1() { cout << "b "; }
/*virtual*/ void ph2() { cout << "d "; }
};
class Two: public Base
{
/*virtual*/ void ph1() { cout << "2 "; }
/*virtual*/ void ph2() { cout << "4 "; }
};
int main()
{
Base *array[] =
{
&One(), &Two()
};
for (int i = 0; i < 2; i++)
{
array[i]->execute();
cout << '\n';
}
}
当我编译时,它给出了错误作为标题:
error: taking address of temporary [-fpermissive]
&One(), &Two()
error: taking address of temporary [-fpermissive]
&One(), &Two()
所以,我尝试在互联网上查找。正如他们所说:
&A() is creating a temporary object which gets destructed on exit of the full expression automagically...
当我更改错误行
&One(), &Two()
到
new One(), new Two()
然后,它起作用了。
但是,我如何使原始代码像作者写的那样工作?我应该使用 delete
吗
delete array[i];
最佳答案
借助现代 C++ 功能(11 及更高版本),您可以使用 std::vector<std::unique_ptr<Base>>
处理此类多态数组. vector
允许自动销毁和扩展,以及unique_ptr
将在其自身销毁时销毁对象:
std::vector<std::unique_ptr<Base>> array;
array.emplace_back(new One());
array.emplace_back(new Two());
for(auto &p : array)
p->execute();
// no explicit cleanup is required here
您可以选择其他智能指针类作为 vector 的元素,甚至可以使用std::array
。作为固定大小的容器,但所有方法的总体思路都是相同的:
Try not to handle memory management manually, use STL primitives for such low-level actions.
关于c++ - 获取临时地址 [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41278316/
我不断得到: ../src/stack.cpp: In function ‘int main()’: ../src/stack.cpp:34:28: error: invalid conversion
#include #include #include #include 我需要有关此代码的帮助。我的编译器一直要求我使用 -fpermissive 选项,但我不知道在哪里输入它。我粘贴了下面的
我正在尝试使用 pthreads 将 Leibniz 的总和并行化以近似 PI。当我使用最新版本的 g++ 运行此代码时,会出现这两个错误,我真的不明白为什么,我正在这样编译:g++ pi2.cpp
我有一个 C 单头库,我想在我的 C++ 项目中使用它。通常,我只需包含该文件就可以了,因为 C++ 几乎是 C 的超集。但是,该库有一个跳过初始化的 goto,这违反了C++ 标准。 我可以通过在
#include #include using namespace std; typedef bool (*compare)(int,int); void SelectionSort(int *i
我正在尝试构建一个非循环树结构,每个节点都由一个字符串标识,每个分支节点都是 PrimMap 类型。同一代的所有节点都保存为 map 中的 Item 对象。这是缩写代码: class PrimMap
我知道这个问题已经回答了。但我只是想确认一下我的理解。 这是我的代码片段。来自this . #include using namespace std; class Base { void a
我写了下面的代码示例,我不得不使用 -fpermissive 来跳过错误/警告 #include #include using namespace std; int endOfProgram(){
我只是想知道 -fpermissive 标志在 g++ 编译器中的作用是什么?我得到: error: taking address of temporary [-fpermissive] 我可以通过将
我定义了两个不同的typedef结构:datatype_2d_1和datatype_2d_2,它们基本上都有两个 double 作为成员,但它们的定义不同。现在我有一个变量数据类型 z1 和一个指针
我正在使用 NDK (Android) 在 C++ 中构建一个库。输出告诉我一些代码被标记为错误,但可以通过使用 -fpermissive 标志来抑制。至少在我看来是这样。输出是: MyClass.c
我正在尝试构建一个带有静态数据成员的模板类,但当我尝试编译以下代码时收到此错误消息: |In instantiation of ‘T& T::t’:| 16|required from here| 1
我是 c++ 的新手,在 ubuntu 上使用 eclipse cdt 并在我的头文件中收到此错误: initializing argument 1 of 'std::map,Supplier*> E
我的想法是检查这个 dll 是否有我需要的版本,或者它是旧的还是新的?我正在尝试使用 THUNK/Trampoline 函数。 这是我在另一个 *.dll 文件中的用法。 int FilterVers
为什么编译器会在指示的行报错? class C { std::string s; public: C() { s = "";} ~C() {} void Set(con
我有一个类Cache,它的函数 write 指定为 bool write(const MemoryAccess &memory_access, CacheLine &cl); 我这样调用这个函数。 c
我在这个控制台应用程序中使用 Qt 4.5.1,当我想在 if 语句 行接收用户输入时,它收到此错误: iso c++ forbids comparison between pointer and i
我有一个数组,其大小在运行时确定。 cout>size1; int* scores=new int[size1]; 然后我填写这个数组分数。现在我想增加它的大小。我创建了一个新的动态数组。 int*
代码 int cycle_length(int i, int j) { int cycleLength = 0; for (int k = i; k cycle_length) {
我有以下代码: int Array::getSize(){ //do something } Movie Array::getMovie(int i){ //do something }
我是一名优秀的程序员,十分优秀!