472,353 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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<typename 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 1206
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<typename 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
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...
4
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....
10
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...
3
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...
1
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...
27
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...
3
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...
2
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...
0
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...
11
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.