Sign in to follow this  

[C++] Deferring overload resolution to runtime.

This topic is 3496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Consider the following simple class hierarchy:
class Base
{
public:
	void foo()
	{
		cout << "Base::foo()" << endl;
	}
};

class Derv1 : public Base
{
public:
	void foo()
	{
		cout << "Derv1::foo()" << endl;
	}
};

class Derv2 : public Base
{
public:
	void foo()
	{
		cout << "Derv2::foo()" << endl;
	}
};






Now consider the following overload functions:
// Function #1
void doFoo(Base* ptr)
{
	cout << "Base" << endl;
	ptr->foo();
}

// Function #1
void doFoo(Derv1* ptr)
{
	cout << "Derv1" << endl;
	ptr->foo();
}

// Function #1
void doFoo(Derv2* ptr)
{
	cout << "Derv2" << endl;
	ptr->foo();
}






Is there a way, at runtime, for the correct overload to be chosen depending on class type instead of the pointer type? For example:
Base* ptr = new Base();
doFoo(ptr); // I want this to call function #1

Base* ptr = new Derv1();
doFoo(ptr); // I want this to call function #2

Base* ptr = new Derv2();
doFoo(ptr); // I want this to call function #3
Of course what I've constructed doesn't do that. Function #1 is always called because the overload is being resolved statically at compile time based on the pointer type. How can I achieve this behavior? Does it necessarily require me to do some dynamic_cast then select the function "manually"?

Share this post


Link to post
Share on other sites
See visitor pattern. Basically, double dispatch solves the problem.

struct Visitor {
virtual void Visit() = 0;
};

class A : public Visitor {
void Visit() { doVisit(this); }
};

class B : public Visitor {
void Visit() { doVisit(this); }
};

class C : public Visitor {
void Visit() { doVisit(this); }
};

void doVisit(A* a) {
...
}

void doVisit(B* a) {
...
}

void doVisit(C* a) {
...
}

Visitor* v = ...
v.Visit();

Share this post


Link to post
Share on other sites
Maybe i didn't understood your problem, but implementing the foo method as virtual doesn't suffice your needs ?



class Base
{
public:
void foo()
{
cout << "Base::foo()" << endl;
}
};

class Derv1 : public Base
{
public:
virtual void foo()
{
cout << "Derv1::foo()" << endl;
}
};

class Derv2 : public Base
{
public:
virtual void foo()
{
cout << "Derv2::foo()" << endl;
}
};





And then just have a function, to which you can passe a pointer of the type u want, Base, Derv1, Derv2.
And if you really wan't that cout in the function instead of inside the method food, RTTI to the rescue

void doFoo(Base* ptr)
{
cout << typeid(ptr).name() << endl;
ptr->foo();
}


Share this post


Link to post
Share on other sites
If you want a specific function to be called depending on the type of one object, use polymorphism.
If you want to call a function depending on the types of two objects, use a "double dispatcher" (you can google this).

Share this post


Link to post
Share on other sites

This topic is 3496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this