开发者

Virtual difference syntax C++ [duplicate]

开发者 https://www.devze.com 2023-02-03 04:40 出处:网络
This questi开发者_运维技巧on already has answers here: Virtual/pure virtual explained (12 answers)
This questi开发者_运维技巧on already has answers here: Virtual/pure virtual explained (12 answers) Closed 8 years ago.

With C++, virtual is used like this? What's the difference between the both?

class Animal
{
public: 
    virtual void something();
    virtual void something() = 0; 
}


I might be fuzzy on this but I think the first says: You can override me, and the second says, You must override me.


virtual void something() = 0; // says it is a pure virtual function
virtual void something(); // is a virtual function

And classes that contain atleast one pure virtual function are called abstract base classes.The main difference between an abstract base class and a regular polymorphic class is that because in abstract base classes at least one of its members lacks implementation, we cannot create instances (objects) of it.


The first states that the function is virtual: that subclasses can override the behaviour. However, the function still has an implementation in the base class. The second is pure virtual: it doesn't have an implementation, and must be overridden by a subclass. It's similar to the abstract keyword in Java and C#. It makes the class abstract too, so it cannot be instantiated.


The second "something" must be implemented by subclasses and any class containing "xxx() = 0" cannot be directly instantiated. It's called a "pure virtual" function and classes containing them are "abstract". A class containing nothing but pure virtuals is "pure abstract".


first one is just declaring a virtual method. if you extend the class and override that method then the child class' implementation is called.

2nd one is a pure virtual method. in other words neither your class or any class that extends it be instantiated (abstract) without first providing a definition for something().


Consider:

class Sloth : public Animal { void something() { ... } };

Animal animal;
Sloth sloth;

Here, we're trying to create two objects - an Animal and a Sloth. But, should we be allowed to create an animal? Perhaps the programmer has created Animal just so it can be used to refer polymorphically to derived types, as in:

std::vector<Animal*> zoo;
zoo.push_back(new Sloth());
zoo.push_back(new Sea_Eagle());

They might then expect p_animal->something() to do something useful. Given Animal is only inteded for this polymorphic abstraction, it would be wrong for anyone to put an actual "new Animal()" object directly into the zoo (or operate on one anywhere else in the program).

Say it was possible - what should then happen if a programmer using this Animal class creates an instance and calls the something() function? Perhaps nothing, or perhaps it's an error condition and should never appear in good code - rather than having Animal's something() function print an error message or throws an exception at run time. That's ugly - run-time errors mean the program is failing when the client's trying to use it.

C++ supports the "= 0" notation so that the compiler knows to prevent (base-class) Animal objects being created at compile time, so you can ship software that always works for the user.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号