This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
in C++, An object of class B derived from class A, in C++ the c’tor of A is invoked before the c’tor of B , why ?
And what happens if A’s C’tor invokes a virtual function? does it invoke A's virtual function of B's ?
Most of your questions, if not all, are covered by the C++ FAQ.
E.g. see the FAQ "When my base class's constructor calls a virtual function on its this object, why doesn't my derived class's override of that virtual function get invoked?".
It is always a good idea to read the FAQ before asking.
Related
This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 2 years ago.
What is the application of virtual and pure virtual destructor in C++? What is the scenario where I would have to use a virtual destructor instead of a normal destructor?
When a pointer to a base class object is deleted, the compiler calls the corresponding destructor based on the actual type of object the pointer refers to.
If the base class destructor is not a virtual function, the compiler will automatically call the destructor of the base class when the base class pointer to the derived class object is deleted, without considering whether the actual object is an object of the base class.This can lead to memory leaks.
This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 6 years ago.
Suppose I have a base class A and a derived class B. B's constructor calls the constructor of A, in which I call a function, say func to do some type-dependent thing. I mean, I have a do-nothing func for A and override this method in B.
My problem:
In the very phase of construction of B, that is, in the constructor of A, what is the type of the object? While, I think it's A. But I'm not sure. If it is A, I'm always calling A's func right? Regardless of the type of the object I want to construct.
In VS, in B's constructor, I see the type for "this" is "B". While I step into and in A's constructor, I see type for "this" "A".
This is correct. Superclasses are constructed first. Derived classes get constructed only after the superclasses are constructed. Until your superclass A is constructed, none of its virtual methods are overridden, and calling them will invoke A's virtual method. If they are pure and not defined, this results in undefined behavior.
Well doesn't really matter, as long as control is in B's constructor, code will execute as per B's visibility, if you called your func in B's constructor the overrided func in B is visible hence B's func gets executed, now in A's constructor A's func is visible, hence A's func will get executed.
This question already has answers here:
How is "=default" different from "{}" for default constructor and destructor?
(3 answers)
Closed 7 years ago.
When writing a virtual destructor, is there any functional or outwardly-discernible difference between having
virtual ~T() = default;
over
virtual ~T() {}
They both seem to have the same affect on anything from type_traits I could think to test with.
They are effectively the same. While there is a difference with non-virtual destructors, once you stick virtual in there, it cannot be trivial anymore.
This is not the only time that = default leads to the generation of a non-trivial special member function. For example, if you have a member that has a non-trivial destructor, then using = default will not cause the creation of a trivial destructor for the containing type, even with a non-virtual destructor.
You should use = default anyway, just to make it clear what your intentions are.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Do I need to explicitly call the base virtual destructor?
Lets say you have the following:
class ParentClass {
...
virtual ~ParentClass();
and
class ChildClass {
...
virtual ~ChildClass();
Which of the destructors would be called? Would both the parent and child's destructors be called? Currently don't have C++ compilers set up on my computer.
If ChildClass is derived from ParentClass then the derived destructor is called first, followed by the parent class. As it stands in your code, ChildClass does not inherit from ParentClass
Yes, both constructors are call: construction and destruction are symmetric: All subobjects get destroyed in exactly the opposite order they were created. For the order of destruction it doesn't matter if the destructor is virtual. The only impact of virtual vs. non-virtual destructors is when deleteing an object of a dreived type using a pointer to a base: This results in undefined behavior if the destructor of the base isn't virtual.