繁体   English   中英

非纯虚函数中使用纯虚函数的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()的父类。 我希望每个派生类在调用构造函数时都说“ hello”。 因此,为什么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方法,只能使用两个阶段的初始化:首先,使用构造函数创建类实例,然后使用单独的方法对其进行初始化。

除此之外,类似的问题可能是重新思考设计的提示。 您不应该强迫您的类以给定的方式初始化自身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM