64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class AA{
public:
AA(int _first, int _second = 1) : first(_first), second(_second){
cout << "AA:: first = " << _first << " second = " << _second << endl;
}
~AA(){}
AA(const AA & aa){
first = aa.first;
second = aa.second;
cout << "AAAAAA" << endl;
}
AA & operator=(const AA & aa){
first = aa.first;
second = aa.second;
cout << "operator" << endl;
}
int getFirst() const{
return first;
}
int getSecond() const{
return second;
}
private:
int first;
int second;
};
const AA aaaa(const AA &a, const AA &b){
return AA(a.getFirst() * b.getFirst(), a.getSecond() * b.getSecond());
}
int main(){
AA aa(1, 2);
AA temp = aaaa(2, aa);
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class A{
public:
A(string _name, char _sex, int _age) : name(_name), sex(_sex), age(_age){
cout << "A::" << endl;
}
~A(){
cout << "~A::" << age << endl;
}
A(const A& a){
cout << "AAAA" << endl;
name = a.name;
sex = a.sex;
age = a.age;
}
A& operator=(const A &a){
cout << "operator" << endl;
}
void print(){
cout << name << endl;
cout << sex << endl;
cout << age << endl;
}
private:
string name;
char sex;
int age;
};
A getA(){
return A("wyp", 'm', 24);;
}
int main(){
A a = getA();
return 0;
}
AA:: first = 1 second = 2
AA:: first = 2 second = 1
AA:: first = 2 second = 2
A::
AAAA
~A::24
~A::24
int main()
{
AA aa(1, 2);
AA a = aa; //这样就可以输出AAAAAA
AA temp = aaaa(AA(2), aa);
return 0;
}
AA:: first = 1 second = 2 //调用AA(1,2)构造函数
AA:: first = 2 second = 1 //对于aaaa(2,aa),等价于aaaa(AA(2,1(默认参数) ),aa),aaaa函数返回值为 //AA()的构造函数
AA:: first = 2 second = 2//而AA temp = aaaa(2, aa);等价于AA temp = AA(2,2) 用构造函数为其初始化,因此也没有用到复制构造函数
const AA aaaa(const AA &a, const AA &b){
return AA(a.getFirst() * b.getFirst(), a.getSecond() * b.getSecond());
}
的行为,我现在解释不清,希望后面的高手详细解释一下,顶起! explicit AA(int _first, int _second = 1, string _name = "XXXX") : first(_first), second(_second), name(_name){
}
#include <iostream>
using namespace std;
class AA{
public:
AA(int _first, int _second = 1) : first(_first), second(_second){
cout << "AA:: first = " << _first << " second = " << _second << endl;
}
~AA(){}
AA(const AA & aa){
first = aa.first;
second = aa.second;
cout << "AAAAAA" << endl;
}
AA & operator=(const AA & aa){
first = aa.first;
second = aa.second;
cout << "operator" << endl;
}
int getFirst() const{
return first;
}
int getSecond() const{
return second;
}
private:
int first;
int second;
};
const AA aaaa(const AA &a, const AA &b){
return AA(a.getFirst() * b.getFirst(), a.getSecond() * b.getSecond());//2,2
}
int main(){
AA aa(1, 2);//1,2
AA temp = aaaa(2, aa);//2,1
return 0;
}