Built-in type 0 initialization issues

Source: Internet
Author: User

For example, with the following piece of code, we can quickly realize that both X and PTR start with undefined values, but SC obviously calls its default constructor to get a reasonable initialization.

Class someclass{

Public

SomeClass (): Val () {}

Private

int Val;

}

void Foo () {
int x;
int *ptr;

SomeClass SC;

......

}

In C + +, there is some difference between handling the underlying type (int, bool, float, double, pointer type, etc.) with the class type. The underlying type does not exist so-called default constructors for initialization.

In the template programming, you can use the following scheme, so that even for the built-in type, you can get the appropriate initialization, the integer initialization of 0, the pointer type is also initialized to 0.

Template <typename t> void foo () {     T x = t ();    is  zero (or false  

Template <typename t> class MyClass {   private:     T x;   Public:     MyClass (): X () {was  initialized even for built-in types     }      

Built-in type 0 initialization issues

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.