MSVC C ++ 11:非标准构造函数继承 [英] MSVC C++11: non-standard constructor inheritance

查看:237
本文介绍了MSVC C ++ 11:非标准构造函数继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows Native C ++库中,我有时在构造函数方面超过它,而没有提供具有相同功能的实际方法(5-10个额外的构造函数)。这使得基本扩展C ++类非常困难因为没有正确的构造函数继承(没有重新声明它们和转发调用)

In my Windows Native C++ library I've sometimes overdone it in terms of constructors without providing actual methods with same functionality (5-10 extra constructors). This makes basic extending a C++ class really hard as there's no proper constructor inheritance (without redeclaring them and forwarding calls).

我使用MSVC。 使用继承构造函数的方法是有办法的吗 因为,如果有人决定扩展一个类,我滥用构造函数(无需添加新属性和单继承),这是一场噩梦。

I use MSVC. (Yes, smile all you want!) Question is: Is there a way to inherit constructors except the default constructor/copy-constructor using using? Because, if someone decided to extend a class where I abuse constructors (w/o adding new properties and with single inheritance), it's a nightmare.

(示例代码)

class parent {
public:
    parent(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
    // a non-standard constructor
    parent(const std::nullptr_t&):parent(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
};

// adds nothing to parent but helper methods
// and maybe self-reliant properties with their own constructors
// so no constructor is required to initialize child class properties
class child: public parent {
public:
     // using parent::parent; // of no real use
    child(){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
    // need to forward call
    child(const std::nullptr_t&):parent(nullptr){
        std::cout << __FUNCTION__ << ':' << __LINE__ << std::endl;
    }
};

这种多重构造方法继承的噩梦,特别是当我构建一个基本的CRTP类并需要扩展建立我的默认类,然后允许其他人扩展并构建自己的变体,同时依次保持可链接的方法功能(父对子)

This multiple-constructor inheritance nightmare occurs especially when I build a my base CRTP class and need to extend it to build my default class and then allow others to extend and build their own variants while maintaining chainable method functionality (parent to child) in order.

在MSVC中,使用parent :: parent 不会继承 parent(const std :: nullptr_t&)是否应该根据 C ++ 11 标准?我应该在 VC13 中预期这样的功能吗?这将极大地影响我对此公共重写的风格选择。

In MSVC anyways, using parent::parent does not inherit the parent(const std::nullptr_t&). Is it supposed to according to the C++11 standard or not? Should I expect such functionality in VC13? This will greatly impact my choice in style for this public rewrite.

我也想知道为什么, C ++ 11 ,他们没有想出当我扩展一个类,添加新属性,它应该继承父级的默认行为(构造函数,赋值运算符等)

I also wonder why, for C++11, they did not figure out that when I'm extending a single class and not adding new properties, it should inherit the parent's default behavior (constructors, assignment operators, etc.). It just makes sense... and the compiler could figure it out.

推荐答案

根据VS2013的路线图从 Herb Sutter的谈话在// build ,继承的构造函数不是来到VC ++在附近未来:

According to the VS2013 Roadmap from Herb Sutter's talk at //build, inheriting constructors are not coming to VC++ in the near future:

他们是计划。

这篇关于MSVC C ++ 11:非标准构造函数继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆