'Why does a Member initialized list for a default constructor in a composite class not call the member object constructor? [duplicate]
The Member initialized list for a default constructor in a composite class does not call the member object constructor.
#include <iostream>
struct test{
test(){
std::cout << "defualt is called" << std::endl;
}
test(int num){
std::cout <<"parameter is called" << std::endl;
}
};
struct test2{
test a;
test2():a(21){}
};
int main(){
test2 b();
}
Nothing is outputted, but if I change the default constructor in the composite class to a parameterized one then it works as expected
#include <iostream>
struct test{
test(){
std::cout << "defualt is called" << std::endl;
}
test(int num){
std::cout <<"parameter is called" << std::endl;
}
};
struct test2{
test a;
test2(int num):a(21){}
};
int main(){
test2 b(4);
}
output is: parameter is called
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
