简体   繁体   中英

Ctor-initializer for a class doesn't call to the appropriate constructor

I'm playing with construting/destructing object. Here is what I've tried http://coliru.stacked-crooked.com/a/ff17cc5649897430 :

#include <iostream>

struct B{
    B(){ std::cout << "B()" << std::endl; }
    B(int){ std::cout << "B(int)" << std::endl; }
};

struct A : virtual B
{
    int B;
    A(int a) : B(a) { std::cout << "A(int)" << std::endl; }
} a(10);

int main()
{
}

The program output is

B()
A(int)

Why? I explicitly specify the constructor of the class B to be invoked in the ctor-initializer .

The B(a) is constructing the B member variable. Name your variables better and you'll see what you want to see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM