473,413 Members | 2,058 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,413 software developers and data experts.

Constructor help

Ook
Can some kind soul explain this line? I'm not quite sure what the different
parts do and exactly how it works.
public:
// Constructors
Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }
Oct 1 '05 #1
6 1215
* "Ook" <Don't send me any freakin' spam>:
Can some kind soul explain this line? I'm not quite sure what the different
parts do and exactly how it works.
public:
// Constructors
Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }


Zoot

That's the name of the class.
(int size = 0)

One argument called 'size' that defaults to 0.
: _size(size),

The member '_size' is initialized with the value of 'size'.
_data(_size ? new int[_size] : 0)

The member '_data' is initialized with the value of
_size ? new int[_size] : 0

if '_size' is non-zero then 'new int[_size]' else 0.
{}

Does nothing in the constructor body.

General comments: this constructor only works if '_size' has been declared
before '_data'. Otherwise '_data' will be initialized first, using the
indeterminate value of '_size'. That is very ungood, and it's very simple to
avoid: use 'size' instead of '_size' in the '_data' initialization expression.

Since the problem is so easy to avoid and so totally unnecessary, this
constructor was either coded by a novice or as an illustration of this
problem.

--
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?
Oct 1 '05 #2
Alf P. Steinbach <al***@start.no> schrieb:
Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }
Zoot

That's the name of the class.


At _this_ place, actually, it is the name of the constructor, which
of course is identical to the class name.
(int size = 0)

One argument called 'size' that defaults to 0.
And because it has a default, it can be ommited in the call of the
c'tor, and because it can be omitted, the c'tor can serve as a de-
fault c'tor.
{}

Does nothing in the constructor body.
Does it make the c'tor 'inline'? btw.: there's a ';' missing.
General comments: this constructor only works if '_size' has been declared
before '_data'. Otherwise '_data' will be initialized first, using the
indeterminate value of '_size'. That is very ungood, and it's very simple to
avoid: use 'size' instead of '_size' in the '_data' initialization expression.
This is a _very_ good tip, I had overlooked that. I guess, without knowing
the 'standard' by word, that the order of the initialisations in the head
of the c'tor is not strictly defined?
Since the problem is so easy to avoid and so totally unnecessary, this
constructor was either coded by a novice or as an illustration of this
problem.


Or as a trap in a test.

Markus
Oct 2 '05 #3
* Markus Becker:
Alf P. Steinbach <al***@start.no> schrieb:
Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }
Zoot

That's the name of the class.


At _this_ place, actually, it is the name of the constructor, which
of course is identical to the class name.


The Holy Standard informs us that a constructor has no name. I agree that
that is a word game. But that's how it is, formally.

(int size = 0)

One argument called 'size' that defaults to 0.


And because it has a default, it can be ommited in the call of the
c'tor, and because it can be omitted, the c'tor can serve as a de-
fault c'tor.


Not only "can serve": it is by definition the (one and only) default
constructor for this class.

{}

Does nothing in the constructor body.


Does it make the c'tor 'inline'?


Nope. But the unqualified class name means it is necessarily inline.

btw.: there's a ';' missing.


Nope.

General comments: this constructor only works if '_size' has been declared
before '_data'. Otherwise '_data' will be initialized first, using the
indeterminate value of '_size'. That is very ungood, and it's very simple to
avoid: use 'size' instead of '_size' in the '_data' initialization expression.


This is a _very_ good tip, I had overlooked that. I guess, without knowing
the 'standard' by word, that the order of the initialisations in the head
of the c'tor is not strictly defined?


It is very strictly & rigorously defined. For data members, it's the
declaration order. The textual order in the contstructor initialization list,
if any, does not matter.
Since the problem is so easy to avoid and so totally unnecessary, this
constructor was either coded by a novice or as an illustration of this
problem.


Or as a trap in a test.


Ah, I didn't think of that... ;-)

--
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?
Oct 2 '05 #4
* Alf P. Steinbach:
> Zoot(int size = 0) : _size(size), _data(_size ? new int[_size] : 0) { }


Not only "can serve": it is by definition the (one and only) default
constructor for this class.


Sorry. It is by definition _a_ default constructor. And only by the
impracticality of having multiple default constructors (there's no way to
disambiguate for use as default constructor), the one and only one.

--
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?
Oct 2 '05 #5
Alf P. Steinbach <al***@start.no> schrieb:
Does it make the c'tor 'inline'?
Nope. But the unqualified class name means it is necessarily inline.


What's meant by 'unqualified' at this point? That he just didn't
mention (or declare) it? Can I define a whole class by just defining
a constructor?
btw.: there's a ';' missing.


Nope.


Oops, quite right. I have to admit that - up to now - in _all_ my class
definitions where I defined the member function in the class definition
(like above), I have written something like this:

class klasse
{
public:
klasse(int n=0):_n(n) {}; // <- _this_ ';' I mean ...
};
It is very strictly & rigorously defined. For data members, it's the
declaration order. The textual order in the contstructor initialization list,
if any, does not matter.


Ah, thanks. But, as you mention 'data members' explicitly, what's the ex-
ception to this rule, or what are 'non-data members' (if there're any)?

Markus
Oct 4 '05 #6
* Markus Becker:
Alf P. Steinbach <al***@start.no> schrieb:
Does it make the c'tor 'inline'?
Nope. But the unqualified class name means it is necessarily inline.


What's meant by 'unqualified' at this point?


"Zoot" as opposed to "Zoot::Zoot".

Can I define a whole class by just defining
a constructor?
No.

[about initialization order]
It is very strictly & rigorously defined. For data members, it's the
declaration order. The textual order in the contstructor initialization list,
if any, does not matter.


Ah, thanks. But, as you mention 'data members' explicitly, what's the ex-
ception to this rule, or what are 'non-data members' (if there're any)?


Base class sub-objects.

--
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?
Oct 4 '05 #7

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

Similar topics

11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
3
by: anddos | last post by:
ive got this sample from a book i am learning from c++ , and need abit of help on constructor #ifndef CAR_HPP #define CAR_HPP class Car { public: Car(); Car(int initRadioFreq, int...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
1
by: Carl Fenley | last post by:
I've been programming exclusively in C# for the last few years but am now working on a project where I am required to write all code in VB.NET. I'm trying to create a class with multiple...
7
by: gopal | last post by:
Can one constructor of a class call another constructor of the same class to initialize the this object? I read in the FAQ , to the above question the following ans was given Nope. Let's...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
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?
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
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,...
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
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...
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
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,...
0
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...

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.