Archived

This topic is now archived and is closed to further replies.

Yratelev

wots a casting operator? and how do u make one?

Recommended Posts

AFAIK there''s no casting operator, just a casting operand i.e. the value to be casted. There are, though, several ways to cast. For instance, to cast a char to an int you could do:
(int)Char; 
or
int(Char); 
and why not the c++ way
static_cast(char); 

I mostly prefer the first though...

_______________ ____ ___ __ _
Enselic''s Corner

Share this post


Link to post
Share on other sites
Oh, I get you...

Well, in a class you can have an operator so when you type
    some_double = class_instance;  
class_instance will return a double, which you define how to return.
Just have a
    operator double() {return any double after something is done;}  


_______________ ____ ___ __ _
Enselic's Corner

[edited by - Enselic on July 27, 2003 6:12:15 PM]

Share this post


Link to post
Share on other sites
a casting operator is defined as follows:


// assuming you wish to define an operator to cast from

// type MyClass, to type SomeOtherClass, then here''s the way to

// do it, as a unary member function ... it may also be able

// to be done as a global binary operator, not sure though


class MyClass
{
public:
operator SomeOtherClass(void);
};


notice the return type of the operator is not specified on the left, because it would be invlaid to define a cast operator in which both types wern''t identical, they decided to only require the one to the right of the word operator ...

it looks like you are defining an operator named for some type, which is kinda odd, but that''s how it works ...

inside the function definition you do whatever you want, just make sure and return the item of the correct type ...

Here''s a silly example

// assuming a struct Point2D with X and Y members

// and that this Point3D has X, Y and Z members

// and that Point2D has a constructor which takes x and y

struct Point3D
{
public:
operator Point2D(void)
{
Point2D point2d(x,y);
return point2d;
}
};

Share this post


Link to post
Share on other sites
also, defining a constructor which takes one parameter, will also define the ability to cast ...

for example


// assuming Point2D and Point3D again

// if you add to Point3D this public constructor

Point3D(Point2d point2d);
// then you will have the ability to cast from 2D to 3D points

// which may not make sense ... but it works none the less


Share this post


Link to post
Share on other sites
Casting operators define an implicit conversion from one type to another. If your Foo class has an operator Bar then, wherever a function expects a Bar, if you pass it a Foo, the compiler will insert a call to the casting operator and pass the result to the function.

As Xai pointer out, Foo::operator Bar() (in Foo) defines a conversion from Foo to Bar, and so does Bar::Bar(const Bar&) (in Bar). It is an error to define both, as the compiler cannot decide which one to use.

Implicit conversions can also cause hard-to-find errors. Consider adding the explicit keyword before your conversion definition (only for constructors, I think), so that the conversion is only done when you request it. Only use implicit conversions when your class really represents another type (e.g. proxies).

Finally, these conversions occur in addition to built-in type conversions (like int to float or char to long), but the compiler will only ever do one user-defined conversion at a time. Even if you have casting operators defined from Foo to Bar and from Bar to Baz, the compiler will not convert a Foo into a Baz.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
the operator is implemented as a function ... so the cost is exactly as expensive as any othe function ... and just like any other function, you can theoretically inline it, and a smart optimizing compiler may optimize it for you reguardless ...

so just think of it as a function ... such as:

ReturnType ConvertToReturnType(const OriginalType &object);

or

ReturnType ConvertToReturnType(OriginalType &object);

// depending on which way you write it ...

and you should never be confused about what''s really going on ...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
operators are inline by definition, unless the compiler decides otherwise (eg. debug build, etc.)

Share this post


Link to post
Share on other sites
the AP may be right that operators behave as if DECLARED inline at all times ... which means adding the inline keyword has no effect (and may be invalid) ... but even in release builds, they are not INLINED at all times ... a costly function, no matter how it is declared (inline or not), will NOT be inlined repeatedly by the compiler (usually a max of about 8 instances may occur for a fairly high cost function, before the compiler refuses to inline it).

Share this post


Link to post
Share on other sites