copying object from reference

Discussion in 'C++' started by techieV, Feb 21, 2011.

  1. techieV

    techieV Guest

    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.
     
    techieV, Feb 21, 2011
    #1
    1. Advertising

  2. techieV

    PhilipDexter Guest

    On Feb 21, 7:20 am, techieV <> wrote:
    > 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!
     
    PhilipDexter, Feb 21, 2011
    #2
    1. Advertising

  3. techieV wrote:
    > 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
     
    Marcel Müller, Feb 21, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Copying A Control Not As A Reference

    Nathan Sokalski, Jun 12, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    4,745
    Herfried K. Wagner [MVP]
    Jun 12, 2006
  2. Replies:
    1
    Views:
    515
    Mark Rae [MVP]
    Sep 20, 2007
  3. , India
    Replies:
    1
    Views:
    699
    Pascal J. Bourguignon
    Apr 22, 2008
  4. Nathan Sokalski

    Copying A Control Not As A Reference

    Nathan Sokalski, Jun 12, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    139
    Herfried K. Wagner [MVP]
    Jun 12, 2006
  5. Sparhawk
    Replies:
    5
    Views:
    125
    Harag
    Aug 29, 2004
Loading...

Share This Page