64,661
社区成员
发帖
与我相关
我的任务
分享
char *buf = "abcdefg";
string s1(buf);
string s2 = s1.substr(0,3);
# include <iostream>
using namespace std;
class C{
public:
C(){n=mark++; cout<<"default construct "<<n<<endl;}
C(const C& c){n = mark++; cout << "copy construct " << c.n << " to "<<n<< endl;}
~C(){cout << "destruct " << n << endl;}
void operator= (const C& c){cout << "asign " << c.n << " to " << n << endl; /*n = c.n;*/}//没有变n的值
private:
int n;
static int mark;
};
int C::mark = 0;
C f(){
C c;
return c;
}
int main(){
C c0=f(); //equals to c0(f())
cout<<"-----------first variable builded--------------"<<endl;
c0 = f();
return 0;
}
# include <iostream>
using namespace std;
class C{
public:
C(int i):n(i){cout << "Construct C" << i << endl;}
C(const C& c){n = c.n; cout << "Construct _C" << c.n << endl;}
~C(){cout << "Destruct C" << n << endl;}
void operator= (const C& c){cout << "Copy from C" << c.n << " to " << "C" << n << endl; /*n = c.n;*/}//没有变n的值
private:
int n;
};
C f(int i){
C c(i);
return c;
}
int main(){
C c0 = f(0);
c0 = f(1);
//system("PAUSE");
return 0;
}
Construct C0
Construct _C0
Destruct C0
Construct C1
Construct _C1
Destruct C1
Copy from C1 to C0
Destruct C1
Destruct C0
Press any key to continue
# include <iostream>
using namespace std;
class C{
public:
C(int i):n(i){cout << "Construct C" << i << endl;}
C(const C& c){n = c.n; cout << "Construct _C" << c.n << endl;}
~C(){cout << "Destruct C" << n << endl;}
C& operator= (const C& c){cout << "Copy from C" << c.n << " to " << "C" << n << endl; /*n = c.n;*/}//没有变n的值
private:
int n;
};
C f(int i){
C c(i);
return c;
}
int main(){
C c0 = f(0);
c0 = f(1);
system("PAUSE");
return 0;
}