using initializer list

T

Tony Johansson

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
 
A

Alf P. Steinbach

* 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.
 
J

John Harrison

Tony said:
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
 
R

Rolf Magnus

John said:
....

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?
 
A

Alf P. Steinbach

* Rolf Magnus:
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;

...
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top