473,698 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

possibility to forbid use of "this"?

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

Jul 22 '05
14 2253
> 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.


A version, but without smartpointers, is at
http://control.awite.com/awitecontro...trol-api/html/

I am currently cleaning up the source, therefore my questions. I began 2
years ago and the ioverall design didn`t need changes. However, in the
beginning I didn't use more sophisticated programming technics like design
patterns, factory classes etc.

Greetings

Ernst

Jul 22 '05 #11
> 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.
Yes, I only construct them on the heap. And if I only allow a factory to
create my Objects (e.g. as Mr. Carson suggested in another message thread),
I cannot accidentally define automatic variables.
Of course, you still have the problem that some places in the code may
be using an A* rather than an APtr. :-(


OK. Can I avoid this?

The factory only delivers sthared_ptr. The only possibility to use the
poiner would be to use the shared_ptr's get().

Greetings
Ernst

Jul 22 '05 #12
On Wed, 7 Jan 2004 18:24:54 +0100, "Ernst Murnleitner"
<mu******@awite .de> wrote:

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_th is();

during construction of an object.


It isn't - the weak pointer is only initialized in the constructor of
the shared pointer, which is obviously run after the constructor of
your object.
I want to use it in the base class andonly 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.


How are you creating your object? I suggest you use a factory, and
make the necessary extra calls after you've created the shared
pointer. e.g.

shared_ptr<Your Type> make()
{
shared_ptr<Your Type> ptr(new YourType);
//this was called from the YourType constructor before:
someFunc(ptr);
return ptr;
}

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #13
"Ernst Murnleitner" <mu******@awite .de> wrote:
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.


Yes, I only construct them on the heap. And if I only allow a factory to
create my Objects (e.g. as Mr. Carson suggested in another message thread),
I cannot accidentally define automatic variables.


class A {
protected:
A();
};

class B: public A {
public:
B();
}

void foo( A* a );

int main() {
B b;
foo( &b );
}

How are you going to stop A's from being created on the stack again?

Of course, you still have the problem that some places in the code may
be using an A* rather than an APtr. :-(


OK. Can I avoid this?

The factory only delivers sthared_ptr. The only possibility to use the
poiner would be to use the shared_ptr's get().


Fine, but then you must be changing the origional code rather than
simply changing a typedef from A* to shared_ptr<A>.. .
Jul 22 '05 #14
> >I found this just before. Now I wonder if it is possible to use

shared_from_th is();

during construction of an object.
It isn't - the weak pointer is only initialized in the constructor of
the shared pointer, which is obviously run after the constructor of
your object.


I tried: it throws an exception (and is also documented this way).

Therefore I used in the constructor of the baseclass something like this:

std::vector<sha red_ptr<Base> > Base::AllItemsV ector;

Base::Base(...)
{
shared_ptr<Base > p (this);
Base::AllItemsV ector.push_back (p);
....
shared_ptr<Base > pChild
(Factory::NewCh ild("newchildna me",shared_from _this()));
}

This way it seems to work. However, somewhere I read that shared_ptr cannot
be constructed in the constructor (but I cannot exactly remember). But this
seems to work.

Greetings

Ernst



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.


How are you creating your object? I suggest you use a factory, and
make the necessary extra calls after you've created the shared
pointer. e.g.

shared_ptr<Your Type> make()
{
shared_ptr<Your Type> ptr(new YourType);
//this was called from the YourType constructor before:
someFunc(ptr);
return ptr;
}

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Jul 22 '05 #15

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

Similar topics

5
2782
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, status=no');
3
1728
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 .
1
1995
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
9
2180
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 dynamically)? Example: if a member function is invoked on an object of class Apple (appleobj.drip_it() ),...
1
1297
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=&quot;2.0 &quot;/channel/title").InnerText, String) The XML file: <?xml version="1.0"?>
7
2241
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 member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
8
2236
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: class I_want_To_Send : public I_will_write_In_Logger
10
4794
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:
0
8685
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, 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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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 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.