Dear Readers,
Is it possible to forbid conversion from this or use of this in general
except where it is explicitly wanted?
Reason:
I changed my program from using normal pointers to classes A, ...
typedef A * APtr;
to a shared pointer
typedef boost::shared_p tr<A> APtr;
Now, it crashes because of statements like this
// call
DoSomething(thi s);
.....
// implementation
void DoSomething(APt r a)
{
// do nothing with a
}
Obviously, "this" is converted to a shared ptr locally. Outside the function
DoSomething() the shared ptr is destroyed and hence it tries to delete the
class where "this" points, too. This is clearly not wanted.
Greetings,
Many thanks in advance
Ernst 14 2242
On Wed, 7 Jan 2004 14:53:45 +0100, "Ernst Murnleitner"
<mu******@awite .de> wrote: Dear Readers,
Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted?
Reason:
I changed my program from using normal pointers to classes A, ...
typedef A * APtr;
to a shared pointer
typedef boost::shared_p tr<A> APtr;
Now, it crashes because of statements like this
// call DoSomething(th is);
.... // implementation void DoSomething(APt r a) { // do nothing with a }
That shouldn't compile - the constructor of shared_ptr taking a T* is
explicit, so a pointer can't implicitly convert to a shared_ptr.
Obviously, "this" is converted to a shared ptr locally. Outside the function DoSomething( ) the shared ptr is destroyed and hence it tries to delete the class where "this" points, too. This is clearly not wanted.
You have a few choices, depending on what DoSomething does. If
DoSomething doesn't hold onto a reference to a, then you can change it
to:
void DoSomething(A& aref)
{
}
If it does hold onto a reference to a, but you know it will release
that reference before a is destroyed (which you probably can't
guarantee), then you could do:
struct null_deleter
{
void operator()(void const *) const
{
}
};
//...
DoSomething(APt r(this, null_deleter()) );
The safe approach is to make a shared_ptr from the this pointer. This
is only possible if the A object was created as a shared_ptr in the
first place. Follow the instructions here: http://www.boost.org/libs/smart_ptr/...html#from_this
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"Ernst Murnleitner" <mu******@awite .de> wrote in message news:bt******** ****@ID-130107.news.uni-berlin.de... Obviously, "this" is converted to a shared ptr locally. Outside the function DoSomething() the shared ptr is destroyed and hence it tries to delete the class where "this" points, too. This is clearly not wanted.
shared_pointer( T*) isn't declared explicit? Seems like it ought to be.
> That shouldn't compile - the constructor of shared_ptr taking a T* is explicit, so a pointer can't implicitly convert to a shared_ptr.
With gcc 2.95 it compiles:
// -------------------------------------
#include <iostream.h>
#include <stdlib.h>
#include <boost/shared_ptr.hpp>
class A;
typedef boost::shared_p tr<A> APtr;
class A {
public:
A(){};
virtual ~A(){};
virtual void f(){func(this); };
void func(APtr p) {cout << "func(APtr) " << std::endl;};
};
int main(int argc, char *argv[])
{
A a;
a.f();
return EXIT_SUCCESS;
}
// -------------------------------------
"Ron Natalie" <ro*@sensor.com > schrieb im Newsbeitrag
news:3f******** *************** @news.newshosti ng.com... "Ernst Murnleitner" <mu******@awite .de> wrote in message
news:bt******** ****@ID-130107.news.uni-berlin.de... Obviously, "this" is converted to a shared ptr locally. Outside the
function DoSomething() the shared ptr is destroyed and hence it tries to delete
the class where "this" points, too. This is clearly not wanted.
shared_pointer( T*) isn't declared explicit? Seems like it ought to be.
At least with the old gcc 2.95 it need not! But I tried it with gcc 3.3 also
and at least there was the expected error.
(I still use the gcc 2.95 as the gcc 3.3 needs 20times longer for certain
source files).
Greetings
Ernst
On Wed, 7 Jan 2004 16:46:03 +0100, "Ernst Murnleitner"
<mu******@awite .de> wrote: That shouldn't compile - the constructor of shared_ptr taking a T* is explicit, so a pointer can't implicitly convert to a shared_ptr.
With gcc 2.95 it compiles:
That's a shame - you should upgrade. It suspect it fails with all of
the compilers I have (GCC 3.2, Comeau C++ 4.3, VC 7.1, VC 6?), since
it is non-standard.
With this change:
#include <iostream.h>
to
#include <iostream>
using namespace std;
Comeau gives this error:
"main.cpp", line 22: error: no suitable constructor exists to convert
from
"A *" to "boost::shared_ ptr<A>"
virtual void f(){func(this); };
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"Ernst Murnleitner" <mu******@awite .de> wrote in message
news:bt******** ****@ID-130107.news.uni-berlin.de... "Ron Natalie" <ro*@sensor.com > schrieb im Newsbeitrag news:3f******** *************** @news.newshosti ng.com... "Ernst Murnleitner" <mu******@awite .de> wrote in message news:bt******** ****@ID-130107.news.uni-berlin.de...> Obviously, "this" is converted to a shared ptr locally. Outside the function DoSomething() the shared ptr is destroyed and hence it tries to delete the class where "this" points, too. This is clearly not wanted.
shared_pointer( T*) isn't declared explicit? Seems like it ought to be.
At least with the old gcc 2.95 it need not! But I tried it with gcc 3.3
also and at least there was the expected error.
The library now contains a enable_shared_f rom_this<>() function. You might
look up http://www.boost.org/libs/smart_ptr/sp_techniques.html (I still use the gcc 2.95 as the gcc 3.3 needs 20times longer for certain source files).
However, I´d recommend using gcc 3.3 and you probably might want to go
through your code for unnecessary compile time dependencies.
Regards
Chris
> The library now contains a enable_shared_f rom_this<>() function. You might look up http://www.boost.org/libs/smart_ptr/sp_techniques.html
I found this just before. Now I wonder if it is possible to use
shared_from_thi s();
during construction of an object. I want to use it in the base class and
only store it in a std::vector. It would be used only after complete
construction but is needed during construction, as childs are constructed
from withing the constructor of the parent. The childs eventually get a
pointer to the parent for later use.
Greetings
Ernst
"Ernst Murnleitner" <mu******@awite .de> wrote in message
news:bt******** ****@ID-130107.news.uni-berlin.de... The library now contains a enable_shared_f rom_this<>() function. You
might look up http://www.boost.org/libs/smart_ptr/sp_techniques.html
I found this just before. Now I wonder if it is possible to use
shared_from_thi s();
during construction of an object. I want to use it in the base class and only store it in a std::vector. It would be used only after complete construction but is needed during construction, as childs are constructed from withing the constructor of the parent. The childs eventually get a pointer to the parent for later use.
Greetings
Ernst
Could you give an explicit code example of what you want to do, please
because creating childs within the ctor of a base sounds very much like
flawed design to me. But I would need to see some code. Anyway, if I
understand your description correctly then there should be no problem.
Still, you should test it as I´ve not used shared_from_thi s() so far.
Regards
Chris
"Ernst Murnleitner" <mu******@awite .de> wrote: Dear Readers,
Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted?
Reason:
I changed my program from using normal pointers to classes A, ...
typedef A * APtr;
to a shared pointer
typedef boost::shared_p tr<A> APtr;
I would call the above a bad idea. Not just because of the problem
cited, but because there is no way, in general to guarantee that all
APtr's in the original code actually point to things that must be
deleted at some point in the future. For example:
typedef A* APtr;
void foo( APtr a ) { }
int main() {
A myA;
foo( &myA );
}
is quite valid until you change the typedef...
Now if you can guarantee that all of the things held by an APtr have
actually been newed, then using a SmartPointer class that has some sort
of global way of knowing the number of references would be OK.
Of course, you still have the problem that some places in the code may
be using an A* rather than an APtr. :-( This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Stevens |
last post by:
Probably the wrong wording but since I'm not a scripter I won't claim
to know what I'm talking about.
I got this script from www.htmlgoodies.com
<script language="JavaScript">
<!--
window.open ('photos01.html','photogallery',config='height=550,
width=750,toolbar=no, menubar=no, scrollbars=no, resizable=no,
location=no, directories=no,...
|
by: maadhuu |
last post by:
well,i am curious to know what would be the output of the following:
delete this;
basically , comment on this .
|
by: tnhoe |
last post by:
Hi,
<Form method='post' action="next.htm?btn="+"this.myform.myobj.value">
What is the correct syntax for above ?
Regards
Hoe
|
by: aden |
last post by:
I have read the years-old threads on this topic, but I wanted to
confirm what they suggest. . .
Can the this pointer EVER point to a type different from the class that
contains the member function that the this pointer is being used in?
That is, is the type of the this pointer always determined entirely
syntactically (and never...
|
by: Shapper |
last post by:
Hello,
I am accessing a value in a XML value:
news.Load(Server.MapPath("xml/ news.rss"))
newslabel.Text = CType(news.SelectSingleNode("rss version="2.0
"/channel/title").InnerText, String)
The XML file:
<?xml version="1.0"?>
| |
by: relient |
last post by:
Question: Why can't you access a private inherited field from a base
class in a derived class? I have a *theory* of how this works, of
which, I'm not completely sure of but makes logical sense to me. So,
I'm here for an answer (more of a confirmation), hopefully.
First let me say that I know people keep saying; it doesn't work
because the...
|
by: solarin |
last post by:
Hi all.
I'm writting a logger class to write all the debug/info/warning/error
messages in a file. Every time a class needs to send any message,
should send a code (int) and a message (string). I would like to write
also in the file, the class that has sent the message. all the clases
that need to send a message, derives from a base clas:
...
|
by: craig.keightley |
last post by:
I am trying to get the next row within a loop for a script i am
developing...
I need to display a final table row within the table that i have
displayed on the page, but i only want to show it if value of the
current field is not the same value of the next row.
eg:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |