什么是C ++中的复制构造函数? [英] What is a copy constructor in C++?

查看:222
本文介绍了什么是C ++中的复制构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scott Meyers的 Effective C ++ 第6页,定义了复制构造函数。我一直使用Schiltdt的书作为我的参考,我可以找到没有提到的复制构造函数。我得到的想法,但这是c ++的标准部分?复制构造函数的形式如下:

On page 6 of Scott Meyers's Effective C++, the term 'copy constructor' is defined. I've been using Schiltdt's book as my reference and I can find no mention of copy constructors. I get the idea but is this a standard part of c++? Will such constructors get called when a pass a class by value?

推荐答案



A copy constructor has the following form:

class example 
{
    example(const example&) 
    {
        // this is the copy constructor
    }
}

void foo(example x);

int main(void)
{
    example x1; //normal ctor
    example x2 = x1; // copy ctor
    example x3(x2); // copy ctor

    foo(x1); // calls the copy ctor to copy the argument for foo

}

这篇关于什么是C ++中的复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆