473,599 Members | 3,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't remember

LuB
Bruce Sutter talks about this a bit - but I can't remember where. When
is it safe to use an object or reference to an object. When does it
have an accessible address?

Specifically, I think it has to pass out of the out initialization
stage ... but I can't remember exactly. Can anyone clarify?

Safety aside, is this undefined - passing B to A's ctor before B has
completed initialization?

Thanks in advance,

-Luther

/////////////////////////////////////////////////////

#include <iostream>

template<typena me BType>
class A
{

public:
A(const BType& b) : b_(b) { std::cout << "A ctor" << std::endl; }

private:
const BType& b_;

};

class B
{

public:
B() : a_(*this) { std::cout << "B ctor" << std::endl; }

private:
const A<B>& a_;

};

int
main(int argc, char** argv)
{

B b;
std::cout << "hello world" << std::endl;
}

Nov 22 '05 #1
2 1251
LuB wrote:
Bruce Sutter talks about this a bit - but I can't remember where. When
is it safe to use an object or reference to an object. When does it
have an accessible address?

Specifically, I think it has to pass out of the out initialization
stage ... but I can't remember exactly. Can anyone clarify?
I think you've also mistaken Bruce Sutter for Herb Sutter, but you can
search his website for the topic. Here's one that might be what you're
looking for:

http://www.gotw.ca/publications/mill13.htm

The [this] pointer is valid and points to the storage allocated for the
object when the constructor starts (compare what happens with when you
call operator new:
http://www.parashift.com/c++-faq-lit...tml#faq-11.14), and using
it is legitimate.

However, an object's lifetime has not begun until the constructor
successfully completes, and indeed, were that object's constructor to
throw an exception, the memory allocated for it would be deallocated,
and any reference to it would be dangling, which, if used, leads to
undefined behavior.
Safety aside, is this undefined - passing B to A's ctor before B has
completed initialization?

Thanks in advance,

-Luther

/////////////////////////////////////////////////////

#include <iostream>

template<typena me BType>
class A
{

public:
A(const BType& b) : b_(b) { std::cout << "A ctor" << std::endl; }

private:
const BType& b_;

};

class B
{

public:
B() : a_(*this) { std::cout << "B ctor" << std::endl; }

private:
const A<B>& a_;
I'll assume you didn't want the reference here. Otherwise, it wouldn't
compile.

};

int
main(int argc, char** argv)
{

B b;
std::cout << "hello world" << std::endl;
}


That code could lead to undefined behavior (though it doesn't as
written). A is itself a member of B (if you delete the reference, as I
suggested) and would be destructed after B::~B() executes, but if A
tried to use its reference to B either in its constructor or destructor
to access other data members of B, you could get undefined behavior if
those part(s) of B had already been destroyed. That of course depends
on the construction/destruction order of B's members.

Cheers! --M

Nov 22 '05 #2
LuB
Eye yai yai ... I guess Bruce Sutter was a pitcher for the Cardinals
here in St. Louis ... ;-)

And many thanks for your answer. Helped me better understand the
potential danger of the idiom.

Thanks again,

-Luther

Nov 22 '05 #3

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

Similar topics

142
4233
by: Herr Lucifer | last post by:
As the founder of .NET framework, Microsoft claims that it invention will be the next best platform for programming in a near future. Now it is 2005, ..NET is 5 years old, and can talk and walk for himself with some help of his mum. However, we see the same native office applications are coming out again, and many other tools in SP2 of XP which could be in managed code....but are not. So, as the inventor of .NET , why doesn't Microsoft...
4
1765
by: Simon Cooke | last post by:
Hi there; A while back when I was in Albany, NY, I came across what looked like a really great C++ book for the experienced programmer. Unfortunately, I couldn't buy it and take it with me because I was already carrying too much stuff to fly home with it. Unfortunately, I've been looking online and in every bookstore I come across ever since, and I still can't find a copy of it again. Do any of you know which book this was? Here's...
10
4567
by: VictorG | last post by:
Hello, I am new to JS and am trying to add some HTML into a JS function. So that when called the script as well as the HTML will be invoked. Is there some type of embed mechanism, sort of the reverse of embedding JS in an html page with the script tag. (which is what I am doing in this case) Is this even possible?
3
2402
by: laredotornado | last post by:
Hello, I have a simple login script. I have the pages login.php login_response.php login_success.php login_failure.php What I want to do is have a checkbox on the login.php page where if the user checks it and logs in successfully, upon closing the browser and returning to the login.php page, they are automatically redirected to
1
1238
by: Raj Chudasama | last post by:
I am streaming an audio presentation with which i will also have some text. I would like to remember the text that was displayed with the audio. I want this to be replayable later thus requires that text be displayed at the appropriate time as the original presentation. (make sense?) what are some solutions that come to your mind? i haev thought of following so far: "inject" startucture of a sort in to the audio stream.
27
1841
by: Just Me | last post by:
I made a Usercontrol that must have AutoScroll set to true when it is used. So I set it to True in the Load event. However the property still shows in the properties window when the control is placed on a form. That's confusing. How can I make that property not show in the properties window?
3
1963
by: vtxr1300 | last post by:
I'm trying to create a login box control to use that simply exposes the username, password and remember me properties when the login button is clicked. I have the button click wired up and working and at one time, the username was holding state. I don't know what I changed, but now nothing holds state. I've read a number of messages about making sure that the controls are added to the control collection during CreateChildControls, which...
2
2441
by: André | last post by:
Hi, When clicking on "remember me" when logging, the user asks for not to have to log in next time he visits the site. Now, on one side, i read it is recommended to logout properly (clicking on 'logout') when quiting the site, but then, the user looses the benefit of the option "remember me next time" and must login next visit. On the other side, if the user quit the website without logout, at the next
0
1308
by: Frank Miverk | last post by:
Hi, I am not understanding how the Remember Me checkbox is supposed to work here. I have a LoginCtrl (asp.net 2.0, framework 2.0) and all I want to do is remember the user the next time if they previously checked the Remember Me checkbox. By default, on subsequent visits the Remember Me checkbox does not stay checked. I understand that checking this checkbox, allows me to create a cookie using FormsAuthentication.SetAuthCookie(...
11
2423
by: Jason | last post by:
Say I build a simple .exe file. n exe that will look at a file, read that file and put the contents into an email. If I take the .exe that I built from the Bin\Release folder and copy it to a machine will it work or do I need to make sure that I have dot net installed and all of this ? What is the best way to build an .exe so that I can run it on most machines without any updates?
0
7904
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8398
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
8400
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
8051
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
8267
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
3898
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...
1
2414
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
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.