473,485 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Smart pointers - passing 'this' around

Another smart pointer problem. I have a tree structure. The idea is for
parents to have SmartPtr's to the children, with children holding a weak
pointer (WeakPtr) back to the parent. That's great, but how do I
implement the link from child to parent?

Here's where the problem occurs:

SmartPtr<ContainerNode>
ContainerNode :: CreateChildContainer()
{
SmartPtr<ContainerNode> child( new ContainerNode );

// a) This would cause 'this' to be deallocated at end of scope!
// SmartPtr<ContainerNode> smart( this );
// child->SetParent( smart );

// b) Can't assign raw ptr to WeakPtr in SetParent.
// child->SetParent( this );

return child;
}

So how do I pass 'this' to the child? A hack to bump up the strong ref
count in case a) so SmartPtr won't deallocate 'this'? Ugh.
Instead, the calling code could of course say:

SmartPtr<ContainerNode> parent;
SmartPtr<ContainerCode> child;
....
child = parent->CreateChildContainer();
child->SetParent( parent );

and it would work fine. The WeakPtr can be assigned to from the
SmartPtr. But the caller shouldn't have to do that.

Or am I wrong in assuming I shouldn't be putting raw pointers into weak
pointers? WeakPtr::getPtr() would return NULL since there'd never be a
strong ref to the ptr.

This seems like a ridiculous thing to be stumbling on, but I'm not sure
what to do. Thanks for any help.

-jim
Jul 19 '05 #1
1 2359
"Bonzo" <do**@email.me> wrote in message
news:do************************@syrcnyrdrs-02-ge0.nyroc.rr.com...
Another smart pointer problem. I have a tree structure. The idea is for
parents to have SmartPtr's to the children, with children holding a weak
pointer (WeakPtr) back to the parent. That's great, but how do I
implement the link from child to parent?
Why not just use a raw pointer? Smart pointers are generally used for memory
management, but I don't suppose you want the children managing memory for
the parents. (Although sometimes it works that way in real life. heh.)

Here's where the problem occurs:

SmartPtr<ContainerNode>
ContainerNode :: CreateChildContainer()
{
SmartPtr<ContainerNode> child( new ContainerNode );

// a) This would cause 'this' to be deallocated at end of scope!
// SmartPtr<ContainerNode> smart( this );
// child->SetParent( smart );

// b) Can't assign raw ptr to WeakPtr in SetParent.
// child->SetParent( this );

return child;
}


I don't understand the code fragment above but a typical implementation
would be like:

class Child
{
private:
Parent *m_pparent;
public:
Child(Parent (i_pparent) : m_pparent(i_pparent) {}
...
};

class Parent
{
public:
typedef boost::shared_ptr<Child> ChildPtr;
ChildPtr spawn() {return ChildPtr(new Child(this));
...
};

<snip>

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5045
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
11
2237
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
9
2117
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
27
3364
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
59
5051
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
92
4997
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
33
5013
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
35487
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
54
11903
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5418
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
247
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.