在 C++ 中的派生类中为基类方法(构造函数除外)添加别名的简单方法

发布于 2025-01-18 15:50:10 字数 1185 浏览 3 评论 0原文

我想用C ++中的派生方法来吸用基类的方法 - 即仅更改名称,而不是签名。

该方法的名称在其原始范围(在基类中)中已经足够清晰,但是在派生类中变得模棱两可或误导。因此,明显的缓解是为基类中的方法创建一个别名。但是,该方法所采用的长参数列表是随着派生类的完善而变化的。重新测试。我想避免使用样板数据输入任务重新输入方法签名,并以更简单的方式使其更简单,就像我可以使用继承的专用构造函数的“使用”指令一样。

简单示例:(

struct Person {
    int ID;
    void virtual sit() {};
    void virtual walk() {};
    void virtual run(int speed, int duration, int stride) {}; //method name in unambiguous in this scope

    Person(int id) : ID(id) {}  //specialized c'tor
};

struct Politician : Person {
    void speak() {};
    void campaign() {}; //use this to 'run' for office
    //'sit' and 'walk' are clear enough, but 'run' is ambiguous in this scope, so alias it to 'sprint'
    void sprint(int speed, int duration, int stride) { 
        Person::run(speed, duration, stride);
    }   //the above works, but is a boat-load of boilerplate that I'd like to avoid

    using Person::Person;           //use Person c'tor (avoids boilerplate)
    //using sprint = Person::run;   //doesn't work - "'run' in 'struct Person' does not name a type"
};

还考虑一个虚拟类,它从多个基类继承,具有一个或多个相同命名的基类方法。这是我的真实应用程序,我将其归结为这个更简单的根需求示例。)

肯定有一种更简单的方法来为一种不变的继承方法创建别名,对吗?

I'd like to alias a method of a base class in a derived method in C++ -- i.e. change only the name, not the signature.

The method's name is clear enough in its original scope (in the base class), but becomes ambiguous or misleading in the derived class. Thus, the obvious mitigation is to create an alias for the method in the base class. However, the long list of parameters the method takes is in flux as the derived class is being refined & retested. I'd like to avoid the boilerplate data entry task of re-typing the method signature, and alias it in a simpler manner, much like I can do with the 'using' directive for an inherited specialized constructor.

Simple example:

struct Person {
    int ID;
    void virtual sit() {};
    void virtual walk() {};
    void virtual run(int speed, int duration, int stride) {}; //method name in unambiguous in this scope

    Person(int id) : ID(id) {}  //specialized c'tor
};

struct Politician : Person {
    void speak() {};
    void campaign() {}; //use this to 'run' for office
    //'sit' and 'walk' are clear enough, but 'run' is ambiguous in this scope, so alias it to 'sprint'
    void sprint(int speed, int duration, int stride) { 
        Person::run(speed, duration, stride);
    }   //the above works, but is a boat-load of boilerplate that I'd like to avoid

    using Person::Person;           //use Person c'tor (avoids boilerplate)
    //using sprint = Person::run;   //doesn't work - "'run' in 'struct Person' does not name a type"
};

(Also consider a virtual class that inherits from multiple base classes, with one or more identically-named base-class methods. Such is my real-world application that I boiled down into this simpler example of the root need.)

Surely there is an easier way to create an alias for an unchanged inherited method, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太傻旳人生 2025-01-25 15:50:10

您要求的答案

template <typename ...T>
decltype(auto) wrapper(T... t)
{
    return func_whos_sig_keeps_changing(t...);
}

您需要的答案

要么违反继承背后的主要校长,要么尚未考虑如何使用它。

从人那里继承的全部要点是,您的代码中的某个地方可以拥有:

std::unique_ptr<person> person_ptr;
...
...
person_ptr->run();

不关心它是什么类型的人。
如果您不能与政客这样做,那么它不是一个人,也不应该从人那里继承,至少不是公开的。

The answer you asked for

template <typename ...T>
decltype(auto) wrapper(T... t)
{
    return func_whos_sig_keeps_changing(t...);
}

The answer you need

You're either violating the primary principal behind inheritance, or have not yet thought through how you intend to use this.

The whole point of inhering from person is so that somewhere in your code you can have this:

std::unique_ptr<person> person_ptr;
...
...
person_ptr->run();

Without caring about what actual type of person it is.
If you can't do this with a politician, then its not a person and shouldn't inherit from person, at least not publicly.

仅此而已 2025-01-25 15:50:10

显然答案是:这不能通过简单(动态且优雅)的方式完成。它必须只是“硬编码”,就像我作为例子展示的那样,我想如何避免这样做。这是针对 C++ 标准提出的,但被拒绝了,因为这种需求“不太可能变得足够普遍以保证单独的语言功能”,并且“不太可能成为新手的日常工作”。

适合我的示例的重命名/别名继承方法的建议语法是:

void sprint() = Person::run;

感谢 SO 用户 Max Lybbert 和 Tomalla 在 Design & 中找到了它。 C++ 的演变第 12.8 节。

Evidently the answer is: It cannot be done the easy (dynamic & elegant) way. It has to just be 'hard-coded' the way I showed as the example of how I wanted to avoid having to do it. This was proposed for the C++ standard and rejected because the need was "unlikely to become common enough to warrant a separate language feature," and "not likely to become everyday work for novices."

The proposed syntax to rename/alias an inherited method, adapted to my example, was:

void sprint() = Person::run;

Credit to SO users Max Lybbert and Tomalla for finding it in Design & Evolution of C++ section 12.8.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文