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. Pure Virtual Function Call

Pure Virtual Function Call

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestion
7 Posts 5 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
    abc876
    wrote on last edited by
    #1

    I want to know when does "Pure Virtual Function Call" occurs? I am getting this problem in my code and unable to find out. Note: I am not calling a pure vitual function in my base class constructor. So this error is not due to calling a pure virtual function from a base class constructor. + My application is a multithreaded application. I wanna know the conceptual reasons behind other cases in which pure virtual function call can occur. Thanks Muhammad Shoaib Khan http://geocities.com/lansolution

    D M 2 Replies Last reply
    0
    • A abc876

      I want to know when does "Pure Virtual Function Call" occurs? I am getting this problem in my code and unable to find out. Note: I am not calling a pure vitual function in my base class constructor. So this error is not due to calling a pure virtual function from a base class constructor. + My application is a multithreaded application. I wanna know the conceptual reasons behind other cases in which pure virtual function call can occur. Thanks Muhammad Shoaib Khan http://geocities.com/lansolution

      D Offline
      D Offline
      digwizfox
      wrote on last edited by
      #2

      I have no idea what you are asking. Could you be more specific in describing your problem? What do you mean, when does it occur? It occurs when someone types in the function call from another function and runs the program. Is there a compile error or weird runtime behavior that you are debugging?

      A 1 Reply Last reply
      0
      • D digwizfox

        I have no idea what you are asking. Could you be more specific in describing your problem? What do you mean, when does it occur? It occurs when someone types in the function call from another function and runs the program. Is there a compile error or weird runtime behavior that you are debugging?

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        The problem occurs on runtime.. a messagebox popups "Pure Virtual Function Call" and program terminates. i have a base class which contain a pure virtual function. Another derived class provides its implementation. In my multithreaded application, when i try to call that function from base class pointer with address of derived class, this error occurs.. But it occurs in some rare scenario.. not consistant.. I just want to clarify the concept.. when does pure virtual function call occurs? in what possible scenarios can this runtime error occur

        D 1 Reply Last reply
        0
        • A abc876

          I want to know when does "Pure Virtual Function Call" occurs? I am getting this problem in my code and unable to find out. Note: I am not calling a pure vitual function in my base class constructor. So this error is not due to calling a pure virtual function from a base class constructor. + My application is a multithreaded application. I wanna know the conceptual reasons behind other cases in which pure virtual function call can occur. Thanks Muhammad Shoaib Khan http://geocities.com/lansolution

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Most likely you are, indirectly, calling a pure virtual function in your base class constructor. I recently wrote up how the compiler handles abstract classes and the C++ rules for which overrides are called at what point in answer to a question here[^]. Basically what's happening is you're trying to call through a virtual function that was declared pure either in your class or one of your base classes. One possibility:

          // In some header

          class A
          {
          public:
          A();

          protected:
          void OtherFunc();

          virtual void MyFunc() = 0;
          };

          class B : public A
          {
          public:
          B();

          virtual void MyFunc();
          };

          // Source file a1.cpp

          A::A()
          {
          OtherFunc();
          }

          void A::OtherFunc()
          {
          MyFunc();
          }

          B::B()
          {
          }

          void B::MyFunc()
          {
          }

          When A's constructor body runs, the virtual function pointer is still pointing to A's vtable. The constructor calls OtherFunc which calls MyFunc, which is generated as a virtual call through the vtable. However, A::MyFunc is a pure virtual function; calling it this way is an error. The Visual C++ compiler traps the error by making the vtable slot point to a routine in the runtime library which reports the error. Stability. What an interesting concept. -- Chris Maunder

          A 1 Reply Last reply
          0
          • M Mike Dimmick

            Most likely you are, indirectly, calling a pure virtual function in your base class constructor. I recently wrote up how the compiler handles abstract classes and the C++ rules for which overrides are called at what point in answer to a question here[^]. Basically what's happening is you're trying to call through a virtual function that was declared pure either in your class or one of your base classes. One possibility:

            // In some header

            class A
            {
            public:
            A();

            protected:
            void OtherFunc();

            virtual void MyFunc() = 0;
            };

            class B : public A
            {
            public:
            B();

            virtual void MyFunc();
            };

            // Source file a1.cpp

            A::A()
            {
            OtherFunc();
            }

            void A::OtherFunc()
            {
            MyFunc();
            }

            B::B()
            {
            }

            void B::MyFunc()
            {
            }

            When A's constructor body runs, the virtual function pointer is still pointing to A's vtable. The constructor calls OtherFunc which calls MyFunc, which is generated as a virtual call through the vtable. However, A::MyFunc is a pure virtual function; calling it this way is an error. The Visual C++ compiler traps the error by making the vtable slot point to a routine in the runtime library which reports the error. Stability. What an interesting concept. -- Chris Maunder

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            No , i have already mentioned. i am not calling it through constructor. Any other reason? anyways, thanks Shoaib

            M 1 Reply Last reply
            0
            • A Anonymous

              The problem occurs on runtime.. a messagebox popups "Pure Virtual Function Call" and program terminates. i have a base class which contain a pure virtual function. Another derived class provides its implementation. In my multithreaded application, when i try to call that function from base class pointer with address of derived class, this error occurs.. But it occurs in some rare scenario.. not consistant.. I just want to clarify the concept.. when does pure virtual function call occurs? in what possible scenarios can this runtime error occur

              D Offline
              D Offline
              digwizfox
              wrote on last edited by
              #6

              It's still kind of vague to me what you are doing. Normally, you call the virtual function using the base class pointer. I don't see any reason why having a derived class pointer would cause a problem. But as a design critique, the purpose of virtual functions is to be able to hide dependencies by using the base class pointer to interface to the object. That way, the users of the object are only bound to the base class at compile time. During runtime, the user is unaware that the derived class actually implements the function. There isn't much point in creating a pure virtual function if the users of your object are calling the function from a derived class pointer anyway. That defeats the purpose of run time binding. You should have some kind of factory build the classes so that the dependency on the derived class is controlled and limited. The actual users of the class that call the functions should get pointers of the base class type from the factory. Then if the implementation needs changed in the future, not as many pieces of your program have to recompile when you make changes. Maybe you could show us your class declarations and a few code snippets where the function calls are made to give us an example of what you are doing.

              1 Reply Last reply
              0
              • A Anonymous

                No , i have already mentioned. i am not calling it through constructor. Any other reason? anyways, thanks Shoaib

                M Offline
                M Offline
                mark novak
                wrote on last edited by
                #7

                I've never experienced a pure virtual function call. However from what I hear it really can only happen in the constructor or in the deconstructor. Then off course it could be a side effect of using invalid pointers to these objects. Also MSVC6 has been known to allow abstract classes to be created in certain cases. It also never hurts to do a rebuild all. I’ve seen some really straight up weird occurrences cause me a good couple hours of confusion all completely melt with a simple rebuild all.

                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