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. 5 1476
> 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;
};
"balor" <ur*@4refs.co m> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
"balor" <ur*@4refs.co m> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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
"Howard" <al*****@hotmai l.com> wrote in message
news:Qz******** *************@b gtnsc04-news.ops.worldn et.att.net "balor" <ur*@4refs.co m> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.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
"John Carson" <jc************ ****@netspace.n et.au> wrote in message
news:d6******** **@otis.netspac e.net.au... "Howard" <al*****@hotmai l.com> wrote in message news:Qz******** *************@b gtnsc04-news.ops.worldn et.att.net "balor" <ur*@4refs.co m> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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 pieces of data around, and requires that certain methods return
an Envelope with a specific kind of Letter as it's content. I have a
cast operator that converts from what I've got to what should be
returned, but it seems that the compiler only...
|
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 a public member function
called get_value() that is not present in the class OptionBase. My problem
is that when calling get_value() I have to cast to Option<some_type>* and I
was wondering if I can somehow remove the cast so I dont have to...
|
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 char* formatter, ...);
Then, I want to call it as so:
|
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 5), but fails on IIS 6 running on a Win2003 server.
The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide
some common functionality. The Page_Load handler the failing webpage starts out like this:
...
| |
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 who will find them very helpfull. gcc has
them as a non-standard option, but only when compiling C language code,
so I'm afraid there might be some obscure reason why local functions are
not so easy to be dealt with in C++, which I do not yet know.
|
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 "input$char$void"
int input(); //can be compiled to name "input$int$void"
....
int i= 3+'0'+input(); //can be compiled to:
int(3)+int('0')+input$int$void()
|
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 of
pointers, where each is a pointer to the base class (each is a SPRITE
*). I know that each of these pointers actually points to one of the
derived classes, even though the type of the pointer is SPRITE *, but
I don't know which derived class it...
|
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 with this:
void qsort(int v, int left, int right)
{
int i, last;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |