C++ 如何模拟在c++;?

C++ 如何模拟在c++;?,c++,inheritance,overriding,C++,Inheritance,Overriding,从阅读这个答案()可以明显看出,不能从子类重写父函数 然而,我需要这样的功能。这是一个非功能设置,描述了我正在尝试做的事情 class Parent { public: Graph createGraph() { return new Graph([this](float x){ return this->getY(x); }); } float getY(float x) { return sin(x); } } 我的设置

从阅读这个答案()可以明显看出,不能从子类重写父函数

然而,我需要这样的功能。这是一个非功能设置,描述了我正在尝试做的事情

class Parent {
public:
  Graph createGraph() {
    return new Graph([this](float x){ 
        return this->getY(x); 
    });
  }

  float getY(float x) {
    return sin(x);
  }
}

我的设置中重要的一点是,我有一个父类,它有一个函数始终引用一个经常被子类重载的函数。来自java/javascript的土地,我的方法是做你上面看到的,但是我觉得C++的错误是这样的。 我如何模拟(即获得相对类似的功能)这种形式的覆盖

我知道,如果不干燥,我可以将/paset
createGraph
复制到这两个文件中,并且它会起作用。如果这是一个有经验的C++开发人员的做法,那对我来说就足够了。但现在,我正在寻找一种方法来解决这个问题,这种方法和我更像java的方法一样枯燥


编辑:这里的核心问题似乎是我误解了
virtual
的作用,假设这意味着父类中没有函数的定义(类似于其他语言中的抽象函数)。事实并非如此,virtual似乎做了一些其他的事情,允许抽象类,但不需要抽象类。

在父类中,您应该将要重写的函数设置为virtual,例如:

 class Parent{
 virtual float getY(float x) {
    return sin(x);
  }}

在父类中,应将要重写的函数设置为虚拟函数,例如:

 class Parent{
 virtual float getY(float x) {
    return sin(x);
  }}
使用CRTP模式

void sink(int);
样板
结构父级{
void doStuff(){
下沉(
静态_cast(this)->getY()
);
}
int getY(){
返回42;
}
};
结构子级:父级{
int getY(){
返回43;
}
};
结构Child2:父级{
//不希望自定义,默认值为罚款。
};
void foo(){
儿童c;
c、 多斯塔夫();#通过43
儿童2 c2;
c2.doStuff();#通过42
}
使用CRTP模式

void sink(int);
样板
结构父级{
void doStuff(){
下沉(
静态_cast(this)->getY()
);
}
int getY(){
返回42;
}
};
结构子级:父级{
int getY(){
返回43;
}
};
结构Child2:父级{
//不希望自定义,默认值为罚款。
};
void foo(){
儿童c;
c、 多斯塔夫();#通过43
儿童2 c2;
c2.doStuff();#通过42
}

<>代码> < p>不确定你在寻找的链接在你所提供的链接中没有提供,但是这就是你在C++中实现重写的方法。
class Parent
{
public:
    virtual ~Parent() = default;

    Graph createGraph(float x)
    {
        return Graph(getY(x));
    }

    virtual float getY(float x) const
    {
        return sin(x);
    }
};

class Child: public Parent
{
public:
    float getY(float x) const override
    {
        return x * x;
    }
};

int main()
{
    // Play around here with creating a Parent instead of a Child
    //    and/or taking away the virtual keyword to learn how this works
    std::unique_ptr<Parent> thingy = std::make_unique<Child>();
    thingy->createGraph(1.0f);
}
类父类
{
公众:
virtual~Parent()=默认值;
Graph createGraph(浮点x)
{
返回图(getY(x));
}
虚拟浮点getY(浮点x)常量
{
返回sin(x);
}
};
类子:公共父类
{
公众:
浮点getY(浮点x)常量覆盖
{
返回x*x;
}
};
int main()
{
//在这里玩创建父对象而不是子对象的游戏
//和/或删除虚拟关键字以了解其工作原理
std::unique_ptr thingy=std::make_unique();
thingy->createGraph(1.0f);
}

<>代码> < p>不确定你在寻找的链接在你所提供的链接中没有提供,但是这就是你在C++中实现重写的方法。
class Parent
{
public:
    virtual ~Parent() = default;

    Graph createGraph(float x)
    {
        return Graph(getY(x));
    }

    virtual float getY(float x) const
    {
        return sin(x);
    }
};

class Child: public Parent
{
public:
    float getY(float x) const override
    {
        return x * x;
    }
};

int main()
{
    // Play around here with creating a Parent instead of a Child
    //    and/or taking away the virtual keyword to learn how this works
    std::unique_ptr<Parent> thingy = std::make_unique<Child>();
    thingy->createGraph(1.0f);
}
类父类
{
公众:
virtual~Parent()=默认值;
Graph createGraph(浮点x)
{
返回图(getY(x));
}
虚拟浮点getY(浮点x)常量
{
返回sin(x);
}
};
类子:公共父类
{
公众:
浮点getY(浮点x)常量覆盖
{
返回x*x;
}
};
int main()
{
//在这里玩创建父对象而不是子对象的游戏
//和/或删除虚拟关键字以了解其工作原理
std::unique_ptr thingy=std::make_unique();
thingy->createGraph(1.0f);
}

如何工作函数调用:

struct Parent{
  virtual void foo(){} // Func(1)
  void call(){
    this->foo();         // (A) call final overrider
    this->Parent::foo(); // (B) call Parent::foo
    }
  };

struct Derived:Parent{
  void foo() override {} // Func(2)
  };

void test(){
  Parent parent; //final overrider of foo is Func(1)
  Derived derived1; //final overrider of foo is Func(2)
  Parent& derived2 = derived; //final overrider of foo is Func(2).

  parent.call()//  At (A) call => Func(1)
               //  At (B) call => Func(1)

  derived1.call()  // At (A) call => Func(2)
                   // At (B) call => Func(1)

  derived2.call()  // At (A) call => Func(2)
                   // At (B) call => Func(1)

如何工作函数调用:

struct Parent{
  virtual void foo(){} // Func(1)
  void call(){
    this->foo();         // (A) call final overrider
    this->Parent::foo(); // (B) call Parent::foo
    }
  };

struct Derived:Parent{
  void foo() override {} // Func(2)
  };

void test(){
  Parent parent; //final overrider of foo is Func(1)
  Derived derived1; //final overrider of foo is Func(2)
  Parent& derived2 = derived; //final overrider of foo is Func(2).

  parent.call()//  At (A) call => Func(1)
               //  At (B) call => Func(1)

  derived1.call()  // At (A) call => Func(2)
                   // At (B) call => Func(1)

  derived2.call()  // At (A) call => Func(2)
                   // At (B) call => Func(1)

你在寻找什么不在你提供的链接中?您可以选择使用虚拟和覆盖,或不使用和隐藏。如果这是你所要求的,那么就没有第三种选择。你对虚拟这个关键词有什么意见吗?此外,您不需要键入“this”,还有第三个选项,您可能不熟悉其他语言。它在功能上是有用的,但与我到目前为止展示的语法不同。关于使用“this”,我只想说得具体一点。我知道它不会改变什么,但它让我更容易阅读。谢谢你的意见。你能解释一下让
getY
virtual以什么方式不能满足你的需要吗?在你提到的“其他语言”中,这个“第三选项”叫什么?你能链接它,所以我知道你在追求什么?<代码>类似于C++中其他语言的抽象函数< /Cuff> >,真正的抽象函数被称为“纯虚函数”,规则的虚拟函数可以有默认实现。您可以选择使用虚拟和覆盖,或不使用和隐藏。如果这是你所要求的,那么就没有第三种选择。你对虚拟这个关键词有什么意见吗?此外,您不需要键入“this”,还有第三个选项,您可能不熟悉其他语言。它在功能上是有用的,但与我到目前为止展示的语法不同。关于使用“this”,我只想说得具体一点。我知道它不会改变什么,但它让我更容易阅读。谢谢你的意见。你能解释一下让
getY
virtual以什么方式不能满足你的需要吗?在你提到的“其他语言”中,这个“第三选项”叫什么?你能链接它吗,我知道你在追求什么?<代码>类似于C++中其他语言的抽象函数< /Cuff> >,真正的抽象函数被称为“纯虚函数”,规则的虚函数可以默认实现。