By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

using initializer list

P: n/a
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
Nov 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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?
Nov 6 '05 #2

P: n/a
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
Nov 7 '05 #3

P: n/a
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?

Nov 7 '05 #4

P: n/a
* 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?
Nov 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.