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. virtual function declarations

virtual function declarations

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionannouncement
6 Posts 4 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.
  • L Offline
    L Offline
    lastgen
    wrote on last edited by
    #1

    I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO // This version gives me an unresolved external error class ABC { public: virtual void FOO(BAR& foo); }; // This version is fine class ABC { public: virtual void FOO(BAR& foo) **_= 0_**; }; My question is this, What does the "= 0" indicate? Why is it needed? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

    L N C J 4 Replies Last reply
    0
    • L lastgen

      I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO // This version gives me an unresolved external error class ABC { public: virtual void FOO(BAR& foo); }; // This version is fine class ABC { public: virtual void FOO(BAR& foo) **_= 0_**; }; My question is this, What does the "= 0" indicate? Why is it needed? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

      L Offline
      L Offline
      lastgen
      wrote on last edited by
      #2

      I think I have found the answer, The "= 0" is to indicate to the c(++) compiler that this function is purely virtual and there is no implementation in the base class, which therefore cannot be instantiated directly (which is what I intended). Is this correct? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

      C 1 Reply Last reply
      0
      • L lastgen

        I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO // This version gives me an unresolved external error class ABC { public: virtual void FOO(BAR& foo); }; // This version is fine class ABC { public: virtual void FOO(BAR& foo) **_= 0_**; }; My question is this, What does the "= 0" indicate? Why is it needed? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        lastgen wrote:

        What does the "= 0" indicate?

        This means FOO is a pure virtual function, implementation for which will be provided in a derived class of the class ABC. ABC cannot be instantiated directly. But derived classes for ABC which provide an implementation for FOO can be instantiated.


        Nibu thomas Software Developer

        1 Reply Last reply
        0
        • L lastgen

          I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO // This version gives me an unresolved external error class ABC { public: virtual void FOO(BAR& foo); }; // This version is fine class ABC { public: virtual void FOO(BAR& foo) **_= 0_**; }; My question is this, What does the "= 0" indicate? Why is it needed? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          First, it is always usefull to add the exact error message when you got one. We cannot guess what the error message is (and in general they are really helpfull). Now, to answer your question, the =0 means that this is a pure virtual function, that is a function that NEED to be redefined in the child classes. If you don't redefine them in child classes and if you try to instantiate your object, you will get an erro telling you that it cannot instantiate the class due to pure vitual function. But in your example, I don't see why you get an unresolved external error if you don't add this =0. Maybe try to post more code and the exact error message.

          1 Reply Last reply
          0
          • L lastgen

            I think I have found the answer, The "= 0" is to indicate to the c(++) compiler that this function is purely virtual and there is no implementation in the base class, which therefore cannot be instantiated directly (which is what I intended). Is this correct? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Exact but it doesn't correspond to your error (because it was the other way: it worked if you add this '=0' and not if you retrieve it).

            1 Reply Last reply
            0
            • L lastgen

              I have some code that was generating compile errors. I have found the solution to fix the problem, I'm just not 100% sure I understand what the problem actually is. The (much simplified)code below should help explain the issue. I have created a decendant object from ABC that contains the implementation of function FOO // This version gives me an unresolved external error class ABC { public: virtual void FOO(BAR& foo); }; // This version is fine class ABC { public: virtual void FOO(BAR& foo) **_= 0_**; }; My question is this, What does the "= 0" indicate? Why is it needed? When I die I'd like to go peacefully in my sleep like my father, not screaming in terror like his passengers!!!

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              I am making an assumption that you did not define the function in the original class and that the second version worked. If you had defined the function (or method) in the first class you would not have had an error. Your could have simply declared it as this virtual void FOO (BAR& foo) {}; and there would not have been an error, you would have just had a function that did not do anything. The second version declares a pure virtual function, which needs to be implemented by a derived class. That is you inherited the interface from the defined base class (ABC) and must provide a function for the inherited interface. What that means is in the base class you have said this function must exit in the derived class, because it does not actually exist in the base class. Let’s try this, If you declare a function in a class the compiler will complain if that function does not exist. Therefore you must define the function so any one using that class will have that function to call. If you make the function as a pure virtual function, then the class is designed as a base interface class that is not intended to be used by its self, but is intended to be used as a base for derived classes. I know I sound like some text book, but I have been doing this a long time. Look up the words “pure virtual” and “virtual base class” along with “C++” on the Web; you should come up with a few decryptions. Amongst those may be instrument and car, all cars have things in common like press the accelerator, press the brake, and ect… . The base class specifies all things that classes derived from it have in common and forces all derived classes to implement those behaviors. Think of the “= 0” as a requirement that you impose on all users of the class that you have defined. I hope that helps, John R. Shaw INTP Every thing is relative...

              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