Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Question

C++ Question

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alois Kraus
    wrote on last edited by
    #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 ?

    A N J R G 5 Replies Last reply
    0
    • A Alois Kraus

      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 ?

      A Offline
      A Offline
      Alvaro Mendez
      wrote on last edited by
      #2

      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.)

      A 1 Reply Last reply
      0
      • A Alois Kraus

        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 ?

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • A Alvaro Mendez

          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.)

          A Offline
          A Offline
          Alois Kraus
          wrote on last edited by
          #4

          I didnt know of that. I always thought the function of the current namespace would be used. Just tested it. It works. cooool Thanks

          A 1 Reply Last reply
          0
          • N Not Active

            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.

            A Offline
            A Offline
            Alois Kraus
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Alois Kraus

              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 ?

              J Offline
              J Offline
              Jason Henderson
              wrote on last edited by
              #6

              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 *

              1 Reply Last reply
              0
              • A Alois Kraus

                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 ?

                R Offline
                R Offline
                retZ
                wrote on last edited by
                #7

                You should have a new public (not private or protected) member function in class B, inside which you would call A's DoFunc() function... There are no failures; there are only extended learning opportunities.

                1 Reply Last reply
                0
                • A Alois Kraus

                  I didnt know of that. I always thought the function of the current namespace would be used. Just tested it. It works. cooool Thanks

                  A Offline
                  A Offline
                  Alvaro Mendez
                  wrote on last edited by
                  #8

                  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.)

                  1 Reply Last reply
                  0
                  • A Alois Kraus

                    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 ?

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    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;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups