Initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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