C++ unique_ptr and polymorphism [duplicate] - c++

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

Related

`virtual` `override` destructor [duplicate]

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.

Program never enters in the virtual destructor [duplicate]

This question already has answers here:
What if the virtual destructor defined in derived class, but not the top of hierarchy? C++
(4 answers)
Closed 3 years ago.
I am just trying to understand some concepts of inheritance/polymorphism and tried to inherit a class from std::string as shown below:
#include <iostream>
using namespace std;
class MyClass : public std::string
{
virtual ~MyClass()
{
cout << "Inside Virutal Destructor" << endl;
}
};
int main()
{
string *s = new MyClass();
delete s;
cout << "Program started" << endl; //NEVER REACHES HERE
}
QUESTION: With the above code, I was expecting a call to the virtual destructor at the line delete s but as soon as the program reaches there, the program enters into some undefined state (i.e. doesn't crash, doesn't go forward). Where is the program stuck?
The standard class template std::basic_string does not have a virtual destructor. So the class does not have a pointer to the destructor in the table of virtual function pointers. As a result the virtual destructor of the derived class will not be called.
Consider the following demonstrative program
#include <iostream>
struct A
{
void f() const { std::cout << "A::f()\n"; }
};
struct B : A
{
virtual void f() const { std::cout << "virtual B::f()\n"; }
};
struct C : B
{
void f() const override { std::cout << "virtual C::f()\n"; }
};
int main()
{
C c;
A *ac = &c;
B *bc = &c;
ac->f();
bc->f();
return 0;
}
Its output is
A::f()
virtual C::f()
Class A has a non-virtual function f.
Class B has a virtual function f that hides the non-virtual function of the class A and does not override it.
Class C overrides the virtual function of the class B.

Inheritance and Overriding - Call Overriding SubMethod from BaseClass [duplicate]

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.

how to call base class method from derive class in c++? [duplicate]

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();
}

virtual function issue

I am using native C++ with VSTS 2008. A quick question about virtual function. In my sample below, any differences if I declare Foo as "virtual void Foo()" or "void Foo()" in class Derived? Any impact to any future classes which will derive from class Derived?
class Base
{
public:
Base()
{
}
virtual void Foo()
{
cout << "In base" << endl;
}
};
class Derived : public Base
{
public:
Derived()
{
}
void Foo()
{
cout << "In derived " << endl;
}
};
No difference. But for the sake of readbility I always keep the virtual whenever it is.
No, as long as it has the same signature as the member function in the base class, it will automatically be made virtual. You should make it explicitly virtual, however, to avoid confusing anyone reading the code.