Created
October 13, 2015 21:28
-
-
Save jsbattig/a6270fd92f4bedb52b45 to your computer and use it in GitHub Desktop.
Example in C++ of constructor, copy constructor and assignment operator calling
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 <stdio.h> | |
#include <string> | |
using namespace std; | |
class myString { | |
static int globalCounter; | |
string str; | |
public: | |
int counter; | |
myString(const myString& s) : str(s.str), counter(s.counter) { | |
printf("myString created using copy constructor\r\n"); | |
print(); | |
}; | |
myString(const string& s) : str(s), counter(globalCounter++) { | |
printf("myString created\r\n"); | |
print(); | |
}; | |
~myString() { printf("myString destroyed\r\n"); }; | |
myString& operator=(const myString& s) { | |
str = s.str; | |
counter = s.counter; | |
printf("myString operator= called\r\n"); | |
return *this; | |
}; | |
void print() { | |
string msg = str; | |
msg += " "; | |
msg += to_string(static_cast<long long>(counter)); | |
msg += " my address: "; | |
msg += to_string(reinterpret_cast<long long>(this)); | |
msg += "\r\n"; | |
printf(msg.c_str()); | |
}; | |
}; | |
int myString::globalCounter = 0; | |
myString fn(const char* str) { | |
return myString(str); | |
} | |
void iReceiveAMyString(const myString) { | |
printf("iReceiveAMyString called\r\n"); | |
} | |
void main(){ | |
printf("vvvvvvv\r\n"); | |
{ | |
printf("Example with only one copy of myString class...\r\n"); | |
myString str = fn("hello"); | |
str.print(); | |
} // str will be destroyed when leaving scope, right here | |
printf("^^^^^^^\r\n"); | |
printf("vvvvvvv\r\n"); | |
{ | |
printf("Even invoking as a constructor creates only one copy...\r\n"); | |
myString str(fn("hello")); | |
str.print(); | |
} // str will be destroyed when leaving scope, right here | |
printf("^^^^^^^\r\n"); | |
printf("vvvvvvv\r\n"); | |
{ | |
printf("When really copying into an uninitialized object copy constructor is finally used...\r\n"); | |
myString str(fn("hello")); | |
printf("now we will assign str to str2\r\n"); | |
myString str2 = str; | |
str.print(); | |
str2.print(); | |
} // str and str2 will be destroyed when leaving scope, right here | |
printf("^^^^^^^\r\n"); | |
printf("vvvvvvv\r\n"); | |
{ | |
printf("What happens if we invoke function that doesn't use & operator?...\r\n"); | |
myString str(fn("hello")); | |
iReceiveAMyString(str); | |
} // str will be destroyed when leaving scope, right here | |
printf("^^^^^^^\r\n"); | |
printf("vvvvvvv\r\n"); | |
{ | |
printf("When overwriting an object, assignment operator will be called...\r\n"); | |
myString str(fn("hello A")); | |
myString str2(fn("hello B")); | |
printf("let's print str2\r\n"); | |
str2.print(); | |
printf("now we will assign str to str2\r\n"); | |
str2 = str; | |
printf("let's print str and after that str2\r\n"); | |
str.print(); | |
str2.print(); | |
} // str and str2 will be destroyed when leaving scope, right here | |
printf("^^^^^^^\r\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment