By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Can we overload *_cast<> operators?

P: n/a
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.

--
Suman

Jun 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Suman wrote:
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.

--
Suman

I cant sure, but *_cast series seems KEYWORDS not OPERATOR?
Jun 14 '06 #2

P: n/a
Suman wrote:
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.

--
Suman

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...
Jun 14 '06 #3

P: n/a

Rayer wrote:
Suman wrote:
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.

--
Suman

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


<url: http://cs.stmarys.ca/~porter/csc/ref/cpp_operators.html/>
should help you.

Jun 14 '06 #4

P: n/a
Suman wrote:
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.

--
Alan Johnson
Jun 14 '06 #5

P: n/a

"Suman" <sk*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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
Jun 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.