I have the following piece of code and there are a couple of things that bother me the most:
- At what point in time does original constructor call happen?
- Under which circumstances, the copy constructor is getting called?
#include <iostream>
#include <functional>
class Samp {
char str[10] = "Default";
public:
Samp(char s[]) { strcpy(str, s); printf("Constructor\n"); }
Samp(const Samp& s) { printf("Copy Constructor\n"); }
~Samp() { printf("Destructor\n"); }
char* out() {
return str;
}
friend std::ostream& operator<<(std::ostream& stream, const Samp& s) {
stream << s.str;
return stream;
}
};
void output(Samp s) {
std::cout << s << std::endl;
}
int main(int argc, const char * argv[]) {
std::function<void(Samp s)> o;
o = output;
o((char*)"Hello");
return 0;
}
On execution, the program provides the following output
Constructor
Copy Constructor
Default
Destructor
Destructor
Program ended with exit code: 0
