E
Ernst Murnleitner
Hello,
in 2 other threads I had questions partly related to shared_ptr.
I changed my normal pointers to a class to shared_ptr. (i.e.
boost::shared_ptr).
I thought, the use of shared_ptr is more save. But after this my program
crashed by several reasons (see below). But maybe there would be an easy
solution which I cannot see now.
1) I mixed normal pointers and shared _ptr at some places. This is very
ugly, for example:
class A;
shared_ptr<A> a = new A;
A * p = a.this();
{
shared_ptr<A> a2 = p;
} // the object will be deleted here!!!
a->f(); // crash?
Question: Is it possible to make that a shared_ptr can only be initialised
by another shared_ptr or by the new expression?
2) shared_from_this():
template enable_shared_from_this cannot be used in a class with 2 instances
of the same base class?
In my hierarchy, I have the same base class twice in one object
(non-virtual). As this base class has boost::enable_shared_from_this<> as
Base again, I cannot compile (ambigous...).
Maybe I could circumvent this by making one Baseclass a virtual base class.
But wasn't there a restriction by using virtual base classes? (As far as I
can remember it was something with RTTI or with casts).
I know that 2 base classes is not a good idea, but there was a problem by
using virtual inheritance.
Greetings
Ernst
in 2 other threads I had questions partly related to shared_ptr.
I changed my normal pointers to a class to shared_ptr. (i.e.
boost::shared_ptr).
I thought, the use of shared_ptr is more save. But after this my program
crashed by several reasons (see below). But maybe there would be an easy
solution which I cannot see now.
1) I mixed normal pointers and shared _ptr at some places. This is very
ugly, for example:
class A;
shared_ptr<A> a = new A;
A * p = a.this();
{
shared_ptr<A> a2 = p;
} // the object will be deleted here!!!
a->f(); // crash?
Question: Is it possible to make that a shared_ptr can only be initialised
by another shared_ptr or by the new expression?
2) shared_from_this():
template enable_shared_from_this cannot be used in a class with 2 instances
of the same base class?
In my hierarchy, I have the same base class twice in one object
(non-virtual). As this base class has boost::enable_shared_from_this<> as
Base again, I cannot compile (ambigous...).
Maybe I could circumvent this by making one Baseclass a virtual base class.
But wasn't there a restriction by using virtual base classes? (As far as I
can remember it was something with RTTI or with casts).
I know that 2 base classes is not a good idea, but there was a problem by
using virtual inheritance.
Greetings
Ernst