Archived

This topic is now archived and is closed to further replies.

duhroach

polymorphism problem

Recommended Posts

I'm getting a wierd problem that I can't seem to find a way around. Can someone explain this to me?
class a{
public:
void (*draw)();
}

class b{
public:
void draw();
}

init(){
...
a A;
b B;
A.draw = B.draw;
I get the following error: error C2440: '=' : cannot convert from 'void (__thiscall a::*)(void)' to 'void (__cdecl *)(void)' Can someone help me out? ~Main == Colt "MainRoach" McAnlis Programmer www.badheat.com/sinewave [edited by - duhroach on January 17, 2004 6:16:21 PM]

Share this post


Link to post
Share on other sites
Why don't you use a more common way of implemting polymorphism.. virtual functions?
Sure, it implies one extra dereffering, but you wouldn't use it in performance critical parts anyway..

Here's an example:


#include <iostream>

using namespace std;

struct Person {
virtual void hi() { cout << "hi, I'm a person" << endl; }
};


struct Man : public Person {
virtual void hi() { cout << "hi, I'm a man" << endl; }
};


struct Woman : public Person {
virtual void hi() { cout << "hi, I'm a woman" << endl; }
};



int main()
{
Person* people[3];
people[0] = new Person;
people[1] = new Man;
people[2] = new Woman;

for( int i=0; i<3; i++ ) people[i]->hi();

for( i=0; i<3; i++ ) delete people[i];

return 0;
}



Notes: try to save few whitespaces here and there.. struct instead of classes (default public members), and function decleration inside class decleration


btw, how do you keep the linebreaks in the code?

[edited by - BiTwhise on January 17, 2004 6:58:54 PM]

Share this post


Link to post
Share on other sites