473,804 Members | 3,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When Initialization Lists shall not be used?

Can someone clarify few items from FAQ
http://www.parashift.com/c++-faq-lit....html#faq-10.6

'[10.6] Should my constructors use "initializa tion lists" or
"assignment "?'

a. This might happen when your class has two constructors that need to
initialize the 'this' object's data members in different orders. <--
Okay

b. Or it might happen when two data members are self-referential. <--
Can someone clarify this?

c. Or when a data-member needs a reference to the this object, and you
want to avoid a compiler warning about using the this keyword prior to
the { that begins the constructor's body (when your particular compiler
happens to issue that particular warning). ?? An example will be
helpful.

d. Or when you need to do an if/throw test on a variable (parameter,
global, etc.) prior to using that variable to initialize one of your
this members. ?? An example will be helpful.

Regards,
Manish

Sep 28 '06 #1
1 1685
pasa_1 wrote:
Can someone clarify few items from FAQ
http://www.parashift.com/c++-faq-lit....html#faq-10.6

'[10.6] Should my constructors use "initializa tion lists" or
"assignment "?'

a. This might happen when your class has two constructors that need to
initialize the 'this' object's data members in different orders. <--
Okay

b. Or it might happen when two data members are self-referential. <--
Can someone clarify this?
Not sure what's meant here, but if 'plast' needs to point to the last
element of a dynamically allocated array, you can either reoder the
members and initialise plast in the initialiser list or use assignment
like here:

struct A {
int *plast;
int n;
int *p;
A(int nn) : n(nn), p(new int[nn]) { plast = p[n-1]; }
};
c. Or when a data-member needs a reference to the this object, and you
want to avoid a compiler warning about using the this keyword prior to
the { that begins the constructor's body (when your particular
compiler happens to issue that particular warning). ?? An example
will be helpful.
struct A {
A* self;
A() : self(this) {} // can get a warning about use of 'this'
A(int) { self = this; } // no warning
};
d. Or when you need to do an if/throw test on a variable (parameter,
global, etc.) prior to using that variable to initialize one of your
this members. ?? An example will be helpful.
'throw' is a statement, and IIRC has to be in the body. OTOH, I am
not sure it cannot be circumvented with a static member function in
which you could both check and throw (and return the value to boot)...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5352
by: Thomas | last post by:
I have a class that uses environment variables as part of its initialization. The following used to work using the Lucent SCL but now fails with STL. string env = getenv("ENV_VALUE"); // string env(getenv("ENV_VALUE")); // tried this just in case it was problem with operator=. if(env.empty()){ // handle this situation
17
2518
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the following cases, shall a default constructor be provided? 1. Inheritance. Class A is derived from class B. Shall a default constructor
5
2576
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
4
1896
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and so on, quite a few members {} and does nothing else.
14
2420
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, { "needmailhelo", PRIV_NEEDMAILHELO }, { "needexpnhelo", PRIV_NEEDEXPNHELO }, { "needvrfyhelo", PRIV_NEEDVRFYHELO },
9
2474
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
17
4806
by: prakashraovaddina | last post by:
Hi ppl, can some one say if the following pointer initialization is legal or no. int *intPtr = 5; While debugging i still see that the pointer is given some address. However, the initialization value is not found there. But, the following string initialization,
17
2695
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because no one had seen this construct. So i looked it up as best i could in the pointers to the Specification that I find here and could not find any specific reference to this construct. What i did find was reference to (paraphrasing) any values
5
399
by: kiryazev | last post by:
Hello. Given the code below does C++ Standard guarantee that the function my_init() will be called before main()? struct A { A() { my_init(); } };
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.