This question already has answers here:
Is the 'override' keyword just a check for a overridden virtual method?
(6 answers)
Closed 3 years ago.
In the following example:
class A {
public:
virtual ~A() { std::cout << "~A" << std::endl; }
};
class B : public A {
public:
virtual ~B() override { std::cout << "~B" << std::endl; }
};
class C : public B {
public:
~C() override { std::cout << "~C" << std::endl; }
};
clang-tidy gives the following warning for class B:
'virtual' is redundant since this function is already declared 'override'
Removing the virtual keyword from class B seems to allow all destructors in the chain to be called, but I want to make sure that I'm not missing anything.
Removing virtual from function that has override does not change the meaning of the program in any way. That is what it means for the keyword to be redundant (in that context). The removal doesn't allow anything that isn't allowed without the removal.
Related
This question already has answers here:
What's the difference between the Derived class override the base class's virtual function with or not with a "virtual" prefix? [duplicate]
(2 answers)
Closed 5 years ago.
I have three following classes:
class A
{
private:
std::string device;
public:
std::string getDeviceType() { return device; };
void setDeviceType(std::string device) { device = device; };
virtual void doSomething() = 0;
virtual void doSomething2() = 0;
};
class B: public A
{
private:
public:
B(){ ; };
virtual ~B(){ ; };
void doSomething() { std::cout << "I am usual B" << std::endl; };
void virtual doSomething2() { std::cout << "I am usual B" << std::endl; };
};
class C : public B
{
private:
public:
C(){ ; };
~C(){ ; };
void doSomething() { std::cout << "I am C" << std::endl; };
void doSomething2() { std::cout << "I am C" << std::endl; };
};
main:
B *myHandler = new C();
myHandler->doSomething();
myHandler->doSomething2();
but output is not as expected, my expected output was I am usual B and then I am C, because doSomething() is a non virtual member of class B. But the real output was I am C and then I am C. Do you know why?
because of doSomething() is non virtual member of class B
This is where you are mistaken. In A you declare doSomething() as virtual. That means that it is implicitly marked virtual in classes that derive from it. So doSomething() in B is virtual which means you will call C's doSomething().
The reason is that doSomething is marked as virtual in class A. So it remains virtual in classes B and C because they inherit from class A.
As this function is virtual, it is called according to the real type of the object, which is C in your case, and you get the output: I am C.
Once marked virtual, it remains virtual in all derived classes.
In C you overrode both doSomething() and doSomething2(). You instantiate C, so the methods of C are called in both cases.
If you omitted the override in C of doSomething(), the output would be as you expected it.
KR,
Melle
This question already has answers here:
Calling a virtual function from the constructor
(3 answers)
Closed 5 years ago.
#include <iostream>
class Base {
public:
virtual void method() {
std::cout << "Base" << std::endl;
}
Base() {
method();
}
};
class Sub : public Base {
public:
virtual void method() {
std::cout << "Sub" << std::endl;
}
Sub() : Base() {
}
};
int main(void) {
Base *b = new Sub();
delete b;
system("PAUSE");
return 0;
}
Output: "Base"
What do i have to change to make the Base call the Sub Method instead of the Base?
This is probably a duplicate and a beginner-question, but i couldn't find an answer to this problem.
Also a suggestion for a better title is welcome as the current one might be wrong.
a) because the standard says so.
b) philosophically, because Sub at this point has not had its constructor called (inherited objects are constructed in a depth-first manner), so calling Subs version of method would surprise the author of Sub.
This question already has answers here:
Is the 'override' keyword just a check for a overridden virtual method?
(6 answers)
Closed 7 years ago.
Consider the following little program:
#include <iostream>
class Base {
public:
virtual void MyFunction() const {
std::cout << "This is Base" << std::endl;
}
};
class Derived : public Base {
public:
virtual void MyFuntcion() {
std::cout << "This is Derived" << std::endl;
}
};
int main() {
Base *p = new Derived();
p->MyFunction();
return 0;
}
it compiles cleanly with g++ -Wall -Wextra with nary a peep from the compiler, but when you run it, it prints "This is Base", due to the typo in the function name in Derived.
Now in java or C#, if you put an #override tag on the function, you'll get a warning from the compiler about the typo. Is there any way to do something similar with gcc? Perhaps a magic __attribute__ or some such?
C++11 introduced override as well
class Derived : public Base {
public:
void MyFuntcion() override {
std::cout << "This is Derived" << std::endl;
}
};
test.cpp:12:10: error: 'void Derived::MyFuntcion()' marked 'override', but does not override
void MyFuntcion() override {
This question already has answers here:
When to use virtual destructors?
(20 answers)
Closed 8 years ago.
Maybe there is the same question but I haven't found it.
I have following code:
class MBase {
public:
~MBase() { cout << "Base destructor\n" << endl; }
};
class MF: public MBase {
public:
~MF() { cout << "MF Destructor" << endl; }
};
class MS: public MBase {
public:
~MS() { cout << "MS Destructor" << endl;}
};
int main() {
unique_ptr<MBase> ptr1 = unique_ptr<MF>(new MF());
unique_ptr<MBase> ptr2 = unique_ptr<MS>(new MS());
return 0;
}
And there is output
Base destructor
Base destructor
Isn't it right? Why derived destructors weren't called? What I have to do to fix that?
Polymorphism in C++ requires virtual destructor, so that it would be possible to delete an instance
of derived class via a pointer to the base class object.
Long story short - allways declare destructors virtual if class is supposed to be derived from.
When to use virtual destructors?
when using polymorphism and inheritance in C++, you should have a virtual destructor
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to call an object's base class method that's overriden? (C++)
First question is calling super() constructor in java same as initializing the super class constructor first in c++ like.
sub() : super(){}
is there a way to call super class method in c++ like in java
ex.
public sub(){
super.someMethod();
}
To call the base constructor of a class, you call it as BaseClassName(args). For example:
class A
{
public:
A() { }
virtual void Foo() { std::cout << "A's foo" << std::endl; }
};
class B : public A
{
public:
B() : A() { }
void Foo();
};
To call the base class version of a method, you do BaseClassName::MethodName:
void B::Foo()
{
std::cout << "B's foo" << std::endl;
A::Foo();
}