T
TechCrazy
I am compiling like
g++ -O0 MyString.cpp
Should the copy cnstrs be called thrice? Any optimization going on
here?
--------------------------
#include <iostream>
#include <string>
using namespace std;
class MyString: public string {
public:
MyString(const char * str): string(str) {
cout << "copy ctr 1 called" << endl;
}
MyString(MyString const & str): string(str) {
cout << "copy ctr 2 called" << endl;
}
};
MyString getString() {
MyString x("rrr"); //copy ctr 1 called
//This should call copy ctr once to create the temporary
//that will be returned
return x;
}
int main() {
//This should call copy ctr again with the temporary
//created at the end of getString() to create y
MyString y (getString());
}
g++ -O0 MyString.cpp
Should the copy cnstrs be called thrice? Any optimization going on
here?
--------------------------
#include <iostream>
#include <string>
using namespace std;
class MyString: public string {
public:
MyString(const char * str): string(str) {
cout << "copy ctr 1 called" << endl;
}
MyString(MyString const & str): string(str) {
cout << "copy ctr 2 called" << endl;
}
};
MyString getString() {
MyString x("rrr"); //copy ctr 1 called
//This should call copy ctr once to create the temporary
//that will be returned
return x;
}
int main() {
//This should call copy ctr again with the temporary
//created at the end of getString() to create y
MyString y (getString());
}