| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 433 人关注过本帖
标题:求助:有关复制构造函数与赋值
取消只看楼主 加入收藏
cb0212
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-2-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求助:有关复制构造函数与赋值
#include <iostream>
using namespace std;

class X {
public:
    X() { cout << "X()" << endl; }
    X(int i):a(i)
    {
        cout << "X(int)" << endl;
     }
    X( const X& x)
    {
        cout << "X(const X&)" << endl;
        a=x.a;
    }
    X& operator=(const X& x)
    {
        cout << "X& operator=(const X&)" << endl;
        a=x.a;
        return *this;
    }
private:
int a;
};

void main()
{
    X x1; // Constructor: X()
    X x2(1); // Constructor: X(int)
    X x3(x1); // Copy-constructor: X(const X&)
    X x4=x1; // Copy-constructor: X(const X&)
    x4 = x1;// Assign operator: X& operator=(const X&)
}
为什么倒数第二行调用的是X(const X&),而不是X& operator=(const X&)?


搜索更多相关主题的帖子: class public include private 
2012-03-08 14:05
cb0212
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-2-20
收藏
得分:0 
回复 2楼 lz1091914999
但是我把复制构造函数显示化:
explicit X( const X& x)
    {
        cout << "X(const X&)" << endl;
        a=x.a;
    }
那倒数第二行的就会编译错误,说明你上面说的那两个还是不一样的啊!能说下X x4=x1;到底是怎么个流程的不?
2012-03-08 14:34
快速回复:求助:有关复制构造函数与赋值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018369 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved