How to initialize a class member shared pointer, to NULL?

P

Praveen.Boinee

Dear C++'ers

I am experimenting with boost shared_pointers.
My Classes

class A
{

private:
typedef boost::shared_ptr<A> ASharedPtr;
ASharedPtr m_parent; // Keep Track of Parent Node

float m_score; // Total Distance

public:
A(float p_score, ASharedPtr p_ptr ): m_parent(p_ptr)
{
m_score = p_score;
}
};

If i say

ASharedPtr a(new Node(ay, p_ptr));
// where p_ptr already initialzed shared ptr things are quite O.K

but
ASharedPtr a(new Node(ay, 0)); // is not working

Now my question is how to set m_parent to be NULL or 0. I am on VC++
6.0 debugger. It shows m_parent as CXX0030: Error: expression cannot be
evaluated.
I hope I am clear.

Thanking you
 
K

kwikius

Now my question is how to set m_parent to be NULL or 0.

You can use deafault constructor e.g:

#include <boost/shared_ptr.hpp>
#include <iostream>

class my_class{
public:
boost::shared_ptr<my_class> m_p;

my_class( boost::shared_ptr<my_class> p_in): m_p(p_in){}
};


int main()
{
// needs to be done like this else looks like function definition
my_class x = my_class(boost::shared_ptr<my_class>());

if ( ! x.m_p){
std::cout << "OK .. x.m_p is empty\n";
}
else{
std::cout << "something went wrong\n";
}
}

regards
Andy Little
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top