copying object from reference

T

techieV

Hi,

I am struck at below problem while working on a issue.

Is this correct in cpp - sending a reference to an object in a
function call and while receiving receive it in a local object? The
real call to func1 is - someObj.func1( getClass1Obj()); and this 'Get'
function returns a reference to const object.

function prototype -
class someClass
{
public:
func1( Class1 object);
}

and calling this function as - someObj.func1( &class1obj);

Thank you in advance.
 
P

PhilipDexter

Hi,

I am struck at below problem while working on a issue.

Is this correct in cpp - sending a reference to an object in a
function call and while receiving receive it in a local object? The
real call to func1 is - someObj.func1( getClass1Obj()); and this 'Get'
function returns a reference to const object.

function prototype -
class someClass
{
     public:
     func1( Class1 object);

}

and calling this function as - someObj.func1( &class1obj);

Thank you in advance.

I believe you'll get an error that you cannot convert a const ref to a
ref. Other than that it will work, just make sure you have a copy
constructor!
 
M

Marcel Müller

techieV said:
I am struck at below problem while working on a issue.

Is this correct in cpp - sending a reference to an object in a
function call and while receiving receive it in a local object?

Yes. This will implicitly invoke the copy constructor (as long as it
exists).
> The
real call to func1 is - someObj.func1( getClass1Obj()); and this 'Get'
function returns a reference to const object.

However, the call to getClass1Obj() implies that it might be a factory
function. And in this case, it might be a logical error from the
applications point of view to copy the result. If the application code
is well designed, it has no (visible) copy constructor for Class1 in
this case. And then you will get a compiler error.

function prototype -
class someClass
{
public:
func1( Class1 object);
}

and calling this function as - someObj.func1( &class1obj);

This is an error. The &-Operator returns a pointer and func1 takes an
object.
(Of course, unless Class1 can be implicitly constructed from this
pointer. But this is unlikely.)


Marcel
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top