473,396 Members | 1,738 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,396 software developers and data experts.

why I have not cast from object to reference

Hello.

Compiler BCB60.
There is a simple example:

class a1 {
int v;
public:
a1 ():v (0) {}
a1 (int v1):v (v1) {}
a1 (a1& a):v (a.v) {}
a1& operator = (a1& a) {v=a.v; return *this;}
};

a1 fun (a1 a) {
a1 ex=a;
return ex;
}

int main (int argc, char* argv [])
{

a1 ex1 (1);
a1 ex2 (2);
a1 ex3 (3);

ex2=ex1;
ex3=fun (ex1); /*is here diagnostics [C ++ Error] Unit1.cpp (24): E2285 Could not
find a match for 'a1:: operator = (a1)' */

return 0;
}

The argument of function "fun" pass by value. The result comes back by value too.

If class a1 have member: a1& operator = (const a1& a) {v=a.v; return *this;},
error do not occur. But return value of "fun" is not const. I want return
non-const value from "fun", but I may not make it.

Questions:
1. Why returned value is not cast to type of the reference?
2. How to overcome it?
Thank
Basil
Jul 22 '05 #1
5 1323

"Basil" <bu******@fastwel.ru> wrote in message
news:4d**************************@posting.google.c om...
Hello.

Compiler BCB60.
There is a simple example:

class a1 {
int v;
public:
a1 ():v (0) {}
a1 (int v1):v (v1) {}
a1 (a1& a):v (a.v) {}
a1& operator = (a1& a) {v=a.v; return *this;}
};

a1 fun (a1 a) {
a1 ex=a;
return ex;
}

int main (int argc, char* argv [])
{

a1 ex1 (1);
a1 ex2 (2);
a1 ex3 (3);

ex2=ex1;
ex3=fun (ex1); /*is here diagnostics [C ++ Error] Unit1.cpp (24): E2285
Could not
find a match for 'a1:: operator = (a1)' */

return 0;
}

The argument of function "fun" pass by value. The result comes back by
value too.

If class a1 have member: a1& operator = (const a1& a) {v=a.v; return
*this;},
error do not occur. But return value of "fun" is not const. I want return
non-const value from "fun", but I may not make it.

Questions:
1. Why returned value is not cast to type of the reference?
Because it is illegal to bind a non-const reference to a temporary (this
includes the return value of a function).
2. How to overcome it?


Change to this 'a1& operator = (const a1& a) {v=a.v; return *this;}'. Why
would you not want to do this?

John
Jul 22 '05 #2

"Basil" <bu******@fastwel.ru> wrote in message
Hello.

Compiler BCB60.
There is a simple example:
The rule of the game is that you cannot bind an rvalue to a non-const
reference. On a historical note this was allowed originally in the language
but was found quite unsafe and so was dropped eventually.
class a1 {
int v;
public:
a1 ():v (0) {}
a1 (int v1):v (v1) {} a1 (a1& a):v (a.v) {}
a1& operator = (a1& a) {v=a.v; return *this;}
a1 (a1 const& a):v (a.v) {}
a1& operator = (a1 const& a) {v=a.v; return *this;}

};

a1 fun (a1 a) {
a1 ex=a;
return ex;
}

int main (int argc, char* argv [])
{

a1 ex1 (1);
a1 ex2 (2);
a1 ex3 (3);

ex2=ex1;
ex3=fun (ex1); /*is here diagnostics [C ++ Error] Unit1.cpp (24): E2285 Could not find a match for 'a1:: operator = (a1)' */


Sharad
Jul 22 '05 #3
bu******@fastwel.ru (Basil) wrote in message news:<4d**************************@posting.google. com>...
Hello.

Compiler BCB60.
There is a simple example:

class a1 {
int v;
public:
a1 ():v (0) {}
a1 (int v1):v (v1) {}
a1 (a1& a):v (a.v) {}
a1& operator = (a1& a) {v=a.v; return *this;}
Any particular reason why copy constructor and assignment operator
take a1 as non-const? Try
a1 (const a1& a) : v(a.v) {}
a1& operator=(const a1& a) { v=a.v; return *this; }
and it works.

BTW: for a simple class like a1 you could as well reply on the complier
generated opy constructor and assignment operator.
};

a1 fun (a1 a) {
a1 ex=a;
return ex;
}

int main (int argc, char* argv [])
{

a1 ex1 (1);
a1 ex2 (2);
a1 ex3 (3);

ex2=ex1;
ex3=fun (ex1); /*is here diagnostics [C ++ Error] Unit1.cpp (24): E2285 Could not
find a match for 'a1:: operator = (a1)' */

return 0;
}

The argument of function "fun" pass by value. The result comes back by value too.

If class a1 have member: a1& operator = (const a1& a) {v=a.v; return *this;},
error do not occur. But return value of "fun" is not const. I want return
non-const value from "fun", but I may not make it.

Questions:
1. Why returned value is not cast to type of the reference?
The return value from fun(a1 a) is a temporary and can not be bound
to a non-const reference to a1.
2. How to overcome it?
See above.


Thank
Basil


Stephan Brönnimann
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #4
>
Change to this 'a1& operator = (const a1& a) {v=a.v; return *this;}'. Why
would you not want to do this?

John


Because sometimes I want return non-const object (like auto-ptr) from
"fun". In my case "fun" is library function and I want use it for all
object (const and non-const) without modify. If I want return "const"
object from function, I write:

const a1 fun (a1 a) {
a1 ex=a;
return ex;
}

I do not like that compiler insert "const" modifier whitout my
permission.

Thank
Basil
Jul 22 '05 #5

"Basil" <bu******@fastwel.ru> wrote in message
news:4d**************************@posting.google.c om...

Change to this 'a1& operator = (const a1& a) {v=a.v; return *this;}'. Why
would you not want to do this?

John


Because sometimes I want return non-const object (like auto-ptr) from
"fun". In my case "fun" is library function and I want use it for all
object (const and non-const) without modify. If I want return "const"
object from function, I write:

const a1 fun (a1 a) {
a1 ex=a;
return ex;
}

I do not like that compiler insert "const" modifier whitout my
permission.

Thank
Basil


class a1
{
public:
a1& operator = (const a1& a) {v=a.v; return *this;}
int v;
};

a1 fun (a1 a) {
a1 ex=a;
return ex;
}

a1 x;
a1 y;
x = fun(y);

This code, using const, compiles and runs as expected, so what exactly do
you think the problem is? I think you don't understand const.

john
Jul 22 '05 #6

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
16
by: the.duckman | last post by:
G'Day. Anybodey got an idea on this problem. Say I have a function object doCast(object obj, Type t); It's job is to cast the obect (obj) to a new type (t) and return it. Sounds so...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.