简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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