By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Automatic conversion operators

P: n/a
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
}
Nov 1 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

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
}
In what way does:

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

not get you what you want?

Nov 1 '06 #2

P: n/a

Salt_Peter wrote:
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
}

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

Nov 1 '06 #3

P: n/a

Salt_Peter wrote:
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
}

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

Nov 1 '06 #4

P: n/a
Salt_Peter wrote:
Salt_Peter wrote:
>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
}
In what way does:

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

not get you what you want?


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
Nov 1 '06 #5

P: n/a
Tom Smith wrote:
Salt_Peter wrote:
>Salt_Peter wrote:
>>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 1 '06 #6

P: n/a
Victor Bazarov wrote:
Tom Smith wrote:
>Salt_Peter wrote:
>>Salt_Peter wrote:
Tom Smith wrote:
<snip>
>>>>The following doesn't compile. Is there a a way to make it work?
<snip>
>>>>#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 }
[...]
<snip>
>
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
Nov 1 '06 #7

P: n/a
Tom Smith wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 1 '06 #8

P: n/a
Victor Bazarov wrote:
Tom Smith wrote:
>[..]
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.

Nov 2 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.