overriding virtual function to non-virtual function is OK? [duplicate] - c++

This question already has answers here:
C++ "virtual" keyword for functions in derived classes. Is it necessary?
(9 answers)
Closed 9 years ago.
class Base
{
virtual void foo();
}
class Derived : public Base
{
void foo();
}
It it OK?
Or it can make some problems?
I think it insist "DO NOT INHERIT FROM DERIVED".

Once you marked the foo() function in Base class as virtual it is implicitly virtual in Derived class, even if you don't mention the keyword virtual in front of it.

virtualness is inherited. Even if you do not declare an overridden method virtual, it will be virtual.
Hence, if you access an object of Derived using Base pointer or reference, it will call foo of Derived.

virtual functions are inherited automatically. So even if you don't declare it as virtual, it is virtual actually.
The reason for you to explicitly declare it as virtual is for better clarification so that anyone reading this code can instantly understand that it is a virtual functions, without having to check the base class declarations.

Related

Should one declare functions virtual at all levels of inheritance or only at the ancestor level? [duplicate]

This question already has answers here:
C++ Style: Prefixing virtual keyword to overridden methods
(6 answers)
Closed 6 years ago.
Should one mark explicitly as virtual all overrides in descendant classes at any level?
class Base {
// ...
protected:
virtual void to_be_derived() const; // First level to introduce this virtual function
};
class LevelOne : public Base {
// ...
protected:
// virtual??
void to_be_derived() const;
};
class LevelTwo : public levelOne {
// ...
protected:
// virtual??
void to_be_derived() const;
};
I didn't see the Prefixing virtual keyword to overridden methods which answers my question. In particular, one of the answers there was updated to reflect current usage with respect to c++11, especially the override keyword that I didn't know about!
EDIT: I'd rather accept another answer from the linked question for post-c++11 code.
Nowadays, it's better to mark them as override. It tells the reader the function is virtual, and is also a fail-safe mechanism (in case you get the signature wrong).
I'd only use virtual if that was consistent with already existing code.
class LevelOne : public Base {
protected:
void to_be_derived() const override;
// |
// clearly virtual, certain it's the same signature as the base class
};
It's better to mark them as virtual and override. Virtual will prevent you from calling wrong function for delivered object. And override will prevent you from mistakes in signature and will make code more readable.
As Scott Meyers wrote in effective c++ book, you should't redefine in delevired classes non virtual functions.
Base Class Virtual would force the inheriting class ie LevelOne to override it.
Unless you need LevelTwo to override LevelOne's implementation , you do not need to mark it as virtual.
In general, unless the derived class has to override it, no need of using virtual

C++ standard way to create "Abstract Class" (Pure Virtual Class) [duplicate]

This question already has answers here:
C++ abstract class without pure virtual functions?
(3 answers)
Closed 7 years ago.
I will start with what's most of us already know:
If I want my class to be abstract, I must define at least one of its methods as "pure virtual", for example, here, the method someFunction() is defined as "pure virtual", as it is defined with the virtual keyword and it is assigned with 0:
class SomeClass {
virtual void someFunction() = 0;
};
My question is, when I want an "abstract class", i.e. a class which cannot be instantiated (like "pure virtual" class), but I want to implement all its methods. Is there any standard way to do it?
My current workaround is ugly - I just define another dummy pure virtual method:
class UglyWorkaround {
public:
virtual void doAction1();
virtual void doAction2();
// My ugly workaround for making the class abstract - defining a dummy method
virtual void thisIsADummyMethod() = 0;
};
This is very bad, as any deriving non-abstract class will have to implement it
Is there a more standard/popular way to define and implement such a class?
I want to clarify - I don't want another ugly workaround - I ask whether there is any standard way that is commonly used. The objective is to make the code readable for other programmers, so they immediately understand that the class is abstract
Define the constructor protected

why is it possible to make the destructor pure virtual [duplicate]

This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 7 years ago.
As I know in cpp, when we delete an object or when the main finishes, the destructors of all objects will be called. For those objects whose type are class child, the destructors of class child will be called first then the distructors of class parent will be called.
Now I am confused. Because if a pure virtual destructor is allowed, how could it be called when we delete an object of class child? Doesn't it call the destructor of class parent which is pure virtual?
Yes, the destructor of the base class is called. This means it must have an implementation. It is possible to provide an implementation for any pure virtual function, including the destructor. For example:
struct foo
{
virtual ~foo() = 0; // pure virtual dtor
};
foo::~foo() {} // implementation
The use-case of a pure virtual destructor is to ensure a class without any other pure virtual methods cannot be instantiated.

Calling pure virtual function in constructor gives an error [duplicate]

This question already has answers here:
call to pure virtual function from base class constructor
(8 answers)
Closed 5 years ago.
class a //my base class
{
public:
a()
{
foo();
}
virtual void foo() = 0;
};
class b : public a
{
public:
void foo()
{
}
};
int main()
{
b obj; //ERROR: undefined reference to a::foo()
}
Why it gives me error? The pure virtual foo is defined. What do I need to change in my code to make it work? I need pure virtual method from base class be called in its constructor.
Calling virtual functions in a constructor is recognised as a bad thing to do.
During base class construction of a derived class object, the type of
the object is that of the base class. Not only do virtual functions
resolve to the base class, but the parts of the language using runtime
type information (e.g., dynamic_cast (see Item 27) and typeid) treat
the object as a base class type.
So your instantiation of b invokes the a constructor. That calls foo(), but it's the foo() on a that gets called. And that (of course) is undefined.
Quoted from a book "Let Us C++"by Yashwant Kanetkar
It is always the member function of the current class , is called.That
is , the virtual mechanism doesn't work within the constructor
So, the foo() of class a gets called.
Since it is declared pure virtual, it will report an error
foo function is called in class a's constructor, and at that time, object b has not been fully constructed yet, hence it's foo implementation is unavailable.
Quoted from "Effective C++":
Don't call virtual functions during construction or destruction,
because such calls will never go to a more derived class than that of
the currently executing constructor or destructor

Override vs Overwrite [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Overriding… overwriting?
What's the difference between override and overwrite? I've heard them used interchangeably but I suspect that's incorrect.
You can only overwrite what's been written and where it's been written, while you can override things elsewhere (for instance you can override members of base class in derived classes).
override is a C++11 keyword used to override base virtual method:
class A
{
virtual f(int) {}
};
class B
{
virtual f(int) override {} // override A::f(int)
};
This lets you make sure that A::F(int) gets overriden, meaning you are not creating new virtual function.
Of course this code won't compile if the function signature was different.
overwrite is not C++ keyword and it basically means to overwrite some file or text with new one.
The keyword override has been introduced because some times a programmer doesn't know whether he is overriding or whether he is creating a new virtual method with a different signature.
Using that keyword you either get an error or override virtual method.