Pointer question

Discussion in 'C++' started by BCC, Jul 22, 2003.

  1. BCC

    BCC Guest

    Hi,

    If I have something like this:
    CMyClass* p1;
    CMyClass* p2;

    I know I can do:
    p1 = new CMyClass();
    p2 = p1;

    Now both my pointers point to the same object. Cool so far.

    But is it possible to do this -before- you initialize your pointers?

    p2 = p1;
    p1 = new CMyClass();

    and have both pointers point to the same object?

    I don't think so, but thought I'd check for sure.

    Thanks,
    Bryan
    BCC, Jul 22, 2003
    #1
    1. Advertising

  2. BCC

    John Carson Guest

    "BCC" <> wrote in message
    news:wI3Ta.2868$
    > Hi,
    >
    > If I have something like this:
    > CMyClass* p1;
    > CMyClass* p2;
    >
    > I know I can do:
    > p1 = new CMyClass();
    > p2 = p1;
    >
    > Now both my pointers point to the same object. Cool so far.
    >
    > But is it possible to do this -before- you initialize your pointers?
    >
    > p2 = p1;
    > p1 = new CMyClass();
    >
    > and have both pointers point to the same object?
    >
    > I don't think so, but thought I'd check for sure.
    >
    > Thanks,
    > Bryan


    You need to use a reference, which is a kind of alias.

    CMyClass* p1;

    // makes p2 a reference to a pointer and initialises it to
    // be an reference to the pointer p1
    CMyClass* &p2 = p1;

    p1 = new CMyClass();


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Jul 22, 2003
    #2
    1. Advertising

  3. BCC

    David White Guest

    BCC <> wrote in message
    news:wI3Ta.2868$...
    > Hi,
    >
    > If I have something like this:
    > CMyClass* p1;
    > CMyClass* p2;
    >
    > I know I can do:
    > p1 = new CMyClass();
    > p2 = p1;
    >
    > Now both my pointers point to the same object. Cool so far.
    >
    > But is it possible to do this -before- you initialize your pointers?
    >
    > p2 = p1;
    > p1 = new CMyClass();
    >
    > and have both pointers point to the same object?


    Unless your computer runs on tachyons, then no.

    If p1 has not been initialized before the first line, then there is an
    extremely remote possibility that it will accidentally be the same address
    as that returned by the "new" expression.

    DW
    David White, Jul 22, 2003
    #3
  4. "BCC" <> wrote in message
    news:wI3Ta.2868$...
    > Hi,
    >
    > If I have something like this:
    > CMyClass* p1;
    > CMyClass* p2;
    >
    > I know I can do:
    > p1 = new CMyClass();


    No need of the braces.

    > p2 = p1;
    >
    > Now both my pointers point to the same object. Cool so far.
    >
    > But is it possible to do this -before- you initialize your pointers?


    No.
    >
    > p2 = p1;


    Copy the junk address p1 is holding into p2.

    > p1 = new CMyClass();


    p1 now holds a valid heap address while p2 still holds the junk address.
    >
    > and have both pointers point to the same object?


    You see the difference now?

    --
    With best wishes,
    J.Schafer
    Josephine Schafer, Jul 22, 2003
    #4
  5. BCC

    David White Guest

    David White <> wrote in message
    news:w54Ta.1062$...
    > BCC <> wrote in message
    > If p1 has not been initialized before the first line, then there is an
    > extremely remote possibility that it will accidentally be the same address
    > as that returned by the "new" expression.


    Or this might do it on some compilers:
    p1 = new CMyClass;
    delete p1;
    // now your code
    p2 = p1;
    p1 = new CMyClass();

    DW
    David White, Jul 22, 2003
    #5
  6. "David White" <> wrote in message
    news:Qf4Ta.1063$...
    > David White <> wrote in message
    > news:w54Ta.1062$...
    > > BCC <> wrote in message
    > > If p1 has not been initialized before the first line, then there is an
    > > extremely remote possibility that it will accidentally be the same

    address
    > > as that returned by the "new" expression.

    >
    > Or this might do it on some compilers:
    > p1 = new CMyClass;
    > delete p1;
    > // now your code
    > p2 = p1;
    > p1 = new CMyClass();



    Oi, and now you expect p1 to point to the same object as p2? Now the
    possibility that this works is jus as remote as to assume that an
    uninitialized p1 'accidently' points to p2. Actually, this code is even
    invalid (as I have learned a few days ago), because you are assigning p1 to
    p2.

    regards
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Jul 22, 2003
    #6
  7. "BCC" <> wrote in message
    news:wI3Ta.2868$...
    > Hi,
    >
    > If I have something like this:
    > CMyClass* p1;
    > CMyClass* p2;
    >
    > I know I can do:
    > p1 = new CMyClass();
    > p2 = p1;
    >
    > Now both my pointers point to the same object. Cool so far.
    >
    > But is it possible to do this -before- you initialize your pointers?
    >
    > p2 = p1;
    > p1 = new CMyClass();
    >
    > and have both pointers point to the same object?
    >
    > I don't think so, but thought I'd check for sure.



    As you have it, no, like Josephine and David pointed out already. But
    you can change the code a little, so that p1 always points to the same thing
    p2 points to:

    CMyClass* p2;
    CMyClass*& p1 = p2; // make p1 a reference to a pointer

    p2 = new CMyClass ();
    // now p1 and p2 point to the same object

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Jul 22, 2003
    #7
  8. BCC

    John Ericson Guest

    "Josephine Schafer" <> wrote in message
    news:bfiitc$f9sbe$-berlin.de...
    >
    > "BCC" <> wrote in message
    > news:wI3Ta.2868$...
    > > Hi,
    > >
    > > If I have something like this:
    > > CMyClass* p1;
    > > CMyClass* p2;
    > >
    > > I know I can do:
    > > p1 = new CMyClass();

    >
    > No need of the braces.
    >


    Perhaps the OP is initializing a POD? ;)
    John Ericson, Jul 22, 2003
    #8
  9. BCC

    Default User Guest

    Jakob Bieling wrote:

    > Oi, and now you expect p1 to point to the same object as p2? Now the
    > possibility that this works is jus as remote as to assume that an
    > uninitialized p1 'accidently' points to p2. Actually, this code is even
    > invalid (as I have learned a few days ago), because you are assigning p1 to
    > p2.



    It's not THAT ridiculous. Some people don't understand that
    initializations store values, not operations. I've seen questions that
    indicate people thought:


    int a;
    int b;
    int c = a + b;


    Would always set c to be the value of a + b. So if a or b changed, c
    would automagically do so as well. It's not the way things work of
    course.




    Brian Rodenborn
    Default User, Jul 22, 2003
    #9
  10. BCC

    David White Guest

    Jakob Bieling <> wrote in message
    news:bfj1hh$v4u$03$-online.com...
    > "David White" <> wrote in message
    > news:Qf4Ta.1063$...
    > > David White <> wrote in message
    > > news:w54Ta.1062$...
    > > > BCC <> wrote in message
    > > > If p1 has not been initialized before the first line, then there is an
    > > > extremely remote possibility that it will accidentally be the same

    > address
    > > > as that returned by the "new" expression.

    > >
    > > Or this might do it on some compilers:
    > > p1 = new CMyClass;
    > > delete p1;
    > > // now your code
    > > p2 = p1;
    > > p1 = new CMyClass();

    >
    >
    > Oi, and now you expect p1 to point to the same object as p2?


    No, I said, "Or this might do it on some compilers", which establishes the
    entire post as implementation-dependent. All you need is for the second
    "new" to return the same address as the first, which it might well do
    because the first has been deleted. Regarding the "p2 = p1; ", I don't know
    if it's invalid according to the standard, but, let's face it, is there a
    compiler on Earth that wouldn't simply take the bit pattern in p1 and stick
    it in p2? (again, I said "some compilers").

    > Now the
    > possibility that this works is jus as remote as to assume that an
    > uninitialized p1 'accidently' points to p2.


    I don't think so.

    DW
    David White, Jul 23, 2003
    #10
    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. Replies:
    10
    Views:
    697
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    837
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,250
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    634
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    451
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page