How to call exact derived class method?

call derived class method from base class c
calling derived class function from base class
c++ call base class method in derived object
base class and derived class have same function in c++
call base class method c++
c++ call base class method from outside
virtual function
pure virtual function

In continuation of my previous question: How to return derived type?

I have a Validator class and derived classes from it; When i'm trying to return pointer to derived class then method return base class(Validator) instead of Derived.

I modified my class as next:

class Validator
{
public:
    std::string m_name = "BaseValidator";

    static const std::map<std::string, Validator *> validators();

    void validateModel(Model &model, const std::vector<std::string> &attrNames);
    static Validator *getByName(std::string &name);

    virtual void validateAttribute(Model &model, const std::string &attribute);
    virtual ~Validator();

    virtual std::string name() const;
};

const std::map<std::string, Validator*> Validator::validators()
{
    std::map<std::string, Validator*> result;
    result["required"] = new RequiredValidator();
    return result;
}

void Validator::validateModel(Model &model, const std::vector<std::string> &attrNames)
{      
     validateAttribute(model, name.at(0));    
}

Validator* Validator::getByName(std::string &name)
{
    auto g_validators = Validator::validators();
    auto validator = g_validators.find(name);
    if(validator != g_validators.end()){
        return validator->second;
    }else{
        std::cerr << "Unknow type of validator: " << name << std::endl;
    }
    return nullptr;
}

void Validator::validateAttribute(Model &model, const std::string &attribute)
{
    (void)model;
    (void)attribute;
    std::cout << this->name() << std::endl;
}

//------------------

class RequiredValidator : public Validator
{
public:
    std::string m_name = "RequiredValidator";

    void validateAttribute(Model &model, std::string &attribute);
    ~RequiredValidator();
    std::string name() const;
};


std::string RequiredValidator::name() const
{
    return m_name;
}

void RequiredValidator::validateAttribute(Model &model, std::string &attribute)
{
    (void) model;
    (void) attribute;
    std::cout << "RequiredValidator!!!" << std::endl;
}

RequiredValidator::~RequiredValidator()
{

}


//------------------

auto validator = Validator::getByName("required");

validator->name();// will output RequiredValidator

//next i'm calling 

validator->validateModel(); // whos calling validateAttribute() inside
//and it's output Validator::validateAttribute() //but i wan't
//RequiredValidator::validateAttribute(); 
//where i was wrong with logic???

The same virtual method name() working as i want, but validateAttribute() called from base class only.

Look closely at how you defined void RequiredValidator::validateAttribute()

You may note that the signature is slightly different than the base (std::string &attribute isn't const)

there's a keyword override that would help with the compiler catching these types of typos.

How to call derived class method from base class pointer?, You need to make DoSomething() a virtual function in both classes to get the polymorphic behavior you're after: virtual void DoSomething(int i) { You don't  @exru, actually, if there was a const but not a reference, then it would've been ok to overload, i.e. void validateAttribute(Model &model, std::string attribute) and void validateAttribute(Model &model, const std::string &attribute) would not have this problem.

Inheritance, When my base class's constructor calls a virtual function on its this object, why in a base class are not accessible in classes derived from it, which is correct. To have a derived function call a base function of the same name, simply do a normal function call, but prefix the function with the scope qualifier (the name of the base class and two colons). The following example redefines Derived::identify() so it first calls Base::identify() and then does its own additional stuff.

Removing all of the irrelevant code let's look at both your base and inherited classes' signatures:

class Validator {
public:
    virtual void validateAttribute(Model &model, const std::string &attribute);
};

class RequiredValidator : public Validator {
public:
    void validateAttribute(Model &model, std::string &attribute);
};

In the base class's ::validateAttribute() you have for its parameters Model& model and const std::string& attribute, and in the derived class's ::validateAttribute() you have for its parameters Model &model, and std::string& attribute.

The conflict here is const std::string& and std::string&. Either make the two functions match exactly beforehand or in your case because you are trying to use polymorphism you can in your derived class fix this by doing this:

/*virtual*/ void validateAttribute( Model& model, std::string& string ) override;

The override keyword here will generate a compiler error for you explaining what your problem is. Don't forget to add virtual too if you plan on inheriting from this derived class.

11.6a, Let's modify function identify() in the Derived class so it returns the correct response when we call function identify() with a Derived object. The first one is of type Base and holds an instance of Base class. The second one is of type Derived and holds an instance of a Derived class. The third one is of type Base and holds an instance of the Derived class. Now, create an object of the TestOverriding class in the Program.cs and call the method Function1 ().

Function Overriding in C++, To override a function you must have the same signature in child class. cout<<"​Function of Parent Class"; } }; class DerivedClass: public BaseClass{ we make the call to function (involved in overriding), the child class function (overriding  Inheritance in C# and .NET. 07/05/2018; 26 minutes to read +11; In this article. This tutorial introduces you to inheritance in C#. Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.

Virtual Functions, Virtual functions ensure that the correct function is called for an object, The function from the derived class is invoked for objects of the derived class, A call to a virtual function is resolved according to the underlying type of  To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;). A class must have a matching filename (Car and Car.java).

C++ Polymorphism and Method Overriding, In inheritance, polymorphism is done, by method overriding, in C++. Base class object will call base version of the function and derived class's object will call  1. an instance of a derived Class can call methods, and set/get public variables/fields, of its Base Class even if an instance of the Base Class has never been created. 2. the special qualifier "base" followed by a dot and the name of publicly exposed variable/field, or method, in the Base Class: is another way the derived Class can use its

Comments
  • Need i redefine then "virtual" keyword? Because "error: non-virtual member function marked 'override' hides virtual member function"
  • the validateAttribute signature has to be identical in base and in the derived class
  • Only one thing: undefined reference to `Validator::name[abi:cxx11]() const'. How to fix it?
  • @exru: it says that it lacks the implementation of virtual std::string name() const;.
  • Thanks bro, i fixed it myself. It says what: 1.listen the compilator 2. teach english 3. read docas e.t.c. All troublshooting in my bad english. But i'm trying)) And i spent a lot of time with PHP. I'm writing on C++ only 2 month))) Thanks for the help.
  • @exru, actually, if there was a const but not a reference, then it would've been ok to overload, i.e. void validateAttribute(Model &model, std::string attribute) and void validateAttribute(Model &model, const std::string &attribute) would not have this problem. (BUT of course, it is not what we want to do..) I just wanted to elaborate a point. The difference is for references and pointers! For them const makes a difference.
  • Thanks bro. I fixed!
  • @JoeyMallone Did you mean const std::string attribute in the second signature of your comment? Because T and const T are mangled the same in signatures, but T and const T& are certainly not...
  • @MaxLanghof, yes. But I can't edit the comment now. :( I mean't, void validateAttribute(Model &model, std::string attribute) and void validateAttribute(Model &model, const std::string attribute) would've been ok for overloading. No & in both declarations.