Problems copying object pointer

A

Angus

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?
 
B

Boris Glawe

Angus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top