473,394 Members | 1,702 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,394 software developers and data experts.

using initializer list

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
4 1453
* 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
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
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: spipyeah | last post by:
My compiler is known not to be very compliant. So I can't base myself on the fact that the following doesn't work to determine whether or not it is legal C++. In an initializer list, can you...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
3
by: Christopher Benson-Manica | last post by:
I recently had an issue that I'll try to condense for your reviewing pleasure: class A { protected: my_type Member; public: A() {/* initialize Member */}
1
by: Derek | last post by:
I get the following warning warning C4355: 'this' : used in base member initializer list when I compile the following program with VC6 and VC7 (the latest GCC and Comeau doesn't think anything...
2
by: aarklon | last post by:
Hi all, Recently i was reading a c book which contained a section called C pitfalls. it had a paragraph on the following lines:- ...
3
by: Ham Pastrami | last post by:
class Point { public: const int x, y; Point(int x, int y); } Point::Point(int x, int y) : x(x), y(y) { }
4
by: Ham Pastrami | last post by:
How do you initialize objects in the initializer list? Also, while this code is probably incorrect, it does compile (and execute) and I wonder what the actual result is. class Control { Point...
3
by: John Ratliff | last post by:
I've been using g++ for a long time (and this is not a g++ question), and I recently started using MSVC Express. Some of my programs that I rebuilt with MSVC give me a warning I don't understand. ...
0
by: Pooria | last post by:
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char char2; }; class Test2{
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.