Hi,
I have always been taught to use an inialization list for initialising data
members of a class. I realize that initialsizing primitives and pointers use
an inialization list is exactly the same as an assignment, but for class
types it has a different effect - it calls the copy constructor.
My question is when to not use an initalisation list for initialising data
members of a class?
Regards
Adi
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system ( http://www.grisoft.com).
Version: 6.0.516 / Virus Database: 313 - Release Date: 1/09/2003 106 5589
"A" <A@iprimus.com. au> wrote in message
news:3f******** @news.iprimus.c om.au...
| I have always been taught to use an inialization list for initialising
data
| members of a class. I realize that initialsizing primitives and pointers
use
| an inialization list is exactly the same as an assignment, but for class
| types it has a different effect - it calls the copy constructor.
|
| My question is when to not use an initalisation list for initialising data
| members of a class?
Only when you are unable to, e.g. when a complex computation involving
several intermediate variables is needed and a dedicated function doesn't
make sense.
IMO the initialization list should always be preferred.
Regards,
-- http://ivan.vecerina.com
"A" <A@iprimus.com. au> wrote in message
news:3f******** @news.iprimus.c om.au... Hi,
I have always been taught to use an inialization list for initialising
data members of a class. I realize that initialsizing primitives and pointers
use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor.
My question is when to not use an initalisation list for initialising data members of a class?
The basic rule of thumb is that anything that can go in the initialize list
should go there. Obviously, you can't put something like this in there:
if (a)
i = 2;
else
i = 4;
> > Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the same as an assignment, but for class types it has a different effect - it calls the copy constructor.
My question is when to not use an initalisation list for initialising
data members of a class?
The basic rule of thumb is that anything that can go in the initialize
list should go there. Obviously, you can't put something like this in there: if (a) i = 2; else i = 4;
Many, if not most, of those cases can be covered by the ?: operator:
MyClass(int a) : i(a ? 2 : 4) {}
/ Erik
Erik wrote:
.... should go there. Obviously, you can't put something like this in there: if (a) i = 2; else i = 4;
Many, if not most, of those cases can be covered by the ?: operator: MyClass(int a) : i(a ? 2 : 4) {}
Yep - and sometimes it makes sense to create a function that does more
complex things so that everything is in the initializer list.
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bj******** @dispatch.conce ntric.net... Erik wrote: ...should go there. Obviously, you can't put something like this in there: if (a) i = 2; else i = 4;
Many, if not most, of those cases can be covered by the ?: operator: MyClass(int a) : i(a ? 2 : 4) {}
Yep - and sometimes it makes sense to create a function that does more complex things so that everything is in the initializer list.
And how exactly do you propose calling such a function from the initializer
list?
"Erik" <no@spam.com> wrote in message news:bj******** **@news.lth.se. .. The basic rule of thumb is that anything that can go in the initialize list should go there. Obviously, you can't put something like this in there: if (a) i = 2; else i = 4;
Many, if not most, of those cases can be covered by the ?: operator: MyClass(int a) : i(a ? 2 : 4) {}
Yeah, maybe simple "if" cases like that, but you can't put logic or function
calls (other than base class constructors) in the initializer list. You
added an expression, not a statement.
"jeffc" <no****@nowhere .com> wrote in message news:3f******** @news1.prserv.n et... Yep - and sometimes it makes sense to create a function that does more complex things so that everything is in the initializer list.
And how exactly do you propose calling such a function from the initializer list?
Same way you call any other function. It behooves you not to use the
yet uninitialized parts of the object being constructed though.
"jeffc" <no****@nowhere .com> wrote in message news:3f******** @news1.prserv.n et... Many, if not most, of those cases can be covered by the ?: operator: MyClass(int a) : i(a ? 2 : 4) {}
Yeah, maybe simple "if" cases like that, but you can't put logic or function calls (other than base class constructors) in the initializer list. You added an expression, not a statement.
Who says you can't have function calls?
Ying Yang wrote: Never. Initializatio n lists are the only way to *initialize* members of a class or to specify which constructor from a base class should be used.
No, it's not the only way - as i mentioned you can use an assignment operator for initialising primitive data members, which is the same as using an initialization list.
No, you cannot initialize anything with an assignment operator ever.
Assignment operators assign, they don't initialize. If you don't the
difference, you should look it up.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: matro |
last post by:
hi there,
I have the following struct in my class:
class CTbStDialog : public CDialog
{
public:
int fooVar, fooVar2;
... //other foo variables...
|
by: jose luis fernandez diaz |
last post by:
Hi,
I am reading Stroustrup's book 'C++ Programming Language'. In the
10.4.9 section (Nonlocal Store) he says:
"A variable defined outside any function (that is global, namespace,
and class static variables) is initializated (constructed) before main
is invoked . . ."
.. . .
|
by: Neil Zanella |
last post by:
Hello,
When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar;" where nothing has ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.
I am interested in knowing what the standard says should happen in the
|
by: deancoo |
last post by:
I'm hoping this isn't too off topic, but I'm not sure if I've included some
of my member functions in the right class. I needed to develop numerous
functions to help define object A. These functions currently exist in the
class definition for object A. The thing is, there is another object (B)
who's job it is to call these functions when the app starts up, defining all
possibilities for object A's, then allow future object A's to look up...
|
by: Frederick Gotham |
last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.
Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).
Here are some examples of the former:
struct MyStruct {
| |
by: silversurfer2025 |
last post by:
Hello everyone,
once again, I have a very basic problem in C++, which I was not able to
solve (maybe because of my Java-Experience or just because it is always
the small syntax-things which break my neck in C++)...
I would like to have a static variable in class A and change its value
from class B (they are not related to each other)
For example:
|
by: Jeroen |
last post by:
Hi all,
I've got a question about writing a library. Let me characterize that
library by the following:
* there is a class A which is available to the user
* there is a class B that is used in severel 'underwater operations'
* there is a list which stores objects of class B
There are several issues I'm not sure about:
|
by: .rhavin grobert |
last post by:
i have (do try to have?) the following...
& = breakpoints in debugger
// ----------------------------------------------------------------
// cx.h
class CX {
public:
CX(CX* pcx = NULL);
virtual ~CX();
|
by: Anarki |
last post by:
#include <iostream>
using namespace std;
class ConstantMember
{
const int m_const;
public:
ConstantMember(int cons = 10):m_const(cons){}
int getConst() const{ return m_const;}
};
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |