C++ 通过构造函数传递和存储常量引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14169584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:03:35  来源:igfitidea点击:

Passing and storing a const reference via a constructor?

c++pointersreferenceconstants

提问by MrKatSwordfish

This is probably a simple question, but I'm stuck on it. I'm trying to pass an object down from ObjectA to ObjectB (which is a member of ObjectA) via it's constructor. However, instead of passing by value, I want to pass only a const reference and store that reference indefinitely. The problem is that I'm not sure how to do it.

这可能是一个简单的问题,但我一直坚持下去。我试图通过它的构造函数将对象从 ObjectA 传递到 ObjectB(它是 ObjectA 的成员)。但是,我不想通过值传递,而是只想传递一个常量引用并无限期地存储该引用。问题是我不知道该怎么做。

I can get this working with pointerslike this:

我可以使用这样的指针来实现它:

class ClassB
{
private:
    int *ptrInternalX;
public:
    ClassB( int *tempX );
}

ClassB::ClassB( int *tempX )
{
    ptrInternalX = tempX
}

This way, an object is created and passed a pointer to an int, and that pointer is stored inside the class for later use.

通过这种方式,创建了一个对象并传递了一个指向 int 的指针,该指针存储在类中供以后使用。

However, pointers make me worry about memory leaks and other issues when using larger objects, so I'd like to try to do something like this using 'constant references'(const &). However, this doesn't seem to work...

然而,指针让我在使用较大的对象时担心内存泄漏和其他问题,所以我想尝试使用“常量引用”(const &)来做这样的事情。然而,这似乎不起作用......

class ClassB
{
private:
    int &internalX;
public:
    ClassB( const int &tempX );
}

ClassB::ClassB( const int &tempX )
{
    internalX = tempX
}

I know that references are essentially an 'alias' for an existing variable (a different name that refers to the same memory address), and they need to be initialized immediately using an existing variable. So this creates an error in my program!

我知道引用本质上是现有变量(引用相同内存地址的不同名称)的“别名”,它们需要立即使用现有变量进行初始化。所以这会在我的程序中产生一个错误!

Is this even possible? Or is there a better/more clear way of doing something like this? The reasons I want to use constant references are the speed of passing just a reference instead of a large object while keeping the data safe from accidental changes and memory leaks... I'm sure that there is a simple and straight-forward way to do this but I'm not very familiar with const reference passing.

这甚至可能吗?或者有没有更好/更清晰的方法来做这样的事情?我想使用常量引用的原因是只传递一个引用而不是一个大对象的速度,同时保持数据免受意外更改和内存泄漏的影响......我相信有一种简单而直接的方法可以这样做,但我对 const 引用传递不是很熟悉。

回答by cmc

class    ClassB
{
    private:
        const int&    internalX;
    public:
        ClassB(const int& tempX);
}

ClassB::ClassB(const int& tempX):
     internalX(tempX)
{
}

As you said, a referencehas to be initialized immediately. Thus, if you want your referenceto be a class member, you haveto use your constructor's initialization listto set it.

正如你所说, areference必须立即初始化。因此,如果您希望自己reference成为类成员,必须使用构造函数的初始化列表来设置它。

(This short explanationmight also make things clearer for you, as it is specifically centered on the situation you've just met)

(这个简短的解释也可能让你更清楚,因为它专门针对你刚刚遇到的情况)

Good luck

祝你好运