473,408 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Dereferencing this in constructors initialiser list

Hi.

Is it allowed to dereference 'this' in a constructors initialiser list
to initialize a reference? (for later use)

Is this code legal or illegal?

class Foo;

class Bar
{
public:
Bar(Foo& foo)
: foo_(foo)
{
}
private:
Foo& foo_;
};

class Foo
{
public:
Foo()
: bar_(*this)
{
}
private:
Bar bar_;
};

int main()
{
}

Bye.
Jul 22 '05 #1
2 3042
On Wed, 14 Apr 2004 12:02:13 +0200, Martin Zimmermann
<Ma***************@hotmail.com> wrote:
Hi.

Is it allowed to dereference 'this' in a constructors initialiser list
to initialize a reference? (for later use)


Yes. You can even do that before the constructor has even started. You
can only use that lvalue to access things that have already been
initialized (such as earlier base classes, earlier member variables
(in class declaration order)).

This is perfectly valid:

Foo* mem = static_cast<Foo*>(malloc(sizeof *mem));
assert(mem);
Foo& f = *mem;
//don't use f until it has been constructed!
new(mem) Foo();
//now you can use f safely.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
Martin Zimmermann wrote in news:c5*************@news.t-online.com in
comp.lang.c++:
Hi.

Is it allowed to dereference 'this' in a constructors initialiser list
to initialize a reference? (for later use)

Is this code legal or illegal?

class Foo;

class Bar
{
public:
Bar(Foo& foo)
: foo_(foo)
{
}
private:
Foo& foo_;
};

class Foo
{
public:
Foo()
: bar_(*this)
{
}
private:
Bar bar_;
};

int main()
{
}

Bye.


Simple logic should suffice. In order to initialize the bar_
subobject 'this' must be dereferenced (this->bar_) anyway,
so yes you can.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

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

Similar topics

2
by: John Harrison | last post by:
What are the rule concerning calling member functions from an initialiser list? Suppose I have class C : public B { public: C() : x(), y(f()), z() {} private: Y f(); X x;
2
by: Neil Zanella | last post by:
Hello, AFAIK the only way to initialize a reference variable defined inside a class is to initialize it in an initializer list. However, when there are multiple constructors, this means that the...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
20
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something...
1
by: techiepundit | last post by:
I have a class: class ServerThreadManager(threading.Thread): def __init__(self): threading.Thread.__init__(self) # and a bunch of constructor statements def run(self): self.ReqHandlingLoop()
4
by: gw7rib | last post by:
Suppose I have the following code fragment: class base { private: int m; public: base(int x) { m = x; } };
7
by: Peter | last post by:
I know the order of construction of member and base class objects. My question is the following: Is the order of evaluation of argument lists for these constructors also defined? E.g. can I...
49
by: JohnQ | last post by:
There must be something at the implementation level that makes the standard disallow constructors in PODs (?). What is that? Don't most implementations just break out the constructor member...
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...
0
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...
0
isladogs
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...
0
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...

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.