TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: RENE HERMAN
from: DARIN MCBRIDE
date: 1998-04-28 19:24:00
subject: Default constructor

 TH> Every class MUST have a default constructor. But, if a default
 TH> constructor (no args) is not specified in your code, the compiler
 TH> will create one for you. That's why there was no error.
 RH> But now you're confusing me again. Not only was there 
 RH> no error, but declaring a TTime object as "TTime Time;" 
 RH> really resulted in the compiler calling my constructor 
 RH> with all arguments set to -1, not in calling an 
 RH> automatically generated no argument constructor.
Rene,
You're obviously right.  It will call your constructor.  However, the 
confusing thing here is that your TTime constructor that has all default 
parameters will become the default constructor as its signature can match "no 
parameters".
 RH> Furthermore, why must every class have a default 
 RH> constructor? When I never declare an object of the 
 RH> class without explicitly stating which constructor to 
 RH> use (ie only declare a TTime Time(1, 1, 1980, 0, 0);) 
 RH> there is no use for a default constructor, is there?
Right.  The only time (no pun intended) you need a default constructor is 
when you want to use an array of the objects.  For example:
TTime array[10]; // for whatever reason
You have to use the default constructor for this.  Another example is the 
Standard Template Library - but don't worry about this quite yet.  :-)
 RH> Also, with simple classes that don't declare any 
 RH> virtual functions, there's really nothing to construct. 
Objects don't need to have virtual functions to need to construct.  For 
example, think about a fstream - a file stream.  You can have the constructor 
open the file for you - so it needs to construct.
However, if you have a virtual function, the constructor will be added for 
this in all cases.
 RH>      ;
 RH>      ; {
 RH>      ;   TClass Class;
[No assembly output]
Because you have "optimized" this away.  Try having TClass have a member 
variable that is another class!  For example, having a fstream as a member!
class TClass
{
  fstream f;
// ...
};
The construction will no longer be trivial, and thus you will have a function 
call here.
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.