Skip to content

Instantly share code, notes, and snippets.

@jitpaul
Last active April 6, 2020 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitpaul/6942e0591dbd99e572ddc32fdf0c21ff to your computer and use it in GitHub Desktop.
Save jitpaul/6942e0591dbd99e572ddc32fdf0c21ff to your computer and use it in GitHub Desktop.
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