Automatic conversion operators

T

Tom Smith

Hi,

What I want to do is rather complicated to describe but simple to demonstrate:
so I'll do that instead.

The following doesn't compile. Is there a a way to make it work? (by "make it
work", I mean: can I write the classes A, B and C in such a way that the given
main function will compile and do what I expect).

Cheers,


Tom



#include <iostream>

using namespace std;

class A
{
void function() { cout << "function called!"; }
};

class B
{
};

class C
{
operator A& () { return a; }
operator B& () { return b; }
A a;
B b;
};

int main()
{
C c;
c.function(); // I'd like this to automatically convert C to an A
}
 
S

Salt_Peter

Tom said:
Hi,

What I want to do is rather complicated to describe but simple to demonstrate:
so I'll do that instead.

The following doesn't compile. Is there a a way to make it work? (by "make it
work", I mean: can I write the classes A, B and C in such a way that the given
main function will compile and do what I expect).

Cheers,


Tom



#include <iostream>

using namespace std;

class A
{
void function() { cout << "function called!"; }
};

class B
{
};

class C
{
operator A& () { return a; }
operator B& () { return b; }
A a;
B b;
};

int main()
{
C c;
c.function(); // I'd like this to automatically convert C to an A
}

In what way does:

class C
{
A a;
B b;
public:
void function() { a.function(); }
};

not get you what you want?
 
S

Salt_Peter

Salt_Peter said:
In what way does:

class C
{
A a;
B b;
public:
void function() { a.function(); }
};

not get you what you want?

And since you did say "convert" a C to an A:

class C : public A
{
B b;
};
 
S

Salt_Peter

Salt_Peter said:
In what way does:

class C
{
A a;
B b;
public:
void function() { a.function(); }
};

not get you what you want?

And since you did say "convert" a C to an A:

class C : public A
{
B b;
};
 
T

Tom Smith

You're right - I could do that. But this is a very simplified example: what i
want is actually a great deal more complicated, but this syntactic sugar is what
would make it about a thousand times easier; and the implementation problem in
my real program is identical to the one in the toy example above. ("Post minimal
code that demonstrates your problem", remember?)

Tom
 
V

Victor Bazarov

Tom said:
Salt_Peter said:
Salt_Peter said:
Tom Smith wrote:
Hi,

What I want to do is rather complicated to describe but simple to
demonstrate: so I'll do that instead.

The following doesn't compile. Is there a a way to make it work?
(by "make it work", I mean: can I write the classes A, B and C in
such a way that the given main function will compile and do what I
expect). Cheers,


Tom



#include <iostream>

using namespace std;

class A
{
void function() { cout << "function called!"; }
};

class B
{
};

class C
{
operator A& () { return a; }
operator B& () { return b; }
A a;
B b;
};

int main()
{
C c;
c.function(); // I'd like this to automatically convert C to an
A }
[...]
this is a very simplified
example: what i want is actually a great deal more complicated, but
this syntactic sugar is what would make it about a thousand times
easier; and the implementation problem in my real program is
identical to the one in the toy example above. ("Post minimal code
that demonstrates your problem", remember?)

I do not think it is possible. The compiler is not obligated (by the
Standard) to look for those conversions. It is similar to this:

struct A {
A(int) {}
void operator+(A const&) {}
};

int main() {
A a(42);
666 + a; // won't compile
}

The compiler does not have to look for an existing conversion from
'666' to 'A' just to resolve the operator+ when operator+ is a member.
For non-members it would do it. In your case the '.' operator is the
connection (like the '+' in my example). The left-hand side will not
be converted just to satisfy the need to find the 'membership' for
'function' expression (the right-hand side).

V
 
T

Tom Smith

Victor said:
The following doesn't compile. Is there a a way to make it work?
#include <iostream>

using namespace std;

class A
{
void function() { cout << "function called!"; }
};

class B
{
};

class C
{
operator A& () { return a; }
operator B& () { return b; }
A a;
B b;
};

int main()
{
C c;
c.function(); // I'd like this to automatically convert C to an
A }
[...]

I do not think it is possible. The compiler is not obligated (by the
Standard) to look for those conversions.

This is what I thought.


It is similar to this:
struct A {
A(int) {}
void operator+(A const&) {}
};

int main() {
A a(42);
666 + a; // won't compile
}

This is in fact closer to (part of) my actual problem: two classes A and B which
both auto-convert to int, and for which it would be nice to simply write a+b etc
instead of having to write specific operators or explicitly converting.
The compiler does not have to look for an existing conversion from
'666' to 'A' just to resolve the operator+ when operator+ is a member.
For non-members it would do it. In your case the '.' operator is the
connection (like the '+' in my example). The left-hand side will not
be converted just to satisfy the need to find the 'membership' for
'function' expression (the right-hand side).

Ok. Do you have any idea what the neatest way would be to do this sort of thing?

Thanks again,



Tom
 
V

Victor Bazarov

Tom said:
[..]
Ok. Do you have any idea what the neatest way would be to do this
sort of thing?

Well, with operators it is solved simply: the operator needs to be
defined a non-member:

struct A {
A(int) {}
};

void operator+(A const&, A const&) {}

int main() {
A a(42);
666 + a; // compiles nicely
}

Now, I don't know your "real" problem, but if it gives you an idea,
good. If it doesn't, you will have to post more information.

V
 
R

Rolf Magnus

Victor said:
Tom said:
[..]
Ok. Do you have any idea what the neatest way would be to do this
sort of thing?

Well, with operators it is solved simply: the operator needs to be
defined a non-member:

struct A {
A(int) {}
};

void operator+(A const&, A const&) {}

int main() {
A a(42);
666 + a; // compiles nicely
}

That doesn't look like what the OP wants. I think that he wants that in:

int main()
{
A a(42);
A b(4711);
a + b;
}

a and b are both implicitly converted to int and then the operator+ for int
is used, so that he doesn't need to define his own operator+ at all. I
guess the idea is that he doesn't want to implement dozens of operators to
make the class behave similar to int. However, I don't think that's
possible.
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top