virtual function declarations
-
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!!! -
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!!!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!!!
-
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!!!lastgen wrote:
What does the "= 0" indicate?
This means
FOO
is apure virtual
function, implementation for which will be provided in a derived class of the classABC
.ABC
cannot be instantiated directly. But derived classes forABC
which provide an implementation forFOO
can be instantiated.
Nibu thomas Software Developer
-
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!!!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.
-
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!!!
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).
-
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!!!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...