Newbie Q: returning objects from the freestore

Discussion in 'C++' started by Scott, Oct 12, 2003.

  1. Scott

    Scott Guest

    Can you have as a return value for a function an object that has been
    created on the free store? Seems like returning a pointer is the only thing
    that could do this.
    Thanks.
    Scott, Oct 12, 2003
    #1
    1. Advertising

  2. Scott

    Moonlit Guest

    Hi,


    "Scott" <> wrote in message
    news:...
    > Can you have as a return value for a function an object that has been
    > created on the free store? Seems like returning a pointer is the only

    thing
    > that could do this.

    Nope, a reference can be used (of course internally this is the same thing
    as a pointer although some people don't want you to say this).

    class CObject
    {
    };

    CObject& func()
    {
    return *new CObject;
    }
    int main()
    {
    CObject& Object = func();

    delete &Object;

    return 0;
    }


    But it certainly isn't good programming practice. It works but should look
    like this.

    class CObject
    {
    };

    CObject* func()
    {
    return new CObject;
    }
    int main()
    {
    CObject *Object = func();

    delete Object;

    return 0;
    }


    > Thanks.
    >
    >
    >
    >
    >

    And that's pointer of course.

    Regards, Ron AF Greve.
    Moonlit, Oct 12, 2003
    #2
    1. Advertising

  3. Scott

    Scott Guest

    Right, so the reference can be done, but isn't recommended, and the pointer
    works fine in my program. But returning the object from the free store just
    isn't done.

    Scott
    "Moonlit" <> wrote in message
    news:3f88ba83$0$58716$4all.nl...
    > Hi,
    >
    >
    > "Scott" <> wrote in message
    > news:...
    > > Can you have as a return value for a function an object that has been
    > > created on the free store? Seems like returning a pointer is the only

    > thing
    > > that could do this.

    > Nope, a reference can be used (of course internally this is the same thing
    > as a pointer although some people don't want you to say this).
    >
    > class CObject
    > {
    > };
    >
    > CObject& func()
    > {
    > return *new CObject;
    > }
    > int main()
    > {
    > CObject& Object = func();
    >
    > delete &Object;
    >
    > return 0;
    > }
    >
    >
    > But it certainly isn't good programming practice. It works but should look
    > like this.
    >
    > class CObject
    > {
    > };
    >
    > CObject* func()
    > {
    > return new CObject;
    > }
    > int main()
    > {
    > CObject *Object = func();
    >
    > delete Object;
    >
    > return 0;
    > }
    >
    >
    > > Thanks.
    > >
    > >
    > >
    > >
    > >

    > And that's pointer of course.
    >
    > Regards, Ron AF Greve.
    >
    >
    Scott, Oct 12, 2003
    #3
  4. Scott

    Moonlit Guest

    Hi,

    Yes, you can only use a pointer (and reference), when dynamically allocated.
    The other method would be to not allocate on the free store and return the
    object, however then a copy is made.

    CObject func()
    {
    CObject Object;
    return Object;
    }

    int main()
    {
    CObject Object = func(); // Copies the object from the function
    }

    Regards, Ron AF Greve.


    "Scott" <> wrote in message
    news:...
    > Right, so the reference can be done, but isn't recommended, and the

    pointer
    > works fine in my program. But returning the object from the free store

    just
    > isn't done.
    >
    > Scott
    > "Moonlit" <> wrote in message
    > news:3f88ba83$0$58716$4all.nl...
    > > Hi,
    > >
    > >
    > > "Scott" <> wrote in message
    > > news:...
    > > > Can you have as a return value for a function an object that has been
    > > > created on the free store? Seems like returning a pointer is the only

    > > thing
    > > > that could do this.

    > > Nope, a reference can be used (of course internally this is the same

    thing
    > > as a pointer although some people don't want you to say this).
    > >
    > > class CObject
    > > {
    > > };
    > >
    > > CObject& func()
    > > {
    > > return *new CObject;
    > > }
    > > int main()
    > > {
    > > CObject& Object = func();
    > >
    > > delete &Object;
    > >
    > > return 0;
    > > }
    > >
    > >
    > > But it certainly isn't good programming practice. It works but should

    look
    > > like this.
    > >
    > > class CObject
    > > {
    > > };
    > >
    > > CObject* func()
    > > {
    > > return new CObject;
    > > }
    > > int main()
    > > {
    > > CObject *Object = func();
    > >
    > > delete Object;
    > >
    > > return 0;
    > > }
    > >
    > >
    > > > Thanks.
    > > >
    > > >
    > > >
    > > >
    > > >

    > > And that's pointer of course.
    > >
    > > Regards, Ron AF Greve.
    > >
    > >

    >
    >
    Moonlit, Oct 12, 2003
    #4
  5. Scott

    Scott Guest

    Thanks Moonlit:

    I have better answers from you than from 3 reference works I've been pouring
    through. I really appreciate the code samples as well.

    Scott
    "Moonlit" <> wrote in message
    news:3f89b26c$0$58709$4all.nl...
    > Hi,
    >
    > Yes, you can only use a pointer (and reference), when dynamically

    allocated.
    > The other method would be to not allocate on the free store and return the
    > object, however then a copy is made.
    >
    > CObject func()
    > {
    > CObject Object;
    > return Object;
    > }
    >
    > int main()
    > {
    > CObject Object = func(); // Copies the object from the function
    > }
    >
    > Regards, Ron AF Greve.
    >
    >
    > "Scott" <> wrote in message
    > news:...
    > > Right, so the reference can be done, but isn't recommended, and the

    > pointer
    > > works fine in my program. But returning the object from the free store

    > just
    > > isn't done.
    > >
    > > Scott
    > > "Moonlit" <> wrote in message
    > > news:3f88ba83$0$58716$4all.nl...
    > > > Hi,
    > > >
    > > >
    > > > "Scott" <> wrote in message
    > > > news:...
    > > > > Can you have as a return value for a function an object that has

    been
    > > > > created on the free store? Seems like returning a pointer is the

    only
    > > > thing
    > > > > that could do this.
    > > > Nope, a reference can be used (of course internally this is the same

    > thing
    > > > as a pointer although some people don't want you to say this).
    > > >
    > > > class CObject
    > > > {
    > > > };
    > > >
    > > > CObject& func()
    > > > {
    > > > return *new CObject;
    > > > }
    > > > int main()
    > > > {
    > > > CObject& Object = func();
    > > >
    > > > delete &Object;
    > > >
    > > > return 0;
    > > > }
    > > >
    > > >
    > > > But it certainly isn't good programming practice. It works but should

    > look
    > > > like this.
    > > >
    > > > class CObject
    > > > {
    > > > };
    > > >
    > > > CObject* func()
    > > > {
    > > > return new CObject;
    > > > }
    > > > int main()
    > > > {
    > > > CObject *Object = func();
    > > >
    > > > delete Object;
    > > >
    > > > return 0;
    > > > }
    > > >
    > > >
    > > > > Thanks.
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > And that's pointer of course.
    > > >
    > > > Regards, Ron AF Greve.
    > > >
    > > >

    > >
    > >

    >
    >
    Scott, Oct 12, 2003
    #5
    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. spipyeah
    Replies:
    2
    Views:
    300
    spipyeah
    Aug 25, 2003
  2. Jorge Rivera
    Replies:
    10
    Views:
    5,638
    John Harrison
    May 10, 2004
  3. Tomás

    From the freestore

    Tomás, Mar 24, 2006, in forum: C++
    Replies:
    3
    Views:
    286
    Tomás
    Mar 24, 2006
  4. Philipp

    Freestore allocation etc

    Philipp, Sep 4, 2006, in forum: C++
    Replies:
    18
    Views:
    439
    Philipp
    Sep 6, 2006
  5. yonil
    Replies:
    3
    Views:
    355
    yonil
    Oct 6, 2006
Loading...

Share This Page