Implementation of pure virtual method with/without virtual? [duplicate] - c++

This question already has answers here:
C++ "virtual" keyword for functions in derived classes. Is it necessary?
(9 answers)
Closed 8 years ago.
If I have an AbstractClass with a "virtual void Method()=0". What is the difference if a DerivedClass defines the implementation as "virtual void Method() { }" or simply "void Method() { }" ?

There is no difference. It's just for clarity.
Any method defined as virtual in a base class, is also virtual in the classes which inherit from it, regardless whether it's declared like that or not.

Related

vtable in case of virtual inheritance [duplicate]

This question already has answers here:
What is a vtable in C++ [duplicate]
(3 answers)
Why do we need a virtual table?
(5 answers)
How are virtual functions and vtable implemented?
(12 answers)
Closed 5 years ago.
What is the use of vtable (or why is vtable required ) in case of virtual inheritance ? what does this vtable points to in this case.
example:
class A
{
void show()
{ }
};
class B : virtual A
{
void disp()
{ }
};
In the above example the size of class B is 8 bytes. which means class B has vptr pointing to a Vtable. What does this vtable point to .
A vtable is the most common way of implementing the virtual keyword in C++ -- any class that uses the virtual keyword will have a vtable created for it and every instance of that class will contain a pointer to that (single) vtable. The vtable contains information on the dynamic class of the object (to support dynamic_cast and typeinfo) as well as information as to where virtual base classes and functions of the class are located.
In this specific case, the vtable for B will likely contain just dynamic class info, as A has no data members or virtual functions.

Is it possible to require the C++ `override` specifier for virtual functions? [duplicate]

This question already has answers here:
How to enforce the 'override' keyword?
(2 answers)
Closed 5 years ago.
Is there any current way, or possibly planned standards change, that would allow me to require an overridden virtual function use the override keyword?
class base {
virtual void foo() = 0; //what, if anything, can I do here so that...
}
class derived {
void foo(); //...this is an error...
void foo() override; //...and only this accepted
}
No, there is no such feature in C++. You could enforce it with a static analysis tool, perhaps.

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

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

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.

What is a good convention (or requirement) for the location of the const and virtual keywords in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I know the virtual keyword does not need to be resused in a derived class member function declaration if it overrides a virtual base function but is it good practice to do so to make it clear that it is virtual? Also, what about the presence of the const keyword in declaration and/or definition? I think Alexandrescu mentions something about this but I can't recall what it was?
Your question seems very confused. virtual is optional when overriding a base-class method. const is never optional if you need it. This does not do what you think it does:
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func();
};
The struct B has two functions named Func. One of them will be called when the object it is called on is const, and the other will be called when it is not const. Nothing in this code has been overridden; these are two separate virtual functions.
You cannot just ignore the const and expect everything to work out fine.
Indeed, this example also shows why you should use virtual when you're overriding in derived classes. In this case, it's fairly obvious that you intended to override a base class function, but you got the function signature wrong. Without the virtual there, there would not be an immediate indication that you intended to override something.
It's not a huge help, but it's something.
C++11 provides a better solution (in that it actually solves the problem) with the override pseudo-keyword.
struct A
{
virtual void Func() const;
};
struct B : public A
{
virtual void Func() override; //Gives a compiler error, since it is not overriding a base class function.
};