473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1387
On 30 Jul, 10:18, Tao Wang <Dancef...@gmail.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**********************************@a2g2000prm.g ooglegroups.com>,
Tao Wang <Da*******@gmail.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...@gmail.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....@gishpuppy.comwrote:
In article
<e23c84a3-9bfa-4c5d-87f2-eed0cb157...@a2g2000prm.googlegroups.com>,
Tao Wang <Dancef...@gmail.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....@gishpuppy.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....@gishpuppy.comwrote:
>In article
<e23c84a3-9bfa-4c5d-87f2-eed0cb157...@a2g2000prm.googlegroups.com>,
Tao Wang <Dancef...@gmail.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
In article <e23c84a3-9bfa-4c5d-87f2-
ee**********@a2g2000prm.googlegroups.com>, Da*******@gmail.com says...

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

class Data{
...
public:
operator int() {...};
};
[ ... ]
How can I make the cast to int explicitly only?
Use a C++ 0x compiler. Otherwise, you're pretty well stuck...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 30 '08 #11
Tao Wang <Da*******@gmail.comwrote:
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?
Your request is unreasonable. The answer is to stop wanting that.
Jul 30 '08 #12
On Jul 30, 2:18*pm, Tao Wang <Dancef...@gmail.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?

Thanks.

Tao Wang
#include <iostream>
using namespace std;

class Data
{
int m_data;
public:
Data(int data = 0):m_data(data){}
//operator int(){return m_data;}
};

int main()
{
Data x(10);
//int *y = (int*) &x;
int *y = reinterpret_cast<int *(&x);
cout << *y;
return 0;
}

am not sure is this what u need!
Jul 31 '08 #13
Jerry Coffin wrote:
In article <e23c84a3-9bfa-4c5d-87f2-
ee**********@a2g2000prm.googlegroups.com>, Da*******@gmail.com says...

[ ... ]
>I know if I want to cast it to int implicitly I should write like
following:

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

[ ... ]
>How can I make the cast to int explicitly only?

Use a C++ 0x compiler. Otherwise, you're pretty well stuck...
Just wondering, how would you do it in C++0x?
Aug 2 '08 #14
On 2008-08-02 16:23:44 -0400, Rajib <ra*****@verizon.netsaid:
Jerry Coffin wrote:
>In article <e23c84a3-9bfa-4c5d-87f2-
ee**********@a2g2000prm.googlegroups.com>, Da*******@gmail.com says...

[ ... ]
>>I know if I want to cast it to int implicitly I should write like
following:

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

[ ... ]
>>How can I make the cast to int explicitly only?

Use a C++ 0x compiler. Otherwise, you're pretty well stuck...

Just wondering, how would you do it in C++0x?
explicit operator int() {...};

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 2 '08 #15
In article <kn3lk.58$T91.13@trnddc04>, ra*****@verizon.net says...

[ ... ]
Just wondering, how would you do it in C++0x?
You mark the conversion operator explicit, just like you can mark a ctor
explicit in the current version of C++:

class X {
int x;
public:
X(int init) : x(init) {}

explicit operator int() { return x; }
};

To use this, you MUST explicitly cast to int:

X x(2);

int a = x+1; // fails

int z = (int)x+1; // succeeds

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 2 '08 #16

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

Similar topics

7
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
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
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
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,...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
6
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
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...
20
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...
32
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"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.