Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 編譯指令:g++ test.cpp -fno-elide-constructors
- /* 執行結果:
- james732@james:~$ ./a.out
- default constructor
- copy constructor
- copy constructor
- ---------------
- default constructor
- copy constructor
- copy assignment
- default constructor
- copy constructor
- */
- #include <iostream>
- using namespace std;
- class Foo {
- public:
- Foo() {
- cout << "default constructor" << endl;
- }
- Foo( const Foo & rh ) {
- cout << "copy constructor" << endl;
- }
- Foo operator=( const Foo & rh ) {
- cout << "copy assignment" << endl;
- Foo f;
- return f;
- }
- };
- Foo do_something ( ) {
- Foo f1;
- return f1;
- }
- int main()
- {
- Foo f3 = do_something( );
- cout << "---------------" << endl;
- f3 = do_something( ); /* 這行才會引發 assignment */
- }
Add Comment
Please, Sign In to add comment