Use keyword to invoke base class constructor

I have the following base class

class Grammateas { public: Grammateas(std::string name):_name(name){}; virtual ~Grammateas(){}; private: std::string _name; }; 

and the next derived class

 class Boithos final : public Grammateas { public: //using Grammateas::Grammateas; Boithos(int hours):Grammateas("das"),_hours(hours){}; virtual ~Boithos(){}; private: int _hours; }; 

I want to use the base class constructor to create an object like this

  Boithos Giorgakis(5); //works Boithos Giorgakis("something"); //Bug 

I read that I can use the using keyword, but when I try to use it as

  using Grammateas::Grammateas; 

The compiler returns a message

error: 'Grammateas :: Grammateas name constructor

Can you help me understand the using keyword with constructors?

+8
c ++ using
source share
1 answer

Your code is with using Grammateas::Grammateas; without commenting - should work. (But be careful: the inherited constructor would leave _hours uninitialized.)

Inheriting constructors via using -declarations is a new feature in C ++ 11. Perhaps your compiler does not yet support this function or has problems combining inherited constructors and other overloads. (If it accepts the final specifier, it seems to be correctly configured to compile C ++ 11.)

+9
source share

All Articles