通过构造函数传递和存储一个const引用? [英] Passing and storing a const reference via a constructor?

查看:157
本文介绍了通过构造函数传递和存储一个const引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但我坚持。我试图通过它的构造函数将对象从ObjectA传递给ObjectB(它是ObjectA的成员)。但是,我不想传递值,而是传递一个const引用,并无限期地存储该引用。

我可以使用指针得到这个工作

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

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



这样,一个对象被创建并传递一个指向int的指针,该指针存储在类中供以后使用。



但是,指针使我在使用较大的对象时担心内存泄漏和其他问题,因此我想使用常量引用来尝试这样做(const&)。但是,这似乎不工作...

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

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

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



这是否可能?还是有更好的/更清楚的方式做这样的事情?我想使用常量引用的原因是传递一个引用而不是一个大对象的速度,同时保持数据安全,防止意外的更改和内存泄漏...我确信有一个简单和直接的方法这样做,但我不太熟悉const引用传递。

解决方案

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

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

如上所述,必须立即初始化引用。因此,如果您希望引用是类成员,则使用您的 构造函数的初始化列表 来设置它。



(此简短说明)也可能使事情更清楚



祝你好运


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.

I can get this working with pointers like 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.

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...

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.

解决方案

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

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

As you said, a reference has to be initialized immediately. Thus, if you want your reference to be a class member, you have to use your constructor's initialization list to set it.

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

Good luck

这篇关于通过构造函数传递和存储一个const引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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