473,320 Members | 2,162 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,320 software developers and data experts.

Using 'this' in constructor

bb
Hi,

Is it legal and ok to use 'this' in a constructor as follows:

Class A {

public:
A() : pimpl_(0) {
pimpl_ = new AImpl(this);
}

private:
struct AImpl;
AImpl* pimpl_;
};

Thanks.
Oct 17 '08 #1
2 3749
bb wrote:
Is it legal and ok to use 'this' in a constructor as follows:

Class A {
class A
>
public:
A() : pimpl_(0) {
pimpl_ = new AImpl(this);
}

private:
struct AImpl;
AImpl* pimpl_;
};
No. AImpl is unknown at the point where you're trying to instantiate
it. If your constructor is defined after 'AImpl' is defined as well,
then yes, it's legal, but keep in mind that the object is not considered
fully constructed until its constructor completes, so calling member
functions can be dangerous, *generally speaking*.

If *all* your AImpl constructor needs to do is to *store* the pointer to
'A' somewhere so it can access it later, then yes, it's going to be OK.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 17 '08 #2
On Oct 17, 7:04*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
bb wrote:
Is it legal and ok to use 'this' in a constructor as follows:
Class A {
class A
* public:
* * A() : pimpl_(0) {
* * * *pimpl_ = new AImpl(this);
* * }
* private:
* * * *struct AImpl;
* * * *AImpl* *pimpl_;
};
No. *AImpl is unknown at the point where you're trying to
instantiate it.
Not unknown, just incomplete. (Member function code in a class
is compiled as if it were defined immediately after the class.)
Of course, you can't new an incomplete type, so the code is
still illegal.
If your constructor is defined after 'AImpl' is defined as
well, then yes, it's legal, but keep in mind that the object
is not considered fully constructed until its constructor
completes, so calling member functions can be dangerous,
*generally speaking*.
If *all* your AImpl constructor needs to do is to *store* the
pointer to 'A' somewhere so it can access it later, then yes,
it's going to be OK.
Whether A is complete isn't the problem. Something like:

class A {
public:
A() : p( new AImpl( this ) ) {}

private:
struct AImpl
{
AImpl( A* p ) ;
// ...
} ;
AImpl* p ;
} ;

is legal. (Of course, the names in the example suggest the
compliation firewall idiom, and this would defeat the purpose of
it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 18 '08 #3

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

Similar topics

9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
6
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
7
by: Abhi | last post by:
Hi all, I am using .net for C++ and I would like to write some variable values to some files. I will be using that file in many member functions of the class. So I declared the file variable...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
11
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: mersinli | last post by:
Hello; I will use standard library vectors and implement simple map coloring algorithm. The goal of a map coloring problem is to color a map so that regions sharing a common border have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.