C ++ STRUCTURE REVIEW AND STRUCTURE REVIEW

Source: Internet
Author: User

C ++ STRUCTURE REVIEW AND STRUCTURE REVIEW

Structure: struct, used to store a certain type of variables, in the cpp file.

Struct DirLight {

Vec3 direction;

Vec3 ambient;

Vec3 diffuse;

Vec3 specular;

};

Uniform DirLight dirLight; // you can use dirlignt. ambient to access a property.

The structure is suitable for classifying a class of attributes without creating a class. For example, you do not need to create a lamp class when creating all the properties of a lamp. We can call it through the lamp. Property.

Halt: exit the program.

# The exit (0) method in include can exit the program immediately.

Switch: (do not forget for a long time)

Switch (color)

{

CaseCOLOR_BLACK:

Std: cout <"Black ";

Break;

CaseCOLOR_WHITE:

Std: cout <"White ";

Break;

Default:

Std: cout <"Unknown ";

Break;

}

Static_cast (x) is incorrect. static_cast (y) can only implement forced conversion of basic types.

Same as Java, break jumps out of the entire loop, while continue jumps out of this loop.

Generate random number:

# Include // for rand () and srand ()

Intmain ()

{

Srand (5323); // sets an initial value and is called only once (0--32767)

Std: cout <rand () <"\ t"; // call repeatedly to return a new random number

// Multiple runs will return the same sequence (if the initial values in srand () are the same ). If you want to return different sequences, You can input a time value because the time is always different.

Srand (static_cast (time (0 )));

Return0;

}

Generate a random number within the given range: (call getRandomNumber () to generate a random number between 1 and 6)

IntgetRandomNumber (intmin, intmax)

{

Staticconstdoublefraction = 1.0/(static_cast (RAND_MAX) + 1.0); // static used for efficiency, so we only calculate this value once

// Evenly distribute the random number within SS our range

Returnmin + static_cast (max-min + 1) * (rand () * fraction ));

}

Consider how users abuse programs, especially in text input. For each text input point, consider: Has the extraction failed? Can Users enter more input than expected? Can Users enter meaningless input? Can the user overflow input?

Therefore, you need to test and repair the user input:

If (std: cin. fail () // if user input fails

{

Std: cin. clear (); // clears the input buffer.

Std: cin. ignore (32767, '\ n'); // failed to remove the input.

}

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.