Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems copying object pointer

Reply
Thread Tools

Problems copying object pointer

 
 
Angus
Guest
Posts: n/a
 
      04-11-2008
I have a function called subscribe which passes a pointer to a
'subscribing' object to a class which will call functions on this
object.

Here is the function:
void CEventSelect::subscribe(CEventSelect* user)
{
m_subscriber = user
m_coll.push_back(user);
}

I declare this member like this:

CMySubscriber* m_subscriber;

If I copy using = in this way, m_subscriber becomes invalid as soon as
the subscribe function finishes.

If I put object in eg a vector then it works fine. Because I suppose
vector is doing a proper copy. How would I do a sim,ilar copy without
having to use a vector?
 
Reply With Quote
 
 
 
 
Boris Glawe
Guest
Posts: n/a
 
      04-11-2008
Angus schrieb:
> I have a function called subscribe which passes a pointer to a
> 'subscribing' object to a class which will call functions on this
> object.
>
> Here is the function:
> void CEventSelect::subscribe(CEventSelect* user)
> {
> m_subscriber = user
> m_coll.push_back(user);
> }
>
> I declare this member like this:
>
> CMySubscriber* m_subscriber;
>
> If I copy using = in this way, m_subscriber becomes invalid as soon as
> the subscribe function finishes.


Why will m_subscriber become invalid?

The method CEventSelect::subscriber gets only a pointer. At the end of
the scope of the subscribe method, the pointer will be deleted, but not
the user object itself. As you have copied the pointer to m_subscriber,
there is no obvious reason,why it should become invalid!?


>
> If I put object in eg a vector then it works fine. Because I suppose
> vector is doing a proper copy. How would I do a sim,ilar copy without
> having to use a vector?


If you have problems you have to post more code in order to specify the
reason for your problems.

greets Boris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments