Re: Build-in types initialization

From: Sharad Kala (no.spam_sharadk_ind_at_yahoo.com)
Date: 02/25/04


Date: Wed, 25 Feb 2004 20:30:13 +0530


"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message
news:c1ibcm$e3b$1@korweta.task.gda.pl...
> Hi all,
>
> Thanks for your answer, but if it is true, how do I define my custom class
> so that it follows the same schema? It seems impossible, because there's no
> way to distinguish between these 2 types of initialization when writing
> class definition. Both are handled by default constructor.
>
> To be specific, I'd like to define class Vector3, which is a 3 dimensional
> vector. If I initialize coordinates to zeroes in default constructor, I get
> behavior 'i2'. If I do not initialize, I get behavior 'i1'. But I'd like to
> get both if them, so that Vector3 does not differ in this important point
> from built-in types. Additionally, Vector3 is to be used in
> performance-critical code, so I'd like to avoid unnecesary initialization if
> possible - so 'i1' behavior is probably a must to have.

You got it wrong.
The default constructor for int, float, bool initializes them to 0 /false.
But that does not mean that ints, floats etc in UDTs will also be default
initialized in the default constructor.

In this code -

#include <iostream>
using namespace std;
struct A{
int i;
A(){}
A(int I):i(I){}
};

int main(){

A a1 = A(); // Default const
cout << a1.i; // Print garbage..don't expect 0
A a2(10);
cout << a2.i; // Print 10
}



Relevant Pages

  • Re: what does this mean ?
    ... int main ... To zero-initialize an object of type T means: ... - if T is a non-POD class type, the default constructor for T is ... called (and the initialization is ill-formed if T has no accessible default ...
    (microsoft.public.vc.language)
  • Re: Inheritence problem
    ... > try to debug I notice that once it leaves the Animal Constructor, ... > Protected variables lose thier initialization value. ... Cow class every thing you typed here should work just fine. ... int GetY(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: List container passed as a reference
    ... > which uses a default constructor, ... alternate cstor or copy cstor is irrelevent. ... It is initialization, not assignment. ... >> int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: List container passed as a reference
    ... >>Point has are a default and a copy constructor, ... > is a cstor with an assignment operator that initializes publicly accessible ... struct Point ... this is initialization, but it does not use constructors ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)