Calling functions from cast operator

B

balor

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

BigBrian

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;
};
 
J

John Carson

balor said:
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.
 
H

Howard

balor said:
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
 
J

John Carson

Howard said:
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.
 
H

Howard

John Carson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top