Can we overload *_cast<> operators?

S

Suman

Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++
archives and Stroustrup's FAQs (particularly the following:
<url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>)
I am left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>
operators cannot be overloaded as well. Hence this post.

Awaiting enlightenment.
 
R

Rayer

Suman said:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++
archives and Stroustrup's FAQs (particularly the following:
<url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>)
I am left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>
operators cannot be overloaded as well. Hence this post.

Awaiting enlightenment.
I cant sure, but *_cast series seems KEYWORDS not OPERATOR?
 
R

Rayer

Suman said:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++
archives and Stroustrup's FAQs (particularly the following:
<url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>)
I am left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>
operators cannot be overloaded as well. Hence this post.

Awaiting enlightenment.
Do you mean such like static_cast<typedef>?
I cant sure, but it seems is not a operator...

and the same, (typedef), () is not operator too...
 
A

Alan Johnson

Suman said:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++
archives and Stroustrup's FAQs (particularly the following:
<url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>)
I am left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>
operators cannot be overloaded as well. Hence this post.

Awaiting enlightenment.

You can define a conversion operator (this probably has some real name,
but I don't know it off the top of my head). Example:

class A
{
} ;

class B
{
public:
operator A()
{
// Do what ever is needed to "cast" this to type A.
A a ;
return a ;
}
} ;

This would enable something like:

B b ;
A a ;
a = static_cast<A>(b) ; // cast is unnecessary here.
 
C

Chris Theis

Suman said:
Having had a look at the C++ FAQ, comp.lang.c++ & comp.std.c++
archives and Stroustrup's FAQs (particularly the following:
<url:http://www.research.att.com/~bs/bs_faq2.html#overload-dot/>)
I am left to wondering why Stroustrup doesn't mention the
*_cast<> operators or operator .* in that particular FAQ.
Archived discussions have convinced me that .* _cannot_ be overloaded.
I have gathered a notion (perhaps incorrectly) that the *_cast<>
operators cannot be overloaded as well. Hence this post.

Awaiting enlightenment.

Well, aren't we all? ;-)

You cannot directly overload "operators" like static_cast<> but what you can
do is to overload the conversion operators that are invoked upon the use of
a cast. For example:

class MyString {
public:
.....
operator char*(); // Here you could implement the desired behavior when
a cast to char* is required.
}

But you should be very careful with this, as sometimes casting might be
performed implicitely and this can open Pandora's box!!

The operator ".*" cannot be overloaded at all.

HTH
Chris
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top