C++ Question
-
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
-
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
This code should print 1. I went as far as to try it myself and it does. Regards, Alvaro
Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)
-
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
You mean? A.DoFunc() == 1 No, class A knows nothing about class B. B on the other hand knows about A because it is derived from it.
-
This code should print 1. I went as far as to try it myself and it does. Regards, Alvaro
Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)
I didnt know of that. I always thought the function of the current namespace would be used. Just tested it. It works. cooool Thanks
-
You mean? A.DoFunc() == 1 No, class A knows nothing about class B. B on the other hand knows about A because it is derived from it.
I wrote Inst.DoFunc() in the example. This was not obvious to me. Inst.DoFunc() does indeed ignore f() member in class A and uses the derived f() from B.
-
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
If it is public or protected it will work.
Jason Henderson
start page ; articles henderson is coming henderson is an opponent's worst nightmare * googlism * -
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
-
I didnt know of that. I always thought the function of the current namespace would be used. Just tested it. It works. cooool Thanks
No problem. That's what the
virtual
keyword is for, in front of your f() function. I suggest you read up on it. Regards, Alvaro
Well done is better than well said. -- Benjamin Franklin (I actually prefer medium-well.)
-
Is it possible to call a specialized function of a base class ? I mean: class A { protected: virtual int f() { return -1; }; public: void DoFunc() { printf("\nValue: %d",f()); }; }; class B : public A { protected: virtual int f() { return 1; }; }; int main() { B Inst; Inst.DoFunc(); } pure virtual function does not help because the MFC macro IMPLEMENT_DYNCREATE makes the compiler think it get instantiated. Is it possible to get the class A to print 1 instead of -1 ?
If you want to guarantee that the base class version will be called, rather than a virtual one from a derived class, you can do the following:
class A
{
protected:
virtual int f() { return -1; };public:
void DoFunc() { printf("\nValue: %d",A::f()); };};
Software Zen:
delete this;