473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a class that casting to int explicitly only?

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
Jul 30 '08 #1
15 1402
On 30 Jul, 10:18, Tao Wang <Dancef...@gmai l.comwrote:
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?
does this do what you want?

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

--
Nick Keighley
Jul 30 '08 #2
In article
<e2************ *************** *******@a2g2000 prm.googlegroup s.com>,
Tao Wang <Da*******@gmai l.comwrote:
...
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() {...};
};

Data d(1234);
int a = (int) d; // error
int b = d.to_int(); // OK
Jul 30 '08 #3
On Jul 30, 11:28*am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
>
does this do what you want?

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

};
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...
Jul 30 '08 #4
On Wed, 30 Jul 2008 03:07:01 -0700, ManicQin wrote:
On Jul 30, 11:28Â*am, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:

>does this do what you want?

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

};

1 )I'm not sure You can add explicit to a casting operator.
Well, that is a constructor, not a casting operator.
2) If it does work it will still wont do what he wants...
Right. The `explicit' as above will prevent assignment of Data objects by
ints (more or less the opposite of what the OP wants) as in:

Data d;
...
d = 3; // error: no match for ‘operator=’ in ‘d = 3’
I think blargg is right...
Maybe not precisely what the OP requested, but seems sensible enough.

--
Lionel B
Jul 30 '08 #5
On Jul 30, 7:28 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
wrote:
On 30 Jul, 10:18, Tao Wang <Dancef...@gmai l.comwrote:
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?

does this do what you want?

class Data
{
public:
explicit Data(int);

};

--
Nick Keighley
No, it is in wrong direction.

What I expected result is

Data d(1234);

int a = d; // error
int a = (int) d; // OK

Your solution is provide explicitly casting FROM int, rather than to
int.

How this be done?
Jul 30 '08 #6
On Jul 30, 7:46 pm, blargg <blargg....@gis hpuppy.comwrote :
In article
<e23c84a3-9bfa-4c5d-87f2-eed0cb157...@a2 g2000prm.google groups.com>,
Tao Wang <Dancef...@gmai l.comwrote:
...
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() {...};

};

Data d(1234);
int a = (int) d; // error
int b = d.to_int(); // OK
This is similar to what I expected, however, I don't want user to
remember the interface to_int(). What I expect is that the code:

Data d(1234);
int a = (int) d; // OK

should be correct, that is, Data can convert to int, however, I don't
want it convert implicitly.
Jul 30 '08 #7
Tao Wang wrote:
On Jul 30, 7:46 pm, blargg <blargg....@gis hpuppy.comwrote :
>[..]
Data d(1234);
int a = (int) d; // error
int b = d.to_int(); // OK

This is similar to what I expected, however, I don't want user to
remember the interface to_int(). What I expect is that the code:

Data d(1234);
int a = (int) d; // OK

should be correct, that is, Data can convert to int, however, I don't
want it convert implicitly.
There is *no way* to do what you want. And try not to repeat yourself.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '08 #8
Tao Wang wrote:
On Jul 30, 7:46 pm, blargg <blargg....@gis hpuppy.comwrote :
>In article
<e23c84a3-9bfa-4c5d-87f2-eed0cb157...@a2 g2000prm.google groups.com>,
Tao Wang <Dancef...@gmai l.comwrote:
>>...
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() {...};

};

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

This is similar to what I expected, however, I don't want user to
remember the interface to_int(). What I expect is that the code:

Data d(1234);
int a = (int) d; // OK

should be correct, that is, Data can convert to int, however, I don't
want it convert implicitly.
Well, if you stop using C style casts, which you should do anyway, then
you can create for yourself a new kind of cast. Several classes do this
in the boost library. Then they just have to use your new cast. If you
use this cast for other objects though they'll find it easy to remember.

In other words, make an external function but call it something that
ends in "_cast".

In fact, you could even piggy back boost::lexical_ cast if you really
wanted to. Just make an operator >(Data & in, int & out).

Really though, this problem of yours could indicate a bad design
decision. It shouldn't be unreasonable to expect the user to know the
function call they need to make to get the int out of the object.
Jul 30 '08 #9
Noah Roberts wrote:
In fact, you could even piggy back boost::lexical_ cast if you really
wanted to. Just make an operator >(Data & in, int & out).
Actually, I was being a dumbass. This won't work. You probably don't
want anything to do with lexical cast if speed is even remotely a concern.
Jul 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2584
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
6
5872
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
9
4032
by: Roman Mashak | last post by:
Hello, All! Given the sample piece of code I have: #include <stdio.h> #include <string.h> int main(void) { short int i, j;
0
1596
by: Greg Conely via .NET 247 | last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin, not the entire app. I have created a base interface, it looks like: public interface myModel sub Initialize(byref DisplayPanel) sub Connect() sub Disconnect() sub Query() ...
31
3161
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
6
2486
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
6
3204
by: Nishu | last post by:
Hi all, What is difference between typecasting and casting? I used to think that both are same; but few days back someone pointed out here that these are different. Vague guess, Is it that typecasting means casting using typedef? I just want to confirm it. Thanks, Nishu
20
4030
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
32
2373
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION" variable, which of the following lines are correct:
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.