473,465 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling functions from cast operator

I have a class with a casting operator but GCC doesn't want to cast as
necessary and I'm wondering if this is legal or not.

class A {
public:
void test() {}
};

class Wrapper {
public:
Wrapper(A a) : a(a) {}

operator A &() { return a; }

private:
A a;
};
int main() {
A a;
Wrapper wrapper(a);
((A &)wrapper).test(); // works
wrapper.test(); // compile error

return 0;
}

Anyone have any idea whats wrong with wrapper.test()? The compiler
should be able to tell that if it casts to (A &), then a test() method
exists.

Jul 23 '05 #1
5 1459
> what's wrong with wrapper.test()?

There is no Wrappter::test(). How is the compiler supposed to know
that you reall want calling wrapper.test() to actually call
Wrapper::a.test()? You need to tell it by using delagation...
class Wrapper
{
public:
//.. your other stuff...
void test() { a.test(); }

private:
A a;
};

Jul 23 '05 #2
"balor" <ur*@4refs.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com
I have a class with a casting operator but GCC doesn't want to cast as
necessary and I'm wondering if this is legal or not.

class A {
public:
void test() {}
};

class Wrapper {
public:
Wrapper(A a) : a(a) {}

operator A &() { return a; }

private:
A a;
};
int main() {
A a;
Wrapper wrapper(a);
((A &)wrapper).test(); // works
wrapper.test(); // compile error

return 0;
}

Anyone have any idea whats wrong with wrapper.test()? The compiler
should be able to tell that if it casts to (A &), then a test() method
exists.


It doesn't work like that. For it to compile, the compiler would need to
iterate through the conversion operators and then see if there is a test()
function for any of the possible conversions. Apart from the fact that it is
a lot of work for the compiler, there is a high risk that it could lead to
erroneous code compiling. The compiler needs a reason to believe that you
really wanted an A reference where you wrote a Wrapper object. The usual
reason is that you supplied a Wrapper object as an argument of a function
that takes an A object. Given

void foo(A& a)
{
a.test();
}

foo(wrapper);

will compile. You have already identified one alternative in the form of a
cast. "BigBrian" has given another.

--
John Carson

Jul 23 '05 #3

"balor" <ur*@4refs.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have a class with a casting operator but GCC doesn't want to cast as
necessary and I'm wondering if this is legal or not.

class A {
public:
void test() {}
};

class Wrapper {
public:
Wrapper(A a) : a(a) {}

operator A &() { return a; }

private:
A a;
};
int main() {
A a;
Wrapper wrapper(a);
((A &)wrapper).test(); // works
wrapper.test(); // compile error

return 0;
}

Anyone have any idea whats wrong with wrapper.test()? The compiler
should be able to tell that if it casts to (A &), then a test() method
exists.


I believe the proper term is "conversion operator", not "cast operator".
And I'm not positive, but I think that operator needs to be const, as in
"operator A& () const".

-Howard


Jul 23 '05 #4
"Howard" <al*****@hotmail.com> wrote in message
news:Qz*********************@bgtnsc04-news.ops.worldnet.att.net
"balor" <ur*@4refs.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have a class with a casting operator but GCC doesn't want to cast
as necessary and I'm wondering if this is legal or not.

class A {
public:
void test() {}
};

class Wrapper {
public:
Wrapper(A a) : a(a) {}

operator A &() { return a; }

private:
A a;
};
int main() {
A a;
Wrapper wrapper(a);
((A &)wrapper).test(); // works
wrapper.test(); // compile error

return 0;
}

Anyone have any idea whats wrong with wrapper.test()? The compiler
should be able to tell that if it casts to (A &), then a test()
method exists.


I believe the proper term is "conversion operator", not "cast
operator". And I'm not positive, but I think that operator needs to
be const, as in "operator A& () const".

No, it doesn't.

--
John Carson
Jul 23 '05 #5

"John Carson" <jc****************@netspace.net.au> wrote in message
news:d6**********@otis.netspace.net.au...
"Howard" <al*****@hotmail.com> wrote in message
news:Qz*********************@bgtnsc04-news.ops.worldnet.att.net
"balor" <ur*@4refs.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have a class with a casting operator but GCC doesn't want to cast
as necessary and I'm wondering if this is legal or not.

class A {
public:
void test() {}
};

class Wrapper {
public:
Wrapper(A a) : a(a) {}

operator A &() { return a; }

private:
A a;
};
int main() {
A a;
Wrapper wrapper(a);
((A &)wrapper).test(); // works
wrapper.test(); // compile error

return 0;
}

Anyone have any idea whats wrong with wrapper.test()? The compiler
should be able to tell that if it casts to (A &), then a test()
method exists.


I believe the proper term is "conversion operator", not "cast
operator". And I'm not positive, but I think that operator needs to
be const, as in "operator A& () const".

No, it doesn't.


You're right. I was thinking a temporary was involved, and that there'd be
trouble binding a non-const reference to it.

My mistake. (Also, I had the const in the wrong place for returning a const
reference. Ooops!)

-Howard

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Hairgrove | last post by:
Consider the following: #include <string> class A { public: A( const std::string & full_name , const std::string & display_name) : m_full_name(full_name)
0
by: A. W. Dunstan | last post by:
I'm porting some code to Visual C++ and have run into a problem - the compiler won't use a user-written cast operator. The code uses an envelope-letter approach to passing (potentially) large...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
61
by: Ron Ford | last post by:
K&R has three different versions of qsort, and the ultimate one is supposed to be like the one in the std library. I'm trying to implement the first, which is in §4.10. I think I'm pretty close...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.