Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructor help

Reply
Thread Tools

Constructor help

 
 
Ook
Guest
Posts: n/a
 
      10-01-2005
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) { }


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-01-2005
* "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?
 
Reply With Quote
 
 
 
 
Markus Becker
Guest
Posts: n/a
 
      10-02-2005
Alf P. Steinbach <(E-Mail Removed)> 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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-02-2005
* Markus Becker:
> Alf P. Steinbach <(E-Mail Removed)> 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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-02-2005
* 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?
 
Reply With Quote
 
Markus Becker
Guest
Posts: n/a
 
      10-04-2005
Alf P. Steinbach <(E-Mail Removed)> 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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-04-2005
* Markus Becker:
> Alf P. Steinbach <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help on constructor/constructor initialization list with inheritanceusing templates balaji C++ 3 08-10-2011 12:06 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments