Initialization. Are they the same?

This is a discussion on Initialization. Are they the same? within the C++ Programming forums, part of the General Programming Boards category; Code: Is the way they initialise variable facing any problem? Is the way they initialise are the same? class Initial ...

  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,493
    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,699
    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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(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, 11:42 AM
  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-26-2005, 11:44 PM
  5. Globals initialization
    By New++ in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2004, 12:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21