473,786 Members | 2,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about conversion in expressions

Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);
};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
};

If I have code like:

{
A x, y, z;

x = y + z;
}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen
Feb 28 '07 #1
10 1478
On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen
The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.

Feb 28 '07 #2
terminator schreef:
On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
>Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen

The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.
OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!
Feb 28 '07 #3

"Jeroen" <no*****@thanx. comwrote in message news:45e58df7@c s1...
terminator schreef:
>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
>>Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen

The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.

OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!
But the question that you should rather ask yourself is should I really do
this? You're walking towards implicit conversion, which is a dangerous
thing, and also towards a rather questionable design. If A and B are not
related, which is what is indicated by your design, then why would you want
to convert A to B and probably back. It's even more questionable why you
want the compiler to do this silently. If A & B are not related then they
also should not share implementations , like the + op for example. I'd
strongly suggest that you reconsider your design because IMHO this looks
very much like the beginning of a serious design flaw, which could provide
you the arguable pleasure of endless debugging hours.

Cheers
Chris
Feb 28 '07 #4
Chris Theis wrote:
"Jeroen" <no*****@thanx. comwrote in message news:45e58df7@c s1...
>terminator schreef:
>>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
Hi guys,

Just another question. Suppose I have 2 classes (incomplete code):

class A {
A(const B& b);
A& operator = (const A& a);

};

class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);

};

If I have code like:

{
A x, y, z;

x = y + z;

}

There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?

Thanx for any pointers,

Jeroen
The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.
OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!

But the question that you should rather ask yourself is should I really do
this? You're walking towards implicit conversion, which is a dangerous
thing,
Implicit conversion isn't inherently dangerous. It occurs, for example,
whenever you pass an object of a derived type to a function that takes a
reference to one of its base types. And implicit conversions among value
types isn't necessarily dangerous, either.

The real issue here is the interconvertibi lity of the two types, which
often leads to mysterious-looking ambiguities.
and also towards a rather questionable design. If A and B are not
related, which is what is indicated by your design,
Seems to me that the sketch we've seen indicates that A and B are
closely related.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 28 '07 #5
Pete Becker schreef:
Chris Theis wrote:
>"Jeroen" <no*****@thanx. comwrote in message news:45e58df7@c s1...
>>terminator schreef:
On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
Hi guys,
>
Just another question. Suppose I have 2 classes (incomplete code):
>
class A {
A(const B& b);
A& operator = (const A& a);
>
};
>
class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
>
};
>
If I have code like:
>
{
A x, y, z;
>
x = y + z;
>
}
>
There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is
called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?
>
Thanx for any pointers,
>
Jeroen
The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.

OK, so at least one variable should match the available arithmetic
operator to ensure that it works. Thanks!

But the question that you should rather ask yourself is should I
really do this? You're walking towards implicit conversion, which is a
dangerous thing,

Implicit conversion isn't inherently dangerous. It occurs, for example,
whenever you pass an object of a derived type to a function that takes a
reference to one of its base types. And implicit conversions among value
types isn't necessarily dangerous, either.

The real issue here is the interconvertibi lity of the two types, which
often leads to mysterious-looking ambiguities.
>and also towards a rather questionable design. If A and B are not
related, which is what is indicated by your design,

Seems to me that the sketch we've seen indicates that A and B are
closely related.
Indeed they are closely related. B contains a pointer to an 'A' object,
and some additional information. I'm playing around with possible
implementations which may solve several problems of my project. I'm not
yet up to actually implement things. I'll start with that once I know
that I have an elegant architecture which will work without problems.
See my respond in the previous thread 'Question about evaluation order'
for some info of what I'm doing.

Thanx,

Jeroen
Feb 28 '07 #6
On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:
On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:


Hi guys,
Just another question. Suppose I have 2 classes (incomplete code):
class A {
A(const B& b);
A& operator = (const A& a);
};
class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
};
If I have code like:
{
A x, y, z;
x = y + z;
}
There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?
Thanx for any pointers,
Jeroen

The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:

x=B(y)+z;
x=y+B(z);

in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -

- Show quoted text -
Your code works unless another class that similar to B defines a
global binary operator+ and a constructor from an A is introduced.

Feb 28 '07 #7
On Feb 28, 8:25 pm, "terminator " <farid.mehr...@ gmail.comwrote:
On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:


On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
Hi guys,
Just another question. Suppose I have 2 classes (incomplete code):
class A {
A(const B& b);
A& operator = (const A& a);
};
class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
};
If I have code like:
{
A x, y, z;
x = y + z;
}
There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?
Thanx for any pointers,
Jeroen
The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:
x=B(y)+z;
x=y+B(z);
in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
- Show quoted text -

Your code works unless another class that similar to B defines a
global binary operator+ and a constructor from an A is introduced.- Hide quoted text -

- Show quoted text -
class B;
class A{
public:
A(){cout<<"A()\ n";};
A(const B&){cout<<"A(B) \n";};
};

class B{
public:
B(){cout<<"B()\ n";};
B(const A&){cout<<"B(A) \n";};
friend B operator+(const B&,const B&);
};

B operator+(const B&,const B&){cout<<"B+B\ n";return B();};

//if you uncomment the following ,ambiguity error occurs.
/*class C{
public:
C(){cout<<"C()\ n";};
C(const A&){cout<<"C(A) \n";};
friend C operator+(const C&,const C&);
};

C operator+(const C&,const C&){cout<<"C+C\ n";return C();};*/

int _tmain(int argc, _TCHAR* argv[])
{
A x,y,z;
x=y+z;//ok:x=A(B(y)+B(z ));
return 0;
}

Feb 28 '07 #8

"Pete Becker" <pe**@versatile coding.comwrote in message
news:g-*************** *************** @giganews.com.. .
Implicit conversion isn't inherently dangerous. It occurs, for example,
whenever you pass an object of a derived type to a function that takes a
reference to one of its base types. And implicit conversions among value
types isn't necessarily dangerous, either.
I absolutely agree that it's not inherently or necessarily dangerous. But
IMO one should be careful as it might open pitfalls that are very often not
obvious and hard to spot. Still, it certainly has its place and uses. There
is no doubt about that.

Cheers
Chris
Feb 28 '07 #9
terminator schreef:
On Feb 28, 8:25 pm, "terminator " <farid.mehr...@ gmail.comwrote:
>On Feb 28, 5:02 pm, "terminator " <farid.mehr...@ gmail.comwrote:


>>On Feb 28, 4:50 pm, Jeroen <no_m...@thanx. comwrote:
Hi guys,
Just another question. Suppose I have 2 classes (incomplete code):
class A {
A(const B& b);
A& operator = (const A& a);
};
class B {
B(const A& a);
friend B operator + (const B& b1, const B& b2);
};
If I have code like:
{
A x, y, z;
x = y + z;
}
There is no + operator for A. Is it possible that the ctor of B makes
temporary objects of 'y' and 'z', then the + operator of B is called to
add x and y, and the result is converted back to class A with its ctor
and then assigned to x? Or am I talking complete nonsense here?
Thanx for any pointers,
Jeroen
The compiler can not deduce how it should add z and y ;there maybe
other classes similar to B too.I suggest either of the following
lines:
x=B(y)+z;
x=y+B(z);
in either of these cases in contrast to wht you did the compiler is
able to pick 'B operator+(const B&,const B&)'.- Hide quoted text -
- Show quoted text -
Your code works unless another class that similar to B defines a
global binary operator+ and a constructor from an A is introduced.- Hide quoted text -

- Show quoted text -

class B;
class A{
public:
A(){cout<<"A()\ n";};
A(const B&){cout<<"A(B) \n";};
};

class B{
public:
B(){cout<<"B()\ n";};
B(const A&){cout<<"B(A) \n";};
friend B operator+(const B&,const B&);
};

B operator+(const B&,const B&){cout<<"B+B\ n";return B();};

//if you uncomment the following ,ambiguity error occurs.
/*class C{
public:
C(){cout<<"C()\ n";};
C(const A&){cout<<"C(A) \n";};
friend C operator+(const C&,const C&);
};

C operator+(const C&,const C&){cout<<"C+C\ n";return C();};*/

int _tmain(int argc, _TCHAR* argv[])
{
A x,y,z;
x=y+z;//ok:x=A(B(y)+B(z ));
return 0;
}
OK, thanks for the example. If I get this right, then C++ will try to do
any conversion in order to find a corresponding match with the required
operator, but it is a dangerous technique... I will avoid it :-)

Jeroen
Mar 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
25771
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression being used (for example, int* someInt=NULL; ). I used similar initialization myself, and it works fine even if I don't define NULL. But what is "NULL" exactly? Is it a constant defined by compiler? Is there any difference between the following two...
16
5142
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
19
1700
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
52
3070
by: junky_fellow | last post by:
char *str1 = "Hello"; char arr1 = { "Hello" }; char arr2 = { 'H', 'e', 'l', 'l', 'o' }; Is it legal to modify str1, arr1 and arr2 ?
28
2416
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
65
4239
by: Steven Watanabe | last post by:
I know that the standard idioms for clearing a list are: (1) mylist = (2) del mylist I guess I'm not in the "slicing frame of mind", as someone put it, but can someone explain what the difference is between these and: (3) mylist =
14
2229
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 conversion" for a cast operation
40
2604
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd struct? */ Seems to me there is no guarantee it will. /Why Tea
0
952
by: =?Utf-8?B?TGFkaXNsYXYgTXJua2E=?= | last post by:
Hello, I have created my own simple ASP.NET expression. It looks like <%$ InstanceSettings:Name %>. It access NameValueCollection and returns string value. At the moment I need to convert this string to bool for one case because I use it to evalueate Visibility of column in templated grid view. I am trying to do something like this: <asp:BoundField Visible="<%$ InstanceSettings:UseOrg %>" .... but it fails because no implicit...
0
9650
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, 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...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, 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...
0
8992
agi2029
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...
1
7515
isladogs
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...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
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...

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.