why I have not cast from object to reference

B

Basil

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
 
J

John Harrison

Basil said:
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
 
S

Sharad Kala

Basil said:
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
 
S

Stephan Br?nnimann

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
(e-mail address removed)
Open source rating and billing engine for communication networks.
 
B

Basil

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
 
J

John Harrison

Basil said:
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
 

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

Staff online

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top