Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > using initializer list

Reply
Thread Tools

using initializer list

 
 
Tony Johansson
Guest
Posts: n/a
 
      11-06-2005
Hello!

Is it possible to put the body below which is the instansiating of Test in
the initialize list in some way.

CEx07aView::CEx07aView()
{
m_pDlg = new Test(this);
}

//Tony


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-06-2005
* Tony Johansson:
>
> Is it possible to put the body below which is the instansiating of Test in
> the initialize list in some way.
>
> CEx07aView::CEx07aView()
> {
> m_pDlg = new Test(this);
> }


You have asked a lot of such homework questions earlier.

Please consult your textbook.

Cheers.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-07-2005
Tony Johansson wrote:
> Hello!
>
> Is it possible to put the body below which is the instansiating of Test in
> the initialize list in some way.
>
> CEx07aView::CEx07aView()
> {
> m_pDlg = new Test(this);
> }
>


Simple

CEx07aView::CEx07aView() : m_pDlg(new Test(this))
{
}

But some compilers might give you warnings with this code. This is
because you are using 'this' before the object it is refering to has
been constructed, which is potentially a dangerous situation. I think I
would usually prefer this code

CEx07aView::CEx07aView() : m_pDlg(0)
{
m_pDlg = new Test(this);
}

The main difference between all three sets of code is what would happen
if new Test(this) threw an exception.

john
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      11-07-2005
John Harrison wrote:

>> CEx07aView::CEx07aView()
>> {
>> m_pDlg = new Test(this);
>> }


....

> CEx07aView::CEx07aView() : m_pDlg(new Test(this))
> {
> }


....

> CEx07aView::CEx07aView() : m_pDlg(0)
> {
> m_pDlg = new Test(this);
> }
>
> The main difference between all three sets of code is what would happen
> if new Test(this) threw an exception.


How are they different in that regard?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-07-2005
* Rolf Magnus:
> John Harrison wrote:
>
> >> CEx07aView::CEx07aView()
> >> {
> >> m_pDlg = new Test(this);
> >> }

>
> ...
>
> > CEx07aView::CEx07aView() : m_pDlg(new Test(this))
> > {
> > }

>
> ...
>
> > CEx07aView::CEx07aView() : m_pDlg(0)
> > {
> > m_pDlg = new Test(this);
> > }
> >
> > The main difference between all three sets of code is what would happen
> > if new Test(this) threw an exception.

>
> How are they different in that regard?


Consider:

struct CEx07aView;

struct Test
{
Test( CEx07aView* ){ throw 666; }
};

struct FooBar
{
FooBar() { std::cout << "1\n"; }
~FooBar() { std::cout << "2\n"; }
};

struct CEx07aView
{
Test* m_pDlg;
FooBar foo;

...
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using "this" in a base initializer list? Angel Tsankov C++ 10 05-08-2007 09:50 PM
using nonconstant expressions in an initializer list aarklon@gmail.com C Programming 2 08-06-2006 09:34 PM
No constructor initializer list in Java ? Razvan Java 7 07-04-2004 02:28 AM
initialization in initializer list using copy constuctor?!? Alexander Stippler C++ 3 04-09-2004 01:53 PM
derived members & initializer list Aman C++ 4 09-15-2003 06:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57