Constructor help

O

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) { }
 
A

Alf P. Steinbach

* "Ook said:
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.
 
M

Markus Becker

Alf P. Steinbach said:
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
 
A

Alf P. Steinbach

* Markus Becker:
Alf P. Steinbach said:
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.

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 it make the c'tor 'inline'?

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

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



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.


Or as a trap in a test.

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

Alf P. Steinbach

* 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.
 
M

Markus Becker

Alf P. Steinbach said:
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?

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
 
A

Alf P. Steinbach

* Markus Becker:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top