5

Calling the global constructor function

 2 years ago
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.
neoserver,ios ssh client

Calling the global constructor function

advertisements

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*.

Tags oop

Related Articles

Strange error - why does the compiler try to call the copy constructor?

I'm getting some very wierd errors. The compiler seems to want to call the copy constructor for some reason I don't understand. (118) std::map<int, layer> xs; (119) xs.begin()->first; // error?! layer is a non-copyable, movable type. class layer

What happens if you do not explicitly call the parent constructor in PHP?

I was wondering why we have to explicitly call parent constructor in PHP? What happen if, let's say, we forget to call the parent constructor in the derived class? I thought that the derived class won't have access to the member properties or method

Initialize the child before the parent - do not call the parent constructor

In MatLab, i have a superclass A that takes some parameters, x and y classdef A < handle properties x; y; z; end methods function this = A(x, y) this.x = x; this.y = y; this.initialize(); end function initialize(this) this.z = this.x + this.y; end en

Calling the base constructor in C #

This question already has an answer here: Calling the base constructor in C# 10 answers I have the following hierarchy: class Base { public Base(string sMessage) { //Do stuff } } class Derived : Base { public Derived(string someParams) { string sMess

Unable to call the supertype constructor directly - why not?

I have the following example classes in Java: public class A { } public class Super { protected Super() { } public Super(A a) { } } public class Sub extends Super { } public class Consumer { public Consumer() { Sub sub = new Sub(new A()); //compiler

Why does it call the default constructor?

struct X { X() { std::cout << "X()\n"; } X(int) { std::cout << "X(int)\n"; } }; const int answer = 42; int main() { X(answer); } I would have expected this to print either X(int), because X(answer); could be interpreted as

Why does model and non-model overload work with the same signature & rdquo? call the non-model function?

I have this code: template< class T = const int & > void f(T) {} void f(const int &) {} int main() { f(0); } Why does it call the second one instead of first? I would think of them as being the same but they're clearly not as I do not get a

Calling the server-side function of JS using the hidden button

I've came into a need of calling a server-side function from a javscript function. As much as I know, this isn't really possible since JS is running on the client, thus it is only possible to call a static function (which is not in the same context).

How to write two different routes, but call the same controller function in the 4 rails?

There two different routes but it calling the same controller function. two possible routes: 1) ios-mobile-developer-india 2) salesforce-consultant-india we are technology and location dynamically. routes.rb: get ':tech-mobile-developer-:loc' => appl

Why does the overload resolution below call the non-template function?

This question already has an answer here: Why does overload of template and non-template function with the "same signature" call the non-template function? 2 answers Why the overload resolution for the call max(x, y) in the expression return max

How do I call the copy constructor over a variadic constructor?

In the following code, the variadic constructor is called twice. How can I get the copy constructor to be called instead of the single argument version of the variadic constructor when appropriate? #include <iostream> struct Foo { Foo(const Foo &

Custom Exception: Customize the message before calling the base constructor

I defined this custom exception public class ThirdPartyServiceException : System.Exception { public ThirdPartyServiceException(string message, Exception innerException): base(message, innerException) { } } That's fine, but now I want customize the ex

Call the parent constructor but keep the C # child reference

I am calling the base constructor but somehow I need to point to the child one back. Take a look at the example bellow: //Child public CompanyEventsView(IAggregator aggregator, IRepository<CompanyEvents> repository, int i) : base(aggregator, reposit

When I inherit a structure check, should I call the base constructor?

I'm inheriting from a ListBox. Do I need to explicitly call the base constructor? public class MyListBox : ListBox { public MyListBox() : base() { } // or public MyListBox() { } } The two options you listed will compile to identical code. The only re

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK