473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can we overload *_cast<> operators?

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
5 2390
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
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

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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
14
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
3
by: Peterwkc | last post by:
Hello all expert C++ programmer, i fairly new to C++ programming. I have a class cellphone which contain dynamic pointer. I have create (example)ten cellphone. I want to ask user for the...
13
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.