Thread: Initialization. Are they the same?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Question Initialization. Are they the same?

    Code:
    Is the way they initialise variable facing any problem? 
    Is the way they initialise are the same?
    
    class Initial
    {
    public:
    	Initial();
    	~Initial();
    
    private:
    	int val;
    };
    
    Initial::Initial()
    {
    	val = 0;
    }
    
    //AND
    
    class Initial
    {
    public:
    	Initial( int = 0 );
    	~Initial();
    	void setVal();
    
    private:
    	int val;
    };
    
    Initial::Initial( int v )
    {
    	setVal( v );
    }
    
    void setVal( int v )
    {
    	val = v;
    }
    
    //AND
    
    class Initial
    {
    public:
    	Initial( int = 0 );
    	~Initial();
    
    private:
    	int val;
    };
    
    Initial::Initial( int v )
    : val( v ) {}
    
    //AND
    
    class Initial
    {
    public:
    	Initial( int );
    	~Initial();
    
    private:
    	int val;
    };
    
    Initial::Initial( int v )
    {
    	val = v;
    }
    
    int main()
    {
    	Initial i( 5 );
    	....
    	....
    
    	return 0;
    }
    
    Over here, the variable 'val' is straight away initialised 
    by a desirable value from programmer. Shouldn't 
    'val' be initialised to zero first before using it?

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i'm not sure, but i believe all variables in a class are automatically initialized to zero.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Short answer: no. There is no need to initialize a variable to zero if you know what you are about to do with it. In other words, why do this? :

    int i = 0;

    i = 5;

    The exceptions are ones such as:

    int i;

    i += 5; //...undefined behaviour...should've
    initialized to zero first...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    try it out and you'll see whether it is zero or NULL

    I don't know if this is compiler specific (doubt it)...
    think only with code.
    write only with source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 12:42 PM
  2. Assignment vs. Initialization
    By arrgh in forum C Programming
    Replies: 6
    Last Post: 05-06-2008, 03:08 AM
  3. initialize constant and initialization list
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2006, 06:30 AM
  4. class initialization and the = operator
    By krygen in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2005, 12:44 AM
  5. Globals initialization
    By New++ in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2004, 01:11 PM