Initialized value (INT, double, etc)

Source: Internet
Author: User
Tags uppercase letter

First, consider a myclass class with several constructors. Suppose we decided to add a new data member in the private part of this class, called int_data _:

class MyClass{public:MyClass(): int_data_(0){}explicit MyClass(const Apple& apple): int_data_(0){}MyClass(const string& some_text,double weight):int_data_(0),some_text_(some_text){}private:int int_data_;std::string some_text_;};

When adding this new data member, you need to do a lot of work. When adding a data member of the built-in type, do not forget to initialize it in each Constructor (in the form of int_data _ (0 ). In fact, this is an error-prone method. If you forget to initialize the data member, it may be filled with junk information, depending on the previous history of the computer and application, which may lead to strange and difficult copying. Therefore, what should we do to prevent such errors?

First, we will discuss this issue with built-in types. Observe STD: some_text _, a data member of the string type _. When we add the data member some_text _ to the myclass class, we do not need to initialize it in every constructor of the myclass class, because STD :: the default constructor of string will be automatically called by the compiler to initialize some_text _ to a repeatable State (in this example, it is an empty string ). But the built-in types do not have constructor. What should we do? In fact, it is very simple. For data members of the class, do not use the built-in type, but use the class, as shown below:

  • Do not use Int. Use int instead.
  • Do not use unsigned. Use unsigned instead.
  • Do not use double. Use double instead.
Similarly, we can look at the following code, that is, the template class tnumber:

Template <typename T> class tnumber {public: tnumber (const T & x = 0): Data _ (x) {} operator T () const {return data _;} tnumber & operator = (const T & X) {data _ = x; return * This;} // suffix operator x ++ tnumber operator ++ (INT) {tnumber <t> copy (* This); ++ data _; return copy;} // prefix operator + + xtnumber & operator ++ () {++ data _; return * This;} tnumber & operator + = (t x) {data _ + = x; return * This;} tnumber & operator 0 = (t x) {data _ 0 = x; Return * This;} tnumber & operator * = (t x) {data _ * = x; return * This;} tnumber & operator/= (t x) {scpp_test_assert (X! = 0, "attept to divide by 0"); Data _/= x; return * This;} t operator/(t x) {scpp_test_assert (X! = 0, "attept to divide by 0"); return data _/X;} PRIVATE: t data _;};

First, the constructor that accepts the T type (T is any built-in type, such as int, double, float, etc.) does not declare the explicit it. This is intended. The next function declared by this class is operator T (), which allows implicit conversion of the instance of this class back to the corresponding built-in type. This class is intentionally designed to easily switch back and forth between it and the built-in type. It also defines several common operators that are expected to be used when using the built-in numeric type.

The following is a definition of the actual type we can use:

<span style="font-size:18px;">typedef TNumber<int> Int;typedef TNumber<unsigned> Unsigned;typedef TNumber<int64> Int64;typedef TNumber<unsigned64> Unsigned64;typedef TNumber<float> Float;typedef TNumber<double> Double;typedef TNumber<char> Char;</span>


How should we use new types like int and double? They look similar to the built-in types, but only start with an uppercase letter. These types are used exactly the same as the corresponding built-in types. The only difference is that they all have a default constructor that initializes them to zero. In this case, the myclass class can be used as an example:

class MyClass{public:MyClass(){}explicit MyClass(const Apple& apple){}MyClass(const string& some_text,double weight):some_text_(some_text){}private:Int int_data_;std::string some_text_;};

Here, the variable int_data _ is declared as the int type starting with an uppercase letter, rather than the int type. In this way, we do not need to add the code for initialization to all constructors. It is automatically initialized to zero.

There is actually another difference: When we use a built-in type, trying to divide it by zero may lead to different results, depending on the compiler and the operating system. To ensure consistency, this runtime error will result in calling the error handler function that is the same as processing other errors, so that we can debug the error.

Robust code should not reference variables before they are initialized. However, if this happens, it would be much better to give uninitialized variables a zero-level security value than random junk values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.