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

T

Tao Wang

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
 
N

Nick Keighley

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);
};
 
M

ManicQin

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

Lionel B

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

Tao Wang

does this do what you want?

class Data
{
public:
explicit Data(int);

};

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?
 
T

Tao Wang

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

Noah Roberts

Tao said:
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.
 
N

Noah Roberts

Noah said:
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.
 
J

Jerry Coffin

[ ... ]
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...
 
A

Anarki

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!
 
R

Rajib

Jerry said:
[ ... ]
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?
 
J

Jerry Coffin

[email protected] says... said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top