Hi, I have seen some function defintion like this:
virtual int test()const;
virtual int test()const = 0;
virtual int test()=0;
I am wondering what the difference among them and "const" only for virtual only? or what they mean??
Thanks
Hi, I have seen some function defintion like this:
virtual int test()const;
virtual int test()const = 0;
virtual int test()=0;
I am wondering what the difference among them and "const" only for virtual only? or what they mean??
Thanks
Don't laugh at me,I am just a SuperNewbie.
I guess I'll take a stab at this since nobody else has yet.
First off, it appears that these are meant to be member functions of some base class from which a derived class will be created using inheritance.
The const simply means that the function in question is not going to modify the particular instance of the object we are dealing with. Within a const function, the only member functions that can be called are other const functions. No changes to the objects data members can be made. The const qualifier is not limited to virtual functions only.
The =0 parts indicate that these are pure virtual functions. If a class has any pure virtual functions then the class is called an abstract base class. One cannot construct an instance of an abstract base class, you can only use that class to create a derived class and you must overload all of those pure virtual functions in the derived class for you to be able to instantiate an object of the derived class.
ex:
Basically the base class becomes a framework with a specified set of functions that form a basic interface with known arguments and known return values and which the creator of the derived class must implement themselves.Code:// Create an abstract base class, you cannot instantiate an object of this type class base { public: virtual int test() = 0; }; // Create a derived class using the abstract base class class derived: public base { public: int test() { // Implement the test function, must be present in order to // instantiate objects of type "derived" } void func1() const { value = 10; // This statement is illegal since this is a const // function and it changes a class's member data func2(); // Also illegal since func2 is a non-const function func3(); // Fine since func3 is const function } void func2() { value = 10; // This is fine, it is perfectly allowable to modify a // member variable since it is a non-const function } void func3() const { } int value; }; base b; // Error - base is abstract class and you cannot create an instance of this class. derived d; // Ok
Last edited by hk_mp5kpdw; 01-24-2005 at 12:14 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Thanks, a lot!!!
Don't laugh at me,I am just a SuperNewbie.