Skip to content

Instantly share code, notes, and snippets.

@jitpaul

jitpaul/initialization1.cpp Secret

Last active Apr 6, 2020
Embed
What would you like to do?
Initialization
#include <iostream>
class B{
public:
int a;
B():a(1){}
};
class A{
public:
B b;
A(B b):b(b){}
};
int main(){
A obj1(B());
std::cout<< obj1.b.a;
// This might be a compiler error because obj1 gets
// interpreted as a declaration of a method.
A obj2( (B()) );
std::cout<< obj2.b.a;
// With the extra set of parenthesis, this is correctly
// interpreted as the creation of an object of class A;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment