【问题标题】:Can a pure virtual function be called by an accessor function?访问器函数可以调用纯虚函数吗?
【发布时间】:2019-12-14 13:40:45
【问题描述】:

如果我的纯虚函数是 i.e.

virtual string func()=0;

它可以返回到访问器函数吗?

编辑:对于之前的混淆,我深表歉意,所以我添加了一个示例代码

class Parent{
  public:
   parent(void){};
   virtual string func()=0;
   string getFunc (void) const{return var=func();} ;
  protected: 
   string var;
}
class Child: public  Parent{

public:
 class Child(void); 

string func(){
 string random // this is very generic I actually pretend 
               //to do some math here and return a string
 return random}; 

我的意图是然后使用实例化的子对象并要求访问器(getFunc())返回一个值,我只能基于 func()计算。但是错误指出虚函数的性质不允许返回,坦率地说我觉得很奇怪,因为它确实有返回标签。

【问题讨论】:

  • 对不起,“存取函数”是什么意思?
  • 你的意思是像string getFrobaz() { return func(); }
  • 你尝试的时候发生了什么?
  • @user207421 不,但这意味着问题可能应该解释或澄清它是什么。
  • @bolov 解释访问函数?真的吗?这个词无处不在。访问器和修改器(当然,在这种情况下不是)。

标签: c++ virtual


【解决方案1】:

在另一个成员函数中调用纯virtual 函数根本不成问题。 (不幸的是,OP 没有复制/粘贴确切的错误消息。)

不幸的是,公开的 OP 样本存在大量拼写错误、语法错误和其他弱点。

解决这个问题后,我发现了一个基本问题:Parent::getFunc()const-ness。

Parent::getFunc() 调用一个非常量成员函数 改变编译器不接受的成员变量this->var

删除const,它起作用了。

固定样本:

#include <iostream>
#include <string>

class Parent{
  public:
    Parent() = default;

    virtual std::string func() = 0; // PURE VIRTUAL

    std::string getFunc() // WRONG: const
    {
      return var = func();
    }

  protected: 
    std::string var;
};

class Child: public Parent {

  public:
    Child() = default; 

    virtual std::string func() override
    {
      std::string random; // this is very generic I actually pretend 
               //to do some math here and return a string
      return random;
    }
};

#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__ 

int main()
{
  DEBUG(Child child);
  DEBUG(child.getFunc());
}

输出:

Child child;
child.getFunc();

Live Demo on coliru


虽然,const 访问器(我称之为“getter”)似乎是合理的。再设计一点点,也可以实现:

#include <iostream>
#include <string>

class Parent{
  public:
    Parent() = default;

    virtual std::string func() const = 0; // PURE VIRTUAL

    std::string getFunc() const
    {
      return var = func();
    }

  protected: 
    mutable std::string var;
};

class Child: public Parent {

  public:
    Child() = default; 

    virtual std::string func() const override
    {
      std::string random; // this is very generic I actually pretend 
               //to do some math here and return a string
      return random;
    }
};

#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__ 

int main()
{
  DEBUG(Child child);
  DEBUG(child.getFunc());
}

输出:同上

Live Demo on coliru

注意:

  1. 我创建了 virtual func() const 以使其可用于 const 实例/成员函数。

  2. 成员变量变为mutable,使其在const成员函数中可写。

我必须承认,我个人觉得mutable 有点吓人。在我简单的想法中,有些东西是const,或者不是。然而,mutable 似乎只是为了更新实际const 对象的内部缓存而发明的。因此,它可能是这里的正确工具。 (如果没有更多上下文,这有点难以说。)

更多关于mutablemutable specifier

【讨论】:

  • 如果实际对象是派生类(如您的示例中所示),则它只能 不是 成为问题,这不是 OP 所要求的。
  • @user207421 抱歉,您能详细说明一下吗?
  • @user207421 在 OPs 示例中,class Child 派生自 class Parent。否则,virtual 方法没有多大意义。我监督了什么?
猜你喜欢
  • 2018-08-05
  • 2016-02-05
  • 2017-01-22
  • 1970-01-01
  • 2021-06-21
  • 2011-08-06
  • 2013-01-19
  • 1970-01-01
相关资源
最近更新 更多