473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Automatic conversion operators

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
8 2062

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

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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Example... This would make that i can assign a ULONG to a CComBSTR2 class while the body of the code performs necessary conversion. CComBSTR2& operator=(ULONG ulong) {
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
1
by: Philip Bondi | last post by:
Hello to all SQL Server junkies who work with non-English characters: For people running scripts from the command line using ANSI files with special characters, it is very important to use isql...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
3
by: Russ | last post by:
I posted a message on this several days ago, but it apparently got lost in googlespace, so I'll try it again. I recently discovered a bug in my code that apparently resulted from the automatic...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.