Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloading 'casting' operator

Reply
Thread Tools

Overloading 'casting' operator

 
 
David Williams
Guest
Posts: n/a
 
      01-17-2006
Hi all,

I have a templated Vector3D class which holds (x,y,z) components as the
specified type. I quite often wish to cast a Vector3D holding ints into
a Vector3D holding floats and vice versa. Like so:

Vector3D<int> intVec(10,20,30);
Vector3D<float> floatVec = intVec;

Of course this doesn't work. I would be happy if instead the following
worked:


Vector3D<int> intVec(10,20,30);
Vector3D<float> floatVec = static_cast< Vector3D<float> >(intVec);

but of course that doesn't either. I have read online that it is not
possible to overload the static_cast operator (why, incidently?) but
what is the best approach to this problem? Ideally I would like the
first example to compile but generate a warning and the second example
to be fine (as works for built-in types).

Any thoughts appriciated,

David
 
Reply With Quote
 
 
 
 
tbuc99@gmail.com
Guest
Posts: n/a
 
      01-17-2006
For the first one, I think you can overload = by
template <class T>
class Vector3D{
.....
template <class N> operator=(const Vector3D<N>& rvalue)
{....}
.....
};
my first reply may not be correct.

I don't know the answer to the second one though.


David Williams wrote:
> Hi all,
>
> I have a templated Vector3D class which holds (x,y,z) components as the
> specified type. I quite often wish to cast a Vector3D holding ints into
> a Vector3D holding floats and vice versa. Like so:
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = intVec;
>
> Of course this doesn't work. I would be happy if instead the following
> worked:
>
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = static_cast< Vector3D<float> >(intVec);
>
> but of course that doesn't either. I have read online that it is not
> possible to overload the static_cast operator (why, incidently?) but
> what is the best approach to this problem? Ideally I would like the
> first example to compile but generate a warning and the second example
> to be fine (as works for built-in types).
>
> Any thoughts appriciated,
>
> David


 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      01-17-2006
David Williams wrote:
> I have a templated Vector3D class which holds (x,y,z) components as the
> specified type. I quite often wish to cast a Vector3D holding ints into
> a Vector3D holding floats and vice versa. Like so:
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = intVec;
>
> Of course this doesn't work. I would be happy if instead the following
> worked:
>
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = static_cast< Vector3D<float> >(intVec);
>
> but of course that doesn't either. I have read online that it is not
> possible to overload the static_cast operator (why, incidently?) but
> what is the best approach to this problem? Ideally I would like the
> first example to compile but generate a warning and the second example
> to be fine (as works for built-in types).


Without code showing the definition of template<typename T> Vector3D,
it's difficult to respond. See the FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

As another poster said, a templated operator=() may give you what you
want for the first issue. For the second issue, although you can't
overload the static_cast operator directly, you could make the
static_cast operator work the way you want by providing a templated
copy constructor - something like (VERY untested code):

template<typename T> class Vector3D {
public:
template<typename U>
explicit Vector3D(const Vector3D<U>&);
};

Best regards,

Tom

 
Reply With Quote
 
Luke Meyers
Guest
Posts: n/a
 
      01-17-2006
David Williams wrote:
> I have a templated Vector3D class which holds (x,y,z) components as the
> specified type. I quite often wish to cast a Vector3D holding ints into
> a Vector3D holding floats and vice versa. Like so:
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = intVec;


What you need here is not, as other have suggested, an overloaded
operator=. This is not assignment, but initialization -- i.e.,
construction. For construction, we use constructors. If you define a
single-argument constructor which takes another Vector3D of a different
type, you can do what you want here. It's important to recognize the
difference, though, between the above, assignment, and casting.

Initialization (construction) creates a new instance, based in some way
on the parameter(s) provided to the constructor.

Assignment modifies an existing instance, based in some way on the
parameter provided to operator=.

Casting is more complicated -- it depends on whether you're casting by
value, pointer, or reference. What's more, it is possible to provide
an overloaded "conversion operator" (not casting operator) which allows
implicit (or explicit, if that keyword is used) conversion to another
type. A conversion operator is sort of the inverse of a constructor.

> Of course this doesn't work. I would be happy if instead the following
> worked:
>
> Vector3D<int> intVec(10,20,30);
> Vector3D<float> floatVec = static_cast< Vector3D<float> >(intVec);


You should not be satisfied with this. Implement an appropriate
single-argument constructor and you'll have a better result.

Luke

 
Reply With Quote
 
David Williams
Guest
Posts: n/a
 
      01-17-2006
Luke Meyers wrote:
> David Williams wrote:
>
>>I have a templated Vector3D class which holds (x,y,z) components as the
>>specified type. I quite often wish to cast a Vector3D holding ints into
>>a Vector3D holding floats and vice versa. Like so:
>>
>>Vector3D<int> intVec(10,20,30);
>>Vector3D<float> floatVec = intVec;

>
>
> What you need here is not, as other have suggested, an overloaded
> operator=. This is not assignment, but initialization -- i.e.,
> construction. For construction, we use constructors. If you define a
> single-argument constructor which takes another Vector3D of a different
> type, you can do what you want here. It's important to recognize the
> difference, though, between the above, assignment, and casting.


Ah yes, I can see that now. However, could it be argued (according to
the 'Big Three' rule) that if I have a custom constructor I should have
a custom assignment operator as well?

>
> Initialization (construction) creates a new instance, based in some way
> on the parameter(s) provided to the constructor.


Great, this now seems to work:

template <typename Type>
template <typename CastType>
Vector3D<Type>::Vector3D(const Vector3D<CastType>& vectorToSet)
{
m_tX = vectorToSet.x();
m_tY = vectorToSet.y();
m_tZ = vectorToSet.z();
}

>
> Assignment modifies an existing instance, based in some way on the
> parameter provided to operator=.
>
> Casting is more complicated -- it depends on whether you're casting by
> value, pointer, or reference. What's more, it is possible to provide
> an overloaded "conversion operator" (not casting operator) which allows
> implicit (or explicit, if that keyword is used) conversion to another
> type. A conversion operator is sort of the inverse of a constructor.


I found the following code and modified it for my class. Is this the
'conversion operator' you are refering to?

template <typename Type>
template <typename CastType>
Vector3D<Type>:perator Vector3D<CastType> () throw()
{
return Vector3D<CastType>(m_tX,m_tY,m_tZ);
}

I was confused by the fact that the return type (Vector3D<CastType>)
appears to come between 'operator' and the empty brackets. But it's ok?

>
>
>>Of course this doesn't work. I would be happy if instead the following
>>worked:
>>
>>Vector3D<int> intVec(10,20,30);
>>Vector3D<float> floatVec = static_cast< Vector3D<float> >(intVec);


Come to think of it, I could write a vector_cast function to do the
same. However, given what I now know I don't think it's necessary.
>
>
> You should not be satisfied with this. Implement an appropriate
> single-argument constructor and you'll have a better result.
>
> Luke
>


Thanks for your help!
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-17-2006
David Williams wrote:
> Ah yes, I can see that now. However, could it be argued (according to
> the 'Big Three' rule) that if I have a custom constructor I should have
> a custom assignment operator as well?


No. The 'Big Three' in the rule are specifically the copy constructor,
assignment operator and destructor. The functions that the compiler
will generate for you if your don't define them yourself. If you need
to write your own version of any one of these three, you probably need
to write them all yourself because the compiler generated ones probably
will not do what you want. There is nothing in the 'rule' about any
other constructors you may write.

Gavin Deane

 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      01-17-2006
Luke Meyers wrote:
> What you need here is not, as other have suggested, an overloaded
> operator=. This is not assignment, but initialization -- i.e.,
> construction. For construction, we use constructors. If you define a
> single-argument constructor which takes another Vector3D of a different
> type, you can do what you want here. It's important to recognize the
> difference, though, between the above, assignment, and casting.


I agree... but didn't I suggest exactly that?

Best regards,

Tom

 
Reply With Quote
 
David Williams
Guest
Posts: n/a
 
      01-17-2006
Gavin Deane wrote:
> David Williams wrote:
>
>>Ah yes, I can see that now. However, could it be argued (according to
>>the 'Big Three' rule) that if I have a custom constructor I should have
>>a custom assignment operator as well?

>
>
> No. The 'Big Three' in the rule are specifically the copy constructor,
> assignment operator and destructor. The functions that the compiler
> will generate for you if your don't define them yourself. If you need
> to write your own version of any one of these three, you probably need
> to write them all yourself because the compiler generated ones probably
> will not do what you want. There is nothing in the 'rule' about any
> other constructors you may write.
>
> Gavin Deane
>


Ok, thanks.
 
Reply With Quote
 
tbuc99@gmail.com
Guest
Posts: n/a
 
      01-18-2006
Yeah, you're right. It's copy constructor instead of assignment
operator, thanks for pointing out.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments