Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include <iostream>
- static int count;
- class A{
- public:
- int c;
- A(){c = ++count; cout << "A() " << c << endl;}
- A(int j){c = ++count; cout << "A(int) " << c << endl;}
- ~A(){cout << "~A() " << c << endl;}
- };
- class B{
- public:
- A a;
- B(){
- cout << "This is B's constructor" << endl;
- A a(); //This is the important line.
- }
- };
- int main(){
- B b;
- cout << "The object we keep is " << b.a.c << endl;
- }
- A() 1
- This is B's constructor
- The object we keep is 1
- ~A() 1
- A() 1
- This is B's constructor
- A(int) 2
- ~A() 2
- The object we keep is 2
- ~A() 2
- A a(); //This is the important line.
- A a{}; // *this* is a variable declaration
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement