简体   繁体   English

在VS2013中继承构造函数

[英]Inheriting constructor in VS2013

VS2013 doesn't seem to have support for C++11's constructor inheriting, as stated here: http://msdn.microsoft.com/en-us/library/hh567368.aspx VS2013似乎不支持C ++ 11的构造函数继承,如下所示: http : //msdn.microsoft.com/zh-cn/library/hh567368.aspx

So the following isn't possible for me: 所以以下对我来说是不可能的:

class Animal {
public:
    int age;
    explicit Animal(int _age) {
        this->age = _age;
    }
};

class Dog : public Animal {
public:
    using Animal::Animal;
};

int main() {
    Dog d = Dog(5);
}

Is there any workaround in VS2013 without hardcoding Dog's constructor to be the same as Animal? VS2013中是否有任何变通办法,而没有将Dog的构造函数硬编码为Animal?

Closest you could do is just perfect-forward the constructor; 您所能做的就是完善构造函数。

template <typename... Args>
Dog(Args&&... args)
: Animal(std::forward<Args>(args)...)
{ }

It's still not exactly the same as using Animal::Animal for some cases, but it'll at least get you all the Animal constructors and protect you from changes there from forcing you to rewrite Dog 's constructor every time. 在某些情况下,它仍然与using Animal::Animal并不完全相同,但是它至少会为您提供所有Animal构造函数,并保护您免受在那里的更改,而不必每次强制您重写Dog的构造函数。

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

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