如何创建一个只显式转换为int的类? [英] How to create a class that casting to int explicitly only?

查看:53
本文介绍了如何创建一个只显式转换为int的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个类数据如下


class Data {

... 。

};


我知道如果我想将它隐式地转换为int我应该像

一样写下:


类数据{

....

public:

operator int(){.. 。};

};


所以,我可以使用类似的课程,


数据d(1234 );

int a = d;


然而,我真正想要的是类Data的用户已明确

cast它是int。


也就是说,用户应该编写如下代码:


int a =(int)d;


除了:


int a = d;


如何才能明确地将强制转换为int ?


谢谢。


王涛

Hi,

I have a class Data as following

class Data{
....
};

I know if I want to cast it to int implicitly I should write like
following:

class Data{
....
public:
operator int() {...};
};

So, I can use the class like,

Data d(1234);
int a = d;

However, what I really want is that user of class Data have explicitly
cast it to int.

that is, user should write the code like:

int a = (int) d;

other than:

int a = d;

How can I make the cast to int explicitly only?

Thanks.

Tao Wang

推荐答案

7月30日,10:18,Tao Wang< Dancef ... @ gmail.comwrote:
On 30 Jul, 10:18, Tao Wang <Dancef...@gmail.comwrote:




我有一个类数据如下


class Data {

...


};


我知道如果我想将它隐式地转换为int我应该写成

以下:


class Data {

...

public:

* * operator int(){...};


};


所以,我可以使用类似的课程,


数据d(1234);

int a = d;


但是,我真正想要的是,类Data的用户明确地将
投射到了int。


也就是说,用户应该编写如下代码:


int a =(int)d;


除了:


int a = d;


如何才能明确地将强制转换为int?
Hi,

I have a class Data as following

class Data{
...

};

I know if I want to cast it to int implicitly I should write like
following:

class Data{
...
public:
* *operator int() {...};

};

So, I can use the class like,

Data d(1234);
int a = d;

However, what I really want is that user of class Data have explicitly
cast it to int.

that is, user should write the code like:

int a = (int) d;

other than:

int a = d;

How can I make the cast to int explicitly only?



这样做你想要的吗?


class Data

{

public:

显式数据(int);

};


-

Nick Keighley

does this do what you want?

class Data
{
public:
explicit Data(int);
};

--
Nick Keighley


文章

< e2 ****************** ****************@a2g2000prm.g ooglegroups.com> ;,

Tao Wang< Da ******* @ gmail.comwrote:
In article
<e2**********************************@a2g2000prm.g ooglegroups.com>,
Tao Wang <Da*******@gmail.comwrote:

...

class Data {

...

public :

operator int(){...};

};


所以,我可以使用类,


数据d(1234);

int a = d;


然而,我真正想要的是类Data的用户明确地将
强制转换为int。


也就是说,用户应该编写如下代码:


int a =(int)d;


除了:


int a = d;


如何才能明确地将强制转换为int?
...
class Data{
...
public:
operator int() {...};
};

So, I can use the class like,

Data d(1234);
int a = d;

However, what I really want is that user of class Data have explicitly
cast it to int.

that is, user should write the code like:

int a = (int) d;

other than:

int a = d;

How can I make the cast to int explicitly only?



class Data {

....

public:

int to_int(){...};

};


数据d(1234);

int a =(int )d; //错误

int b = d.to_int(); // OK

class Data{
....
public:
int to_int() {...};
};

Data d(1234);
int a = (int) d; // error
int b = d.to_int(); // OK


7月30日上午11:28 *上午,Nick Keighley< nick_keighley_nos ... @ hotmail.com>

写道:
On Jul 30, 11:28*am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:

>

这样做你想要的吗?


class Data

{

* * public:

* * * *显式数据(int);


} ;
>
does this do what you want?

class Data
{
* * public:
* * * * explicit Data(int);

};



1)我不确定你可以向铸造操作员添加显式。

2)如果确实有效,它将会仍然不会做他想要的......


我认为blargg是对的......

1 )I''m not sure You can add explicit to a casting operator.
2) If it does work it will still wont do what he wants...

I think blargg is right...


这篇关于如何创建一个只显式转换为int的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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