Calling the global constructor function
source link: https://www.codesd.com/item/calling-the-global-constructor-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Calling the global constructor function
I have this code:
#include <iostream>
using namespace std;
struct A;
struct B;
void g(A* a){ cout << "A";}
void g(B* b){ cout << "B";}
struct A{
A(){ g(this); }
};
struct B : A{
B(){}
};
int main() {
B* b=new B();
return 0;
}
in which the output is :
Does this mean the type of this
pointer passed to constructor A()
is of type A*
?
The thing is a B
object is also a A
object. While you are inside of the functions of A
the class does not know if it is a B
or not. So the this
-ptr will be of type A*
.
When you are calling functions inside of B
it is B*
.
Related Articles
Strange error - why does the compiler try to call the copy constructor?
What happens if you do not explicitly call the parent constructor in PHP?
Initialize the child before the parent - do not call the parent constructor
Calling the base constructor in C #
Unable to call the supertype constructor directly - why not?
Why does it call the default constructor?
Why does model and non-model overload work with the same signature & rdquo? call the non-model function?
Calling the server-side function of JS using the hidden button
How to write two different routes, but call the same controller function in the 4 rails?
Why does the overload resolution below call the non-template function?
How do I call the copy constructor over a variadic constructor?
Custom Exception: Customize the message before calling the base constructor
Call the parent constructor but keep the C # child reference
When I inherit a structure check, should I call the base constructor?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK