jeudi 25 février 2016

C++11 - constructor inheritance

I am not a C++ expert, so I need a help with perhaps a basic concept of C++ programming.

I have two classes, where one inherit from the other, so

class SuperClass
{
   private:
      std::string name;
      std::string str;
      long value;

   public: 
      SuperClass(std::string, std::string, long);
      ~SuperClass(void);
}

class SubClass : public SuperClass
{
   public: 
      SubClass(std::string, std::string, long);
      ~SubClass(void);
}

Now, in the main function, I would like to use SubClass objects, so I will need to initialize variables of SuperClass object. but with this code:

SubClass::SubClass(string name, string str, long value) : SuperClass(name, 
str, value)
{
   ;
}

gcc returns the error:

multiple definition of `SubClass::SubClass(std::string, std::string, long)'

So, what it the correct way to call a super-class constructor from a derived class in C++11 ?

Aucun commentaire:

Enregistrer un commentaire