Get copy of object from pointer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Get copy of object from pointer?

    I have a class "Call". I have instantiated a Call object in one class then
    passed a pointer to that object to another class.

    How do I create a *copy* of that Call object using only the pointer?


  • deane_gavin@hotmail.com

    #2
    Re: Get copy of object from pointer?

    Mike wrote:
    [color=blue]
    > I have a class "Call". I have instantiated a Call object in one class then
    > passed a pointer to that object to another class.
    >
    > How do I create a *copy* of that Call object using only the pointer?[/color]

    The same way you get hold of the object pointed to for any other
    purpose. Dereference the pointer.

    Call c1;
    Call* p = &c1;

    p is a pointer to a Call object.

    Call c2 = *p;

    c2 is created as a copy of the object pointed to by p.

    Gavin Deane

    Comment

    • Alf P. Steinbach

      #3
      Re: Get copy of object from pointer?

      * Mike:[color=blue]
      > I have a class "Call". I have instantiated a Call object in one class then
      > passed a pointer to that object to another class.
      >
      > How do I create a *copy* of that Call object using only the pointer?[/color]

      If you know that the dynamic type of object pointed to is really Call,
      and not some unknown class derived from Call, you can do

      Call copyOfCall = *p;

      or e.g.

      Call* pCopyOfCall = new Call( *p );

      Otherwise, if you don't want slicing you can use cloning.

      FAQ item 20.8 describes the basics of cloning,
      <url: <url:
      http://www.parashift.c om/c++-faq-lite/virtual-functions.html# faq-20.8>.

      A more in-depth discussion of cloning, especially the use of copy
      constructors for cloning in a class hierarchy, is section 1.3.6 of
      <url:
      http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_bet a.doc.pdf>.



      Conspiracy theory of the day. How come I gave the same two URLs in this
      and my previous posting to clc++? Is there a global conspiracy to ask
      questions where the answers naturally list those two URLs?

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • Greg

        #4
        Re: Get copy of object from pointer?

        deane_gavin@hot mail.com wrote:[color=blue]
        > Mike wrote:
        >[color=green]
        > > I have a class "Call". I have instantiated a Call object in one class then
        > > passed a pointer to that object to another class.
        > >
        > > How do I create a *copy* of that Call object using only the pointer?[/color]
        >
        > The same way you get hold of the object pointed to for any other
        > purpose. Dereference the pointer.
        >
        > Call c1;
        > Call* p = &c1;
        >
        > p is a pointer to a Call object.
        >
        > Call c2 = *p;
        >
        > c2 is created as a copy of the object pointed to by p.[/color]

        Not necessarily. If p were actually a pointer to a subclass of Call,
        then c2 would have ended up with a "sliced" copy of p, and c2 and p
        would not be of the same type. For this reason, it is best to use
        whatever copy operation the class has defined. In other words, ask p to
        copy itself, rather than perform a copy-by-value operation by
        dereferencing the pointer and that assumes that p is a pointer to an
        exact type.

        Greg

        Comment

        • davidrubin@warpmail.net

          #5
          Re: Get copy of object from pointer?


          Mike wrote:[color=blue]
          > I have a class "Call". I have instantiated a Call object in one class then
          > passed a pointer to that object to another class.
          >
          > How do I create a *copy* of that Call object using only the pointer?[/color]

          Typically, you don't. Either (a) 'Call' has value semantics, and you
          copy the value, or you copy (b) the raw pointer, or (c) a smart pointer
          (e.g., one that does reference counting).

          Comment

          Working...