C++通过引用传递对象

发布于 2024-10-19 15:35:30 字数 633 浏览 1 评论 0原文

我正在尝试学习 C++,并且正在学习“Sams Teach Yourself C++ in 21 Days”。

到目前为止,我进展得很好,甚至毫无困难地完成了关于指针的章节。然而,“通过引用传递对象”的列表让我很困惑。

有一个类有两个构造函数:

class SimpleCat
{
public :
    SimpleCat();
    SimpleCat(SimpleCat&);
...
};

两个带有原型的函数:

SimpleCat FunctionOne( SimpleCat theCat );
SimpleCat* FunctionTwo( SimpleCat *theCat );

/ 令我困惑的是,当调用第二个函数时,第二个构造函数 SimpleCat(SimpleCat&);被称为。有人可以解释一下吗?任何进一步的搜索都让我同样感到困惑。 /

编辑:我在这里的帖子中犯了一个错误,复制构造函数(因为我现在知道它是什么,非常感谢)是用第一个调用的功能。对于造成的混乱,我深表歉意。我知道现在理解了这个链接,你们都提供了巨大的帮助。

I am trying to learn C++, and I am working through "Sams Teach Yourself C++ in 21 Days".

I have been progressing quite well so far, and even got through the chapter on pointers without difficulty. However, a listing on "Passing Objects by Reference" has left me quite confused.

There is a class with two constructors:

class SimpleCat
{
public :
    SimpleCat();
    SimpleCat(SimpleCat&);
...
};

two functions with the prototype:

SimpleCat FunctionOne( SimpleCat theCat );
SimpleCat* FunctionTwo( SimpleCat *theCat );

/ What is confusing me is that when calling the second function, the second constructor SimpleCat(SimpleCat&); is called. Could someone please explain? Any further searching has left me equally confused. /

EDIT: I have made a mistake in my post here, the copy constructor (as I now know what it is, thank-you so much ) is called with the first function. I am sorry for the confusion. I know understand the link now and you have all helped tremendously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南渊 2024-10-26 15:35:31

引用其自身类型的构造函数称为“复制构造函数”(将在您的书中介绍)。

每当编译器需要复制某种类型的对象时,如果类定义具有复制构造函数,它将使用它,否则它将使用它提供的默认实现。因此,复制构造函数允许您控制类的复制方式。

当您调用 FunctionOne 时,很可能会使用复制构造函数,而不是 FunctionTwo,因为参数是按值传递的。

A constructor that takes a reference to it's own type is called a "copy constructor" (that will be in your book).

Whenever the compiler needs to make a copy of an object of certain type, if the class definition has a copy-constructor it will use that, otherwise it uses a default implementation that it provides. So the copy constructor allows you to take control of how your class is copied.

Most likely your copy constructor is being used when you call FunctionOne, not FunctionTwo because the parameter is being passed by value.

一生独一 2024-10-26 15:35:31

您的 FunctionOne 采用 BY VALUE 对象。
这意味着,每当调用该函数时,都会生成数据类型的副本。这适用于普通数据类型,如 int、char、...,也适用于对象(在您的例子中是 SimpleCat 对象)。
因此,当您有像 void doIt(int a) 这样的函数时,当您调用 doIt(3) 时,通过创建整数的新实例来复制值 3值为 3。
转换为您的 FunctionOne 时,调用 FunctionOne(cat) 将创建您的 cat 对象的副本,从而导致调用复制构造函数。

Your FunctionOne takes an object BY VALUE.
That means, that whenever the function is called, a copy is made of the datatype. This holds for ordinary datatypes like int, char, ..., but also for objects (in your case a SimpleCat object).
So when you have a function like void doIt(int a), then when you call doIt(3), the value 3 is copied by creating a new instance of an integer with the value of 3.
Translated to your FunctionOne, calling FunctionOne(cat) will make a copy of your cat object, resulting in the copy constructor being called.

关于从前 2024-10-26 15:35:30

SimpleCat(SimpleCat&) 是一个复制构造函数SimpleCat FunctionOne(SimpleCat theCat) 使用按值传递语义。这需要复制类实例。因此调用了复制构造函数。

SimpleCat(SimpleCat&) is a copy constructor. SimpleCat FunctionOne(SimpleCat theCat) uses pass by value semantics. This requires that the class instance be copied. Hence the call to the copy constructor.

素食主义者 2024-10-26 15:35:30

我无法重现您所描述的行为。正如您在此处看到的,仅当您调用FunctionOne时才会调用复制构造函数。

编辑直接在此处包含代码,以便更容易阅读。

来源:

#include <iostream>

class SimpleCat {
    public:
        SimpleCat() {
            std::cout << "\tSimpleCat() called\n";
        }

        SimpleCat(SimpleCat&) {
            std::cout << "\tSimpleCat(SimpleCat&) called\n";
        }
};

SimpleCat FunctionOne( SimpleCat theCat ){
    return theCat;
}

SimpleCat* FunctionTwo( SimpleCat* theCatPtr ){
    return theCatPtr;
}

int main() {
    SimpleCat cat;
    std::cout << "-----\n";

    std::cout << "FunctionOne{\n";
    FunctionOne(cat);
    std::cout << "}\n";

    std::cout << "FunctionTwo{\n";
    FunctionTwo(&cat);
    std::cout << "}\n";
}

输出

    SimpleCat() called
-----
FunctionOne{
    SimpleCat(SimpleCat&) called
    SimpleCat(SimpleCat&) called
}
FunctionTwo{
}

I cannot reproduce the behavior you describe. As you can see here the copy-constructor is only called when you call FunctionOne.

edit including the code directly here so that it is easier to read.

source:

#include <iostream>

class SimpleCat {
    public:
        SimpleCat() {
            std::cout << "\tSimpleCat() called\n";
        }

        SimpleCat(SimpleCat&) {
            std::cout << "\tSimpleCat(SimpleCat&) called\n";
        }
};

SimpleCat FunctionOne( SimpleCat theCat ){
    return theCat;
}

SimpleCat* FunctionTwo( SimpleCat* theCatPtr ){
    return theCatPtr;
}

int main() {
    SimpleCat cat;
    std::cout << "-----\n";

    std::cout << "FunctionOne{\n";
    FunctionOne(cat);
    std::cout << "}\n";

    std::cout << "FunctionTwo{\n";
    FunctionTwo(&cat);
    std::cout << "}\n";
}

output:

    SimpleCat() called
-----
FunctionOne{
    SimpleCat(SimpleCat&) called
    SimpleCat(SimpleCat&) called
}
FunctionTwo{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文