Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Understanding Functors Polymorphism

I try to implement polymorphic functor objects (pure abstract base class and children) for understanding purposes only. My goal is to create many objects of the base class that use different implementations of the pure virtual functions.

When I create a pointer of the base class and set it equal to a new child class, I can not call the object as a function. The error is:

main.cpp:29:7: error: ‘a’ cannot be used as a function

Here is the code:

#include <iostream>

class foo{
public:
    virtual void operator()() = 0;
    virtual ~foo(){}
};

class bar: public foo{
public:
    void operator()(){ std::cout << "bar" << std::endl;}
};

class car: public foo{
public:
    void operator()(){ std::cout << "car" << std::endl;}
};


int main(int argc, char *argv[])
{

    foo *a = new bar;
    foo *b = new car;

    //prints out the address of the two object: 
    //hence I know that they are being created
    std::cout << a << std::endl;
    std::cout << b << std::endl;

    //does not call bar() instead returns the error mentioned above
    //I also tried some obscure variation of the theme:
    //*a(); *a()(); a()->(); a->(); a()();
    //just calling "a;" does not do anything except throwing a warning
    a();

    //the following code works fine: when called it print bar, car respectivly as expected
    // bar b;
    // car c;
    // b();
    // c();

    delete b;
    delete a;
    return 0;
}

My current understanding is that "foo *a" stores the address of of the function object "bar" in a (as shown by the cout statement). Hence dereferencing it "*a" should provide access to the function to which "a" is pointing to and "*a()" should call it.

But it does not. Can anyone tell me why?

like image 548
Vincent Avatar asked May 05 '15 07:05

Vincent


People also ask

Can you use C language to demonstrate polymorphism?

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. According to that definition, no, C doesn't natively support polymorphism.

What is the point of functors?

A functor (or function object) is a C++ class that acts like a function. Functors are called using the same old function call syntax. To create a functor, we create a object that overloads the operator().

What are functors in C?

A C++ functor (function object) is a class or struct object that can be called like a function. It overloads the function-call operator () and allows us to use an object like a function.

What do you mean by polymorphism in C++?

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


1 Answers

Since you have a pointer for a, you must dereference it to call the () operator:

(*a)(); // Best use parentheseis around the dereferenced instance
like image 62
πάντα ῥεῖ Avatar answered Sep 30 '22 00:09

πάντα ῥεῖ