Sek_ice_
Sek_ice_

Reputation: 39

Can the same function be defined differently for different objects of the same class?

Can the same function be defined differently for different objects of the same class??

in A.h

class Hello{


public:

void myfunction();


}

main.cpp

Hello B0,B1;
// Here I want to define the function 'myfunction' for each object differently 



int main(){

B0.myfunction();
B1.myfunction();
}

Is this possible?

Upvotes: 1

Views: 227

Answers (3)

mkungla
mkungla

Reputation: 3538

As from your comment

I am trying to create a kind of framework using c++ and opengl. Suppose i have a object of a class lets say Button which has a function onClick. On clicking button different users should be able to define the function in their own way.

See here for an excellent article by Herb Sutter on the subject of virtuality, since that is what you would look for when considering to build functionality described in your question and building framework.

The questions are old, but people still keep asking them,

One building framework could be interested on

  1. Polymorphism pointers and class inheritance;
  2. Virtual functions a member functions whose behavior can be overridden in derived classes.

Upvotes: 0

BoBTFish
BoBTFish

Reputation: 19767

Not directly, and I'd wonder why you want to do that if you really do want them to be objects of the same class type? This is what inheritance and virtual functions are for! If you want them to behave differently, you should make them different (but related) classes.

That said, I can think of one way to achieve something like this. std::function.

Have a class member like std::function<void(Hello*)> myfunctionimpl;, and then a member function void myfunction() { myfunctionimpl(this); }

Then in the constructor of Hello, you can set myfunctionimpl as a pointer to some other function, or with a lambda. For example, the following:

#include <functional>
#include <iostream>
class Hello {
    public:
        typedef std::function<void(Hello*)> MyFunctionType;

    private:
        MyFunctionType myfunctionimpl; // this holds the customisable function object

    public:
        Hello(const MyFunctionType& mf) // construct with a custom function object passed in
            : myfunctionimpl(mf)
        {}

        Hello() // construct with a default function object
            : myfunctionimpl([](Hello *h) {
                        std::cout << "Default function called on  " << h << '\n';
                    })
        {}
        Hello(int){} // dummy to leave function object unset

        void myfunction() {
            // call the function object, only if it is safe to do so
            if (this->myfunctionimpl) {
                this->myfunctionimpl(this);
            }
            else {
                std::cerr << "myfunctionimpl not set!\n";
            }
        }
};

void freeFunction(Hello*)
{
    std::cout << "freeFunction\n";
}

int main()
{
    Hello h1; // default
    Hello h2(1); // not set
    Hello h3(freeFunction); // custom function

    h1.myfunction();
    h2.myfunction();
    h3.myfunction();
}

prints:

Default function called on  0x7fffa12518e0
myfunctionimpl not set!
freeFunction

So here the member function myfunction behaves the same way for every instance; calls the myfunctionimpl function object (if it is set). But you can customise the function object that is called for each instance, since that is just class data.

The default constructor demonstrates use of lambdas, which allow you to write small functions in place, which is probably what you will want to do to provide custom behaviour when each object is constructed. There are lots of tutorials for lambdas, for instance.

Upvotes: 1

eerorika
eerorika

Reputation: 238311

No, a member function of all instances of the same class behave the same. However, the behaviour can depend on the state of the object. For example, you could store a function pointer in a data member and call the pointed function in the member function. So, the member function does exactly the same thing for each instance: calls the function pointed by the member, but the observed behaviour may be completely different if the pointed function is different.

Also, if the function is virtual, then instances of different subclasses can override the virtual function differently.

Upvotes: 1

Related Questions