Um... No. You're assigning the POINTERS there, not the objects
themselves; the result would be both pointers pointing to the same
object in memory and the second object not being referenced by anything
(memory leak, actually).

What you meant is...

*O2 = *O1;

This is relatively safe to use, but I believe there might be issues with
doing that on objects that "virtual" components in them. I personally
just do it the long way: create a "CObject::Copy( CObject *pSource );"
function, as described below. Besides, a "Copy" function is sometimes
necessary if you allocate memory within the object; if you just copy the
pointers, both pointers will reference the same memory and get real
messy if one disposes it prematurely.

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]] On Behalf Of Miguel
Aleman
Sent: Monday, February 25, 2002 7:28 AM
To: [EMAIL PROTECTED]
Subject: Re: [hlcoders] Dum C++ pointer question


You should overload the = operator so you can simply do this.

CObject *O1 = new CObject( 0, 1, "Hello" );
CObject *O2 = new CObject( );

O2 = O1;

Now O2 is an exact copy with the same values but in a different location
in memory.

----- Original Message -----
From: "Varlock" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, February 25, 2002 3:36 AM
Subject: Re: [hlcoders] Dum C++ pointer question


>     A pointer is just a variable containing the address to a location
> in memory, so it would be expected that assigning the pointer like
> that would just make ob2 point to ob1. To do what you want to do, I
> would recommend a copy constructor. You could also overload the
> assignment operator.
>
> class CObject
> {
> public:
>     // blah blah blah
>
>     CObject( const CObject &copy );
> };
>
> CObject :: CObject( const CObject &copy )
> {
>     m_iInteger = copy.m_iInteger;
>     m_flFloat = copy.m_flFloat;
>
>     // assign any other values here
> }
>
>     I think that's about it. Watch out for pointers and strings in the
copy
> constructor, since you probably don't want to just assign them like
> above. You probably want to use functions like strcpy and memcpy for
> those.
>
> - Varlock
>
> ----- Original Message -----
> From: "Tom" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, February 25, 2002 12:26 AM
> Subject: [hlcoders] Dum C++ pointer question
>
>
> > This is a multi-part message in MIME format.
> > --
> > [ Picked text/plain from multipart/alternative ]
> > Is there anyway to make another isntance of an object by using
> > another
> objects pointer as a base?
> >
> > So I mean like if I have:
> >
> > CObject *ob1 = new Object(blah,blah,blah,blah);
> >
> > and then I want ob2 to be exactly the same as ob2, is it possable
> > just
to
> copy ob1? (i.e. dont need to write everything out).
> >
> > I tried just doing
> > CObject *ob2 = ob1, but then ob2 is actually ob1 and you end up with

> > two
> pointers to one object and not two pointers to two objetcs
> > --
> >
> > _______________________________________________
> > To unsubscribe, edit your list preferences, or view the list
> > archives,
> please visit:
> > http://list.valvesoftware.com/mailman/listinfo/hlcoders
> >
>
> _______________________________________________
> To unsubscribe, edit your list preferences, or view the list archives,
please visit:
> http://list.valvesoftware.com/mailman/listinfo/hlcoders
>
>

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit: http://list.valvesoftware.com/mailman/listinfo/hlcoders


_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to