MFC, Pointers and References

Hello,
Suppose I have two pointers in two different functions and I have a public variable, lets say:
sh1(){
Shape *sh1 = new Shape;
global_sh = sh1;
}

sh2(){
Shape *sh2 = new Shape;
global_sh = sh2;
}

What I want is something to store the address of the point sh1 or sh2 in global_sh. Now somewhere else I declare another pointer "sh3" and say sh3 = global_sh. Now if I say sh3.something() then it should return/call the related object. How can i do it?

Saqib
LVL 1
jhapakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SkonenCommented:
Shape *global_sh;

// ....

sh1(){
Shape *sh1 = new Shape;
global_sh = &sh1;
}

sh2() {
Shape *sh2 = new Shape;
global_sh = &sh2;
}

// ...

Shape *sh3;

sh1();
sh3 = global_sh;
sh3->Something();
0
peterchen092700Commented:
Skongen:

> Shape *sh1 = new Shape;
> global_sh = &sh1;

that's bad, since you take the address of a temporary object. looking at the calling code you probably meant

global_sh = sh1;

Saquib:
With above correction, this would work fine.
However, you have another problem: who is going to delete the shapes?

If you call

sh1();
sh2();

at least the first allocation leaks, because it is not accessible anymore.
To solve that, I'd recommmend using reference counted pointers (e.g. boost::shared_ptr)
0
SkonenCommented:
Sorry about that, looking back at that I see no reasoning in what I did. I assumed he was going to delete global_sh afterwards so I didn't mention anything about freeing the allocated memory once he was done.
0
jhapakAuthor Commented:
ok,
I have checked smart pointers, cool thing.

But, my problem is that am using CObList and I have different classes in it. So when ever I put an object on the list it gives me memory leak. Any idea?? How can I delete manually a pointer then?
0
peterchen092700Commented:
Wit h raw pointers, it very much depends on the flow of your *entire* program, how and if you can avoid memory leaks. Besides such a discussion is beyond EE (since we would have to analyze your entire prog), I would strongly disfavor it as a huge pitfall when any change i introduced.

My *recommendation* is to:

- use a typedef boost:shared_ptr<CObject> CObjectPtr
- use a CList<CObjectPtr> or a std::list<CObjectPtr> instead of th CObList
- use a CObjectPtr for the global variable.

Note: this is just an outline, I don't know if there are specific features of CObList that you would lose this way. It might be a little bit mroe work to begin with. But, "smart pointers have changed my life", and I don't want to go back.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.