TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: TIM HUTZLER
from: RENE HERMAN
date: 1998-04-28 02:59:00
subject: Default constructor

Hello Tim ...
Tuesday April 21 1998, you wrote Rene Herman
 RH>> According to ANSI C++, will a constructor with nothing but default
 RH>> arguments do as a default constructor?
 TH> It *is* the default constructor.
Thanks, that was indeed what I was asking, sorry if I formulated badly.
 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.
But now you're confusing me again. Not only was there no error, but declaring 
a TTime object as "TTime Time;" really resulted in the compiler calling my 
constructor with all arguments set to -1, not in calling an automatically 
generated no argument constructor.
Furthermore, why must every class have a default constructor? When I never 
declare an object of the class without explicitly stating which constructor 
to use (ie only declare a TTime Time(1, 1, 1980, 0, 0);) there is no use for 
a default constructor, is there?
Also, with simple classes that don't declare any virtual functions, there's 
really nothing to construct. When I have a
  class TClass {
  private:
    int i;
  public:
    void Set(int);
  };
declaring a TClass object doesn't generate any code. For the following main 
function
  int main()
  {
    TClass Class;
    Class.Set(0);
    return 0;
  }
the compiler generated (part of -S output)
  _main proc near
     ;
     ; int main()
     ;
   enter 2,0
     ;
     ; {
     ;   TClass Class;
     ;
     ;   Class.Set(0);
     ;
   push 0
   lea ax,word ptr [bp-2]
   push ax
   call near ptr @TClass@Set$qi
   add sp,4
     ;
     ;   return 0;
     ;
   xor ax,ax
     ;
     ; }
     ;
   leave
   ret
  _main endp
[ sidenote: making member function Set virtual, the compiler *does* indeed 
insert an (inline) constructor, initializing the objects VMT pointer:
     ;
     ;    int main()
     ;
   enter   4,0
     ;
     ;    {
     ;      TClass Class;
     ;
   mov word ptr [bp-4],offset @@TClass@
     ;
     ;   Class.Set(0);
     ;
]
Or did I get you wrong?
Rene
---
---------------
* Origin: postmaster@rene.demon.nl (2:282/1.11)

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™.