在非纯虚函数中使用纯虚函数的 C++ 抽象类
c++ abstract classes using pure virtual functions in non pure virtual functions
我想创建一个抽象类,该类具有由构造函数调用的纯虚函数,该函数不是纯虚拟的。 以下是我的文件class.hpp
:
#ifndef __CLASS_HPP__
#define __CLASS_HPP__
#include <iostream>
class Parent {
public:
Parent(){
helloWorld(); // forced to say hello when constructor called
};
virtual void helloWorld() = 0; // no standard hello...
};
class Child : public Parent {
public:
void helloWorld(){ // childs implementation of helloWorld
std::cout << "Hello, World!n";
};
};
#endif
在这个例子中,我有一个父类,它有一个纯虚函数helloWorld()
。 我希望每个派生类在调用构造函数时都说"你好";因此,为什么helloWorld()
在父类构造函数中。 但是,我希望每个派生类都被迫选择如何说"hello",而不是使用默认方法。 这可能吗? 如果我尝试使用 g++ 编译它,我会收到构造函数正在调用纯虚函数的错误。 我main.cpp
是:
#include "class.hpp"
int main(){
Child c;
return 0;
}
我正在使用g++ main.cpp -o main.out
进行编译,结果错误是:
In file included from main.cpp:1:0:
class.hpp: In constructor ‘Parent::Parent()’:
class.hpp:9:16: warning: pure virtual ‘virtual void Parent::helloWorld()’ called from constructor [enabled by default]
关于如何以合法方式获得类似设置的任何建议?
新问题
DyP 让我注意到构造函数不使用任何重写的函数,所以我希望能够做的事情以我设置它的方式是不可能的。 但是,我仍然想强制任何派生构造函数调用函数helloWorld()
,有什么办法可以做到这一点吗?
你正在做的事情是非法的。
为了在C++中定义抽象类,您的类必须至少有一个纯虚函数。在您的情况下
virtual void helloWorld() = 0;
在这种情况下,你是对的。
但是你的纯虚函数没有任何实现,因为它是一个纯虚函数。所以从同类的康斯坦鲁克托调用纯虚函数是非法的(在类级纯虚函数没有任何实现)
所以
Parent(){
helloWorld(); // forced to say hello when constructor called
};
这是非法的。
如果需要,可以在派生类中实现纯虚函数,然后从派生类的构造函数调用helloWorld()
为什么不简单地将其添加到每个子类的构造函数中?
如果你想避免每次在构造函数中写入它(甚至跳过或继承它),那么你可以使用 CRTP:
class Parent {
public:
Parent(){};
virtual void helloWorld() = 0; // no standard hello...
};
template <typename Par>
class ParentCRTP: public Parent {
public:
ParentCRTP(){
Par::doHelloWorld();
};
virtual void helloWorld(){
Par::doHelloWorld();
}
};
class Child : public ParentCRTP<Child> {
public:
static void doHelloWorld(){ // childs implementation of helloWorld
std::cout << "Hello, World!n";
};
};
此方法不会在子项的 hello 方法中为您提供指向子类的指针 - 此时类实例仅Parent
实例,无法获取有效的Child
指针。要在构造后强制执行Child
的方法,您只能使用两个阶段初始化:首先使用构造函数创建类实例,然后使用单独的方法对其进行初始化。
除此之外,像这样的问题可能是重新思考你的设计的暗示。你不应该强迫你的类以给定的方式初始化自身。
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗