Virtual Overloaded Function Question...
-
I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini
-
I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini
Polymorphism only kicks in if you are accessing your C++ object through a pointer. This is what you need to do: { derived myDerived, *pDerived; int nData = 5; pDerived = &myDerived; pDerived->fn(nData); } ================== The original message was: I was wondering if somebody can explain why the following doesn't work:
class base
{
public:
virtual void fn(int nData) ( /* Do Something */ };
virtual void fn(char * pszData) { /* Do Something */ };
}class derived : public base
{
public:
virtual void fn(char * pszData) { /* Do Something */ };
}void MyFunction
{
derived myderived;
int nData = 5;myderived.fn(nData);
}The compiler complains about myderived.fn(nData). It says....
"error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...."Why doesn't it just use the base class fn(int nData)????
Am I doing something wrong or is this C++ behaviour?Thanks in Advance
Chris Mancini -
I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini
You have actually overrided the function 'fn' and using it as a overloaded function. Check it out. Regards, Prabhat ================== The original message was: I was wondering if somebody can explain why the following doesn't work:
class base
{
public:
virtual void fn(int nData) ( /* Do Something */ };
virtual void fn(char * pszData) { /* Do Something */ };
}class derived : public base
{
public:
virtual void fn(char * pszData) { /* Do Something */ };
}void MyFunction
{
derived myderived;
int nData = 5;myderived.fn(nData);
}The compiler complains about myderived.fn(nData). It says....
"error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...."Why doesn't it just use the base class fn(int nData)????
Am I doing something wrong or is this C++ behaviour?Thanks in Advance
Chris Mancini