Connecting Tech Pros Worldwide Forums | Help | Site Map

using initializer list

Tony Johansson
Guest
 
Posts: n/a
#1: Nov 6 '05
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


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Nov 6 '05

re: using initializer list


* Tony Johansson:[color=blue]
>
> 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);
> }[/color]

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?
John Harrison
Guest
 
Posts: n/a
#3: Nov 7 '05

re: using initializer list


Tony Johansson wrote:[color=blue]
> 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);
> }
>[/color]

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
Rolf Magnus
Guest
 
Posts: n/a
#4: Nov 7 '05

re: using initializer list


John Harrison wrote:
[color=blue][color=green]
>> CEx07aView::CEx07aView()
>> {
>> m_pDlg = new Test(this);
>> }[/color][/color]

....
[color=blue]
> CEx07aView::CEx07aView() : m_pDlg(new Test(this))
> {
> }[/color]

....
[color=blue]
> 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.[/color]

How are they different in that regard?

Alf P. Steinbach
Guest
 
Posts: n/a
#5: Nov 7 '05

re: using initializer list


* Rolf Magnus:[color=blue]
> John Harrison wrote:
>[color=green][color=darkred]
> >> CEx07aView::CEx07aView()
> >> {
> >> m_pDlg = new Test(this);
> >> }[/color][/color]
>
> ...
>[color=green]
> > CEx07aView::CEx07aView() : m_pDlg(new Test(this))
> > {
> > }[/color]
>
> ...
>[color=green]
> > 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.[/color]
>
> How are they different in that regard?[/color]

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?
Closed Thread