This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
main.cpp
#include <iostream>
class BaseClass {
public:
BaseClass() {
init();
}
virtual ~BaseClass() {
deinit();
}
virtual void init() {
std::cout << "BaseClass::init()\n";
}
virtual void deinit() {
std::cout << "BaseClass::deinit()\n";
}
};
class SubClass : public BaseClass {
public:
virtual void init() {
std::cout << "SubClass::init()\n";
}
virtual void deinit() {
std::cout << "SubClass::deinit()\n";
}
};
int main() {
SubClass* cls = new SubClass;
delete cls;
return 0;
}
Why is init() and deinit() not properly overriden and the BaseClasses' methods are called instead of the SubClasses ones? What are the requirements to make it work?
BaseClass::init()
BaseClass::deinit()
Because you are calling a virtual method inside a constructor. While constructing Base class the derived one (SubClass) isn't still constructed, so actually it doesn't still exist.
It's generally a good practice to avoid calling virtual methods inside constructors.
They are overridden just fine.
But you've invoked them from the base constructor, and when the base constructor is executing, the derived part of the object does not yet exist.
So this is a largely a safety feature, and it's mandated by the C++ standard.
Related
This question already has answers here:
Calling child method from parent destructor in C++
(3 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
class Super {
public:
virtual ~Super() {
func();
}
virtual void func() {
cout << "super-class" << endl;
}
};
class Child : public Super {
public:
virtual void func() override {
cout << "child-class" << endl;
}
};
int main(void) {
Super super;
Child child;
return 0;
}
In this code, the default destructor of class Child is calling its super class Super's func() instead of its own overrided func(). And the output was:
super-class
super-class
I've tried this solution below.
#include <iostream>
using namespace std;
class Super {
public:
virtual ~Super() {
func();
}
virtual void func() {
cout << "super-class" << endl;
}
};
class Child : public Super {
public:
virtual ~Child() {
func();
}
virtual void func() override {
cout << "child-class" << endl;
}
};
int main(void) {
Super super;
Child child;
return 0;
}
And the output was:
child-class
super-class
super-class
But is there a way to let class Child automatically call its own func() in its destructor without virtual ~Child()?
By the time a base class destructor has started running, the derived class(es) have already been fully destructed. It (they) no longer exist(s). They cannot be called into. They are EX-objects! (Loosely paraphrasing Herb, paraphrasing the John.)
Level by level, as the object dies, the parts of it that have been destroyed are not left behind; they no longer exist. The type is adjusted accordingly upwards, and virtual calls resolve to the type being destroyed, until nothing is left of that object but a pile of bits.
This question already has answers here:
Changing Function Access Mode in Derived Class
(4 answers)
Closed 3 years ago.
I've been playing around with inheritance and I've tried this code:
#include <iostream>
#include <string>
class Foo
{
public:
virtual void func() = 0;
protected:
virtual void doSum() const = 0;
};
class Bar : public Foo
{
public:
void func() {
doSum();
}
protected:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
};
int main()
{
Foo* ptr = new Bar();
ptr->func();
return 0;
}
So I've also tried replacing the protected keyword in the class Bar with private like this :
private:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
and the code happened to work just the same...
So my question is, is there any difference if I declare a protected method private when implementing a derived class? If so, what are they? Am I even allowed to do this?
So my question is, is there any difference if I declare a protected method private when implementing a derived class?
Yes.
If so, what are they?
That will prevent the next level of derived class from being able to call the derived class's implementation.
class Foo
{
protected:
virtual void doSum() const = 0;
};
class Bar : public Foo
{
private:
void doSum() const
{
std::cout << "hi, i'm doing something" << std::endl;
}
};
class Baz : public Bar
{
public:
void doSum() const
{
//===========================
Bar::doSum(); // NOT ALLOWED
//===========================
}
};
Am I even allowed to do this?
Yes.
So my question is, is there any difference if I declare a protected method private when implementing a derived class?
No. There is no difference. Unfortunately, C++ standard does not impose any requirement on the derived class to place the overriding virtual function within any particular accessibility scope. This means, the base class could declare a virtual method protected, and the derived class could implement the method in public/protected/private scope and the code will still be legal and will work.
This question already has answers here:
Calling virtual functions inside constructors
(15 answers)
Closed 6 years ago.
Why the following example prints "0" and what must change for it to print "1" as I expected ?
#include <iostream>
struct base {
virtual const int value() const {
return 0;
}
base() {
std::cout << value() << std::endl;
}
virtual ~base() {}
};
struct derived : public base {
virtual const int value() const {
return 1;
}
};
int main(void) {
derived example;
}
Because base is constructed first and hasn't "matured" into a derived yet. It can't call methods on an object when it can't guarantee that the object is already properly initialized.
When a derived object is being constructed, before the body of the derived class constructor is called the base class constructor must complete. Before the derived class constructor is called the dynamic type of the object under construction is a base class instance and not a derived class instance. For this reason, when you call a virtual function from a constructor, only the base class virtual function overrides can be called.
Actually, there is a way to get this behavior. "Every problem in software can be solved with a level of indirection."
/* Disclaimer: I haven't done C++ in many months now, there might be a few syntax errors here and there. */
class parent
{
public:
parent( ) { /* nothing interesting here. */ };
protected:
struct parent_virtual
{
virtual void do_something( ) { cout << "in parent."; }
};
parent( const parent_virtual& obj )
{
obj.do_something( );
}
};
class child : public parent
{
protected:
struct child_virtual : public parent_virtual
{
void do_something( ) { cout << "in child."; }
};
public:
child( ) : parent( child_virtual( ) ) { }
};
The question of how it works is a FAQ item.
Summarizing, while class T is being constructed, the dynamic type is T, which prevents virtual calls to derived class function implementations, which if permitted could execute code before the relevant class invariant had been established (a common problem in Java and C#, but C++ is safe in this respect).
The question of how to do derived class specific initialization in a base class constructor is also a FAQ item, directly following the previously mentioned one.
Summarizing, using static or dynamic polymorphism on may pass the relevant function implementations up to the base class constructor (or class).
One particular way to do that is to pass a “parts factory” object up, where this argument can be defaulted. For example, a general Button class might pass a button creation API function up to its Widget base class constructor, so that that constructor can create the correct API level object.
You should not polymorphically call the virtual methods from constructor.
Instead you can call them after construction of object.
Your code can be re written as follows
struct base {
virtual const int value() const {
return 0;
}
base() {
/* std::cout << value() << std::endl; */
}
virtual ~base() {}
};
struct derived : public base {
virtual const int value() const {
return 1;
}
};
int main(void) {
derived example;
std::cout << example.value() << std::endl;
}
The general rule is you don't call a virtual function from a constructor.
In C++, you cannot call a virtual / overriden method from a constructor.
Now, there is a good reason you can do this. As a "best practice in software", you should avoid calling additional methods from your constructor, even non virtual, as possible.
But, there is always an exception to the rule, so you may want to use a "pseudo constructor method", to emulate them:
#include <iostream>
class base {
// <constructor>
base() {
// do nothing in purpouse
}
// </constructor>
// <destructor>
~base() {
// do nothing in purpouse
}
// </destructor>
// <fake-constructor>
public virtual void create() {
// move code from static constructor to fake constructor
std::cout << value() << std::endl;
}
// </fake-constructor>
// <fake-destructor>
public virtual void destroy() {
// move code from static destructor to fake destructor
// ...
}
// </fake-destructor>
public virtual const int value() const {
return 0;
}
public virtual void DoSomething() {
// std:cout << "Hello World";
}
};
class derived : public base {
// <fake-constructor>
public override void create() {
// move code from static constructor to fake constructor
std::cout << "Im pretending to be a virtual constructor," << std::endl;
std::cout << "and can call virtual methods" << std::endl;
}
// </fake-constructor>
// <fake-destructor>
public override void destroy() {
// move code from static destructor to fake destructor
std::cout << "Im pretending to be a virtual destructor," << std::endl;
std::cout << "and can call virtual methods" << std::endl;
}
// </fake-destructor>
public virtual const int value() const {
return 1;
}
};
int main(void) {
// call fake virtual constructor in same line, after real constructor
derived* example = new example(); example->create();
// do several stuff with your objects
example->doSomething();
// call fake virtual destructor in same line, before real destructor
example->destroy(); delete example();
}
As a plus, I recommend programmers to use "struct" for only fields structures, and "class" for structures with fields, methods, constructors, ...
This question already has answers here:
Is there a way to prevent a method from being overridden in subclasses?
(14 answers)
Closed 7 years ago.
How can I enforce that a base class method is not being overridden by a derived class?
If you are able to use the final specifier from C++11 you can prevent derived classes from override that method. (Microsoft compilers appear to support the similar sealed with similar semantics.)
Here's an example:
#include <iostream>
struct base {
// To derived class' developers: Thou shalt not override this method
virtual void work() final {
pre_work();
do_work();
post_work();
}
virtual void pre_work() {};
virtual void do_work() = 0;
virtual void post_work() {};
};
struct derived : public base {
// this should trigger an error:
void work() {
std::cout << "doing derived work\n";
}
void do_work() {
std::cout << "doing something really very important\n";
}
};
int main() {
derived d;
d.work();
base& b = d;
b.work();
}
Here's what I get when I try to compile it:
$ g++ test.cc -std=c++11
test.cc:17:14: error: virtual function ‘virtual void derived::work()’
test.cc:5:22: error: overriding final function ‘virtual void base::work()’
If you make the method non-virtual, derived classes cannot override the method. However, in C++03 a class cannot override a method from a base class, and also prevent further derived classes from overriding the same method. Once the method is virtual, it stays virtual.
Don't make it virtual.
This won't prevent deriving from your class and hiding the function (by providing another member function with the same name). However, if your class is not meant to be derived anyway (no virtual destructor, no virtual member functions), that shouldn't be an issue.
well if you want to keep it public, dont declare it virtual.
EDIT: Srikanth commented wondering about overriding a private member function in a derived class.
class A
{
public:
virtual ~A(){};
void test()
{
foo();
};
private:
virtual void foo()
{
std::cout << "A";
};
};
class B : public A
{
public:
virtual void foo()
{
std::cout << "B";
};
};
void test()
{
B b;
A& a = b;
a.test(); // this calls the derived B::foo()
return 0;
}`
well as far as i know you can't do that on c++, you can try to declare it as private. . find more info on this link http://en.allexperts.com/q/C-1040/prevent-overriding-functions-derived.htm
Short answer: there is no need for that. Long answer you can do some twists, but is it worth it?
This question already has answers here:
Calling virtual functions inside constructors
(15 answers)
Closed 6 years ago.
Why the following example prints "0" and what must change for it to print "1" as I expected ?
#include <iostream>
struct base {
virtual const int value() const {
return 0;
}
base() {
std::cout << value() << std::endl;
}
virtual ~base() {}
};
struct derived : public base {
virtual const int value() const {
return 1;
}
};
int main(void) {
derived example;
}
Because base is constructed first and hasn't "matured" into a derived yet. It can't call methods on an object when it can't guarantee that the object is already properly initialized.
When a derived object is being constructed, before the body of the derived class constructor is called the base class constructor must complete. Before the derived class constructor is called the dynamic type of the object under construction is a base class instance and not a derived class instance. For this reason, when you call a virtual function from a constructor, only the base class virtual function overrides can be called.
Actually, there is a way to get this behavior. "Every problem in software can be solved with a level of indirection."
/* Disclaimer: I haven't done C++ in many months now, there might be a few syntax errors here and there. */
class parent
{
public:
parent( ) { /* nothing interesting here. */ };
protected:
struct parent_virtual
{
virtual void do_something( ) { cout << "in parent."; }
};
parent( const parent_virtual& obj )
{
obj.do_something( );
}
};
class child : public parent
{
protected:
struct child_virtual : public parent_virtual
{
void do_something( ) { cout << "in child."; }
};
public:
child( ) : parent( child_virtual( ) ) { }
};
The question of how it works is a FAQ item.
Summarizing, while class T is being constructed, the dynamic type is T, which prevents virtual calls to derived class function implementations, which if permitted could execute code before the relevant class invariant had been established (a common problem in Java and C#, but C++ is safe in this respect).
The question of how to do derived class specific initialization in a base class constructor is also a FAQ item, directly following the previously mentioned one.
Summarizing, using static or dynamic polymorphism on may pass the relevant function implementations up to the base class constructor (or class).
One particular way to do that is to pass a “parts factory” object up, where this argument can be defaulted. For example, a general Button class might pass a button creation API function up to its Widget base class constructor, so that that constructor can create the correct API level object.
You should not polymorphically call the virtual methods from constructor.
Instead you can call them after construction of object.
Your code can be re written as follows
struct base {
virtual const int value() const {
return 0;
}
base() {
/* std::cout << value() << std::endl; */
}
virtual ~base() {}
};
struct derived : public base {
virtual const int value() const {
return 1;
}
};
int main(void) {
derived example;
std::cout << example.value() << std::endl;
}
The general rule is you don't call a virtual function from a constructor.
In C++, you cannot call a virtual / overriden method from a constructor.
Now, there is a good reason you can do this. As a "best practice in software", you should avoid calling additional methods from your constructor, even non virtual, as possible.
But, there is always an exception to the rule, so you may want to use a "pseudo constructor method", to emulate them:
#include <iostream>
class base {
// <constructor>
base() {
// do nothing in purpouse
}
// </constructor>
// <destructor>
~base() {
// do nothing in purpouse
}
// </destructor>
// <fake-constructor>
public virtual void create() {
// move code from static constructor to fake constructor
std::cout << value() << std::endl;
}
// </fake-constructor>
// <fake-destructor>
public virtual void destroy() {
// move code from static destructor to fake destructor
// ...
}
// </fake-destructor>
public virtual const int value() const {
return 0;
}
public virtual void DoSomething() {
// std:cout << "Hello World";
}
};
class derived : public base {
// <fake-constructor>
public override void create() {
// move code from static constructor to fake constructor
std::cout << "Im pretending to be a virtual constructor," << std::endl;
std::cout << "and can call virtual methods" << std::endl;
}
// </fake-constructor>
// <fake-destructor>
public override void destroy() {
// move code from static destructor to fake destructor
std::cout << "Im pretending to be a virtual destructor," << std::endl;
std::cout << "and can call virtual methods" << std::endl;
}
// </fake-destructor>
public virtual const int value() const {
return 1;
}
};
int main(void) {
// call fake virtual constructor in same line, after real constructor
derived* example = new example(); example->create();
// do several stuff with your objects
example->doSomething();
// call fake virtual destructor in same line, before real destructor
example->destroy(); delete example();
}
As a plus, I recommend programmers to use "struct" for only fields structures, and "class" for structures with fields, methods, constructors, ...