473,385 Members | 1,720 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,385 software developers and data experts.

Why we can not overload the static_cast operator?

In C++ we can not overload the explicit typecast operator like
static_cast, dynamic_cast, const_cast and reinterpret_cast. The only
we can do is the implicit typecast operator like operator
MyClass::MyDistType().

I agree there are maybe no meaning to overload dynamic_cast/const_cast/
reinterpret_cast, except the static_cast. If we enable the overloading
of static_cast, we could be able to force the user doing type casting
explicitly.

There are two ways to implemation this:

Suggestion 1:

class B;
class A
{
public:
friend B operator static_cast<B>(const A &);
};

A a;
// B b=a; //Error, no type casting operator
B b=static_cast<B>(a); //Pass, using the friend casting operator

Suggestion 2:

class B;
class A
{
public:
explicit operator B();
};

A a;
// B b=a; //Error, the type casting operator decalred as explicit
B b=static_cast<B>(a) //Pass, using explicit type casting

Apr 28 '07 #1
1 7077
????? wrote:
In C++ we can not overload the explicit typecast operator like
static_cast, dynamic_cast, const_cast and reinterpret_cast. The only
we can do is the implicit typecast operator like operator
MyClass::MyDistType().

I agree there are maybe no meaning to overload
dynamic_cast/const_cast/ reinterpret_cast, except the static_cast. If
we enable the overloading of static_cast, we could be able to force
the user doing type casting explicitly.

There are two ways to implemation this:

Suggestion 1:

class B;
class A
{
public:
friend B operator static_cast<B>(const A &);
};

A a;
// B b=a; //Error, no type casting operator
B b=static_cast<B>(a); //Pass, using the friend casting operator

Suggestion 2:

class B;
class A
{
public:
explicit operator B();
};

A a;
// B b=a; //Error, the type casting operator decalred as explicit
B b=static_cast<B>(a) //Pass, using explicit type casting
I think that has been discussed a couple of times in the past.
In fact, I vaguely remember that there was a valid reason not to
allow 'explicit' with the type conversion function. But I don't
recall what it was.

Try looking on 'groups.google.com'. You should find something.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 28 '07 #2

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; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
4
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
5
by: Suman | last post by:
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...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
10
by: John Doe | last post by:
Hi, I am trying to transform a class with some time consuming operation by adding a thread. To be able to pass data to thread I have declared a class ThreadParam as shown below : class...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.