使用类型转换运算符存储指向类实例的指针 [英] Storing pointer to instance of class with type conversion operator

查看:62
本文介绍了使用类型转换运算符存储指向类实例的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我对类型转换运算符有疑问。让我们说我

有一个简单(荒谬)的课程如下:


class MakeNewInt

{

私人:

int * _n;


//顺便说一下,由于类型转换操作符,一个

//编译器可能无法捕获意外删除的

//堆栈实例 - 即删除MakeNewInt(123)。 - 所以,

//添加一个私人操作员void *()声明。

operator void *();


public :

operator int *()const

{

返回_n;

}

MakeNewInt(int n):_ n(new int(n)){}

~MakeNewInt()

{

delete _n;

}

};


我们也说我的方法如下:


void f(int * n)

{

int * q = n;

//可以做其他带q的东西...

}


如果我然后在main()中调用f()......


int main()

{

f(MakeNewInt(123)); //好的

返回0;

}


....一切都很好;在f()里面,我可以操作指针q,

知道MakeNewInt()的析构函数不会被调用,直到我

退出f()。


但是,如果我调用MakeNewInt并尝试将结果存储在

局部变量中,那么......


int main()

{

int * n = MakeNewInt(123); // n是垃圾

返回0;

}


.... MakeNewInt()的析构函数已经被我一打电话就叫
到达return 0;线,因此指针n指向垃圾。

我能做些什么来让调用者以这种方式使用我的MakeNewInt类




谢谢!

Whitney Kew

软件工程师

Hello,

I have a question regarding type conversion operators. Let''s say I
have a simple (nonsensical) class as follows:

class MakeNewInt
{
private:
int* _n;

// By the way, because of the type conversion operator, a
// compiler might not catch an accidental deletion of a
// stack instance - i.e., "delete MakeNewInt(123)" - so,
// add a private operator void*() declaration.
operator void*();

public:
operator int*() const
{
return _n;
}
MakeNewInt(int n) : _n(new int(n)) {}
~MakeNewInt()
{
delete _n;
}
};

Let''s also say that I have a method as follows:

void f(int* n)
{
int* q = n;
// Can do other things with q...
}

If I then invoke f() in a main() as such....

int main()
{
f(MakeNewInt(123)); // OK
return 0;
}

....everything is fine; inside f(), I can operate on the pointer q,
knowing that the destructor of MakeNewInt() won''t be called until I
exit f().

If, however, I invoke MakeNewInt and attempt to store the result in a
local variable, as such....

int main()
{
int* n = MakeNewInt(123); // n is garbage
return 0;
}

....the destructor of MakeNewInt() has already been called as soon as I
reach the "return 0;" line, and thus the pointer n points to garbage.
Is there anything I can do to allow a caller to use my MakeNewInt class
in this fashion?

Thanks!
Whitney Kew
Software Engineer

推荐答案

Whitney Kew写道:
Whitney Kew wrote:
我对类型转换运算符有疑问。让我们说我有一个简单的(荒谬的)课程如下:

类MakeNewInt
{
私人:
int * _n; <顺便说一下,由于类型转换运算符,
//编译器可能无法捕获意外删除的
//堆栈实例 - 即删除 MakeNewInt(123)" - 所以,
//添加一个私有操作符void *()声明。
operator void *();

public:
operator int *()const <
返回_n;
}
MakeNewInt(int n):_ n(new int(n)){}
~MakeNewInt()
{
删除_n;
}
};

我们也说我的方法如下:

void f( int * n)
{
int * q = n;
//可以用q做其他事情......

如果我那么在main()中调用f()......

int main()
{
f(MakeNewInt(123)); //好的
返回0;
}

......一切都很好;在f()中,我可以操作指针q,
知道MakeNewInt()的析构函数不会被调用,直到我退出f()。

但是,如果我调用MakeNewInt并尝试将结果存储在
局部变量中,那么......

int main()
{
int * n = MakeNewInt(123); // n是垃圾
返回0;
}

... MakeNwInt()的析构函数一旦我到达返回0;线,因此指针n指向垃圾。
我能做些什么来让调用者以这种方式使用我的MakeNewInt类?
I have a question regarding type conversion operators. Let''s say I
have a simple (nonsensical) class as follows:

class MakeNewInt
{
private:
int* _n;

// By the way, because of the type conversion operator, a
// compiler might not catch an accidental deletion of a
// stack instance - i.e., "delete MakeNewInt(123)" - so,
// add a private operator void*() declaration.
operator void*();

public:
operator int*() const
{
return _n;
}
MakeNewInt(int n) : _n(new int(n)) {}
~MakeNewInt()
{
delete _n;
}
};

Let''s also say that I have a method as follows:

void f(int* n)
{
int* q = n;
// Can do other things with q...
}

If I then invoke f() in a main() as such....

int main()
{
f(MakeNewInt(123)); // OK
return 0;
}

...everything is fine; inside f(), I can operate on the pointer q,
knowing that the destructor of MakeNewInt() won''t be called until I
exit f().

If, however, I invoke MakeNewInt and attempt to store the result in a
local variable, as such....

int main()
{
int* n = MakeNewInt(123); // n is garbage
return 0;
}

...the destructor of MakeNewInt() has already been called as soon as I
reach the "return 0;" line, and thus the pointer n points to garbage.
Is there anything I can do to allow a caller to use my MakeNewInt class
in this fashion?




int * const& n = MakeNewInt(123);


这里的对象绑定到const的引用,并且存在为

,只要引用存在。


V



int * const & n = MakeNewInt(123);

The object here is bound to a reference to const and shall exist for as
long as the reference exists.

V


不幸的是,这不起作用; n指向语句完成时指向

垃圾的指针。另外,我不希望

来电者必须记住这样的事情,因为我不会对b
必然能够控制来电者所写的内容。我希望

有一些方法我可以修改MakeNewInt类,而

保留了类型转换操作符功能,这样一个来电者

可以安全地拨打以下电话:

int * n = MakeNewInt(123);


谢谢,

惠特尼

Unfortunately, that doesn''t work; n points to a pointer that points to
garbage as soon as the statement completes. Plus, I don''t want the
caller to have to remember something like that, since I don''t
necessarily have control over what the caller writes. I was hoping
that there was some way that I could modify the MakeNewInt class, while
preserving the type conversion operator functionality, so that a caller
could safely make the following call:
int* n = MakeNewInt(123);

Thanks,
Whitney


Whitney Kew写道:
Whitney Kew wrote:
不幸的是,这不起作用; n指向语句完成时指向
垃圾的指针。


然后你的编译器可能坏了。绑定到const引用应该

保持临时性超过声明声明AFAIUI。


尝试


int * const& n(MakeNewInt(123));


虽然AFAIK它不应该有任何区别。

加上,我不想要 int * n = MakeNewInt(123);
Unfortunately, that doesn''t work; n points to a pointer that points to
garbage as soon as the statement completes.
Your compiler might be broken, then. Binding to a const reference should
keep the temporary around well past the declaration statement, AFAIUI.

Try

int * const & n(MakeNewInt(123));

although AFAIK it shouldn''t make any difference.
Plus, I don''t want the
caller to have to remember something like that, since I don''t
necessarily have control over what the caller writes. I was hoping
that there was some way that I could modify the MakeNewInt class, while
preserving the type conversion operator functionality, so that a caller
could safely make the following call:
int* n = MakeNewInt(123);




对象什么时候会被销毁呢?必须存在的东西

定义了临时的生命周期。在C ++中,临时生命周期有三个不同的限制。通常它是完整的

表达式。然后初始化(你正在努力)。然后

然后就会绑定到const引用。无论你在

课程中做什么,你都无法改变语言的指定方式。


你可能想重新考虑一下需要你的用户做一个这样一个愚蠢的

的东西,就像抓住从临时对象获得的指针一样。


V



When is the object going to be destroyed, then? Something has to exist
that defines the lifetime of that temporary. In C++ there are three
different limits for a temporary lifetime. Generally it is the full
expression. Then the initialisation (with which you''re struggling). And
then there''s binding to a const reference. No matter what you do in your
class, you won''t be able to change how the language is specified.

You might want to rethink the need for your users to do such a stupid
thing as holding onto a pointer obtained from a temporary object.

V


这篇关于使用类型转换运算符存储指向类实例的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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