Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

[C++] Deferring overload resolution to runtime.

This topic is 3711 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!