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

查看:36
本文介绍了什么是 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
    }
}

以下示例显示了它的调用位置.

The following example shows where it is called.

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天全站免登陆