2

I have a wrapper class and I want to modify the data and convert it back to its original type.

class A
{
public:
    A ( unsigned __int64 _a ) : a (_a)
    {
    }
    operator unsigned __int64 () const
    {
        return a;
    }
    unsigned __int64 a;
}; 

I want the object of this class to implicitly convert back to unsigned __int64, but it failed.

Say,

 A a( 0x100ull );
 unsigned __int64 b = (a >> 16);  // Error

Compiler gives C2678 error, no operator found or there is no acceptable conversion. It seems this function operator unsigned __int64 () const doesn't work.


To be more specific, compiler says there is no acceptable conversion. I cannot accept the complain, because I have already given a good one. Can someone legitimize it?

Dai Haoci
  • 209
  • 2
  • 6
  • What is an ___int64? Is that something you've locally defined? – Amardeep AC9MF Sep 08 '11 at 00:07
  • What compiler ar you using? This compiles fine with VC++2010 - i.e. the implicit conversion to unsigned __int64 is performed. – sashang Sep 08 '11 at 00:57
  • @sashang, I actually, simplified my example, now it's the actual one. I was using vs2005 – Dai Haoci Sep 08 '11 at 01:19
  • @Dai: the example works for me with VS 2005 SP1 (specifically compiler version 14.00.50727.762). Maybe try a later version of Visual Studio (the Express versions are free) or apply SP1 to see if that makes a difference (if you don't already have SP1). – Michael Burr Sep 08 '11 at 02:45

1 Answers1

3

It doesn't work because you haven't created an operator>> overload for your class that takes an integer and does something with it.

I'm guessing you're trying to do a right shift on your int, but I'm not sure that overloading your operator>> is a good idea for that, as these operators in a context like that, are normally used for streaming. It might confuse a reader or maintainer of your code afterwards.

See here for more info on operator overloading

Perhaps rethink your implementation strategy?

Community
  • 1
  • 1
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 1
    But the OP is presumably expecting the compiler to implicitly call the cast operator here? – Oliver Charlesworth Sep 08 '11 at 00:10
  • 1
    Thanks for your quick response. But, why compiler didn't convert a to `unsigned ___int64` – Dai Haoci Sep 08 '11 at 00:11
  • @Oli: do you suggest that `operator>>` should be implemented to cast? I'm not sure what you're trying to suggest? – Tony The Lion Sep 08 '11 at 00:13
  • Try adding an explicit conversion, `__int64(a) >> 16`. – Kerrek SB Sep 08 '11 at 00:13
  • 2
    @Dai: because your A isn't a `unsigned ___int64`, it's a type of itself, which, because of it's ctor, can implicitly convert to a `unsigned ___int64`, that doesn't however mean it also behaves as one, it just stores it in the member of the class. – Tony The Lion Sep 08 '11 at 00:14
  • @Dai: I'm not sure now, it should be... does it work if you just change the type to `int`? – Kerrek SB Sep 08 '11 at 00:17
  • You cannot implicitly cast an object to an int. (Unless there's some weird C++ magic that I'm not aware of.) – Corbin Sep 08 '11 at 00:18
  • @Corbin: I defined an operator to perform the cast and it was used implicitly. See here: http://ideone.com/oPxyJ. – sashang Sep 08 '11 at 00:42
  • @Dai: What compiler are you using? The implicit conversion in your code (for the shift operation) works fine for me with MSVC 16 (VS 2010) and GCC/MinGW 4.5.1. – Michael Burr Sep 08 '11 at 01:04
  • @sashang: Apparently I should google before speaking x.x. I had always assumed that the () operator was used to call something like a() (where a is some object). Never knew that it's the cast operator. Learned something today. (And learned I should check before posting next time :p) – Corbin Sep 08 '11 at 01:35
  • @Corbin : You're thinking of `SomeType operator()`; he has `operator SomeType()` -- two entirely different operators. – ildjarn Sep 08 '11 at 02:53
  • Ah... That makes more sense then! Thanks. – Corbin Sep 08 '11 at 04:15