Differences in declaration and definition in C + +

Source: Internet
Author: User
Tags class definition function definition

declares that there is something associated with this name, and that it is of this type, tells the compiler I want to use it, and expects it to be defined in a certain place. A definition definition is the provision of all necessary information (memory size) so that it can create an entire entity. We must understand that once a definition is declared, a function, class, or variable can be declared and defined at the same time, but not necessarily. Defining a function means providing the body of the function;voidfunc () {}; Defining a class means giving the methods and members of the class;classa {}; Defining a variable means telling the compiler the type of the variable and where the program is going to create the variable;intA; Declare a variable by default, you must provide a definition! How do I just declare a variable?  Please use extern! extern intA; Simply declaring a variable without creating it. When the extern modifier variable is initialized, it loses the extern-only declared effect, which represents the definition. extern intA =3; When extern modifies a function definition as well as a class definition, it also loses the effect of extern only declarations. Say extern, let's take a look at C.++the storage classes storage classes defines the C++The variables in the program, the scope of the function, and the life cycle. The default storage classes register for all local variables of auto is used for local variables, which declare that the variable is stored in a register and cannot be used&Fetch address Arithmeticexternused for global variables or functions, declaring that the variable or function is globally visible, telling the compiler that the variable or function can be found somewhere in the programStaticused for global variables or functions, declaring that the variable or function is visible in this compilation unit (. o file), different. o files contain the same Staic variable or function with the same name, there will be different copies, and the compilation will not be error; C++, Static is also used for data members in classes and member functions local variables default is auto non-const global variable default is EXTERN,CONST global variable default is static all variables default declaration is definition function default is extern analysis ExampleintA//a declaration is definedA is of type int (declaration); int type occupies 4 bytes (definition);extern intA//StatementA is just an int type (declaration)classAintA;};//a declaration is definedA is a class type (declaration);classA has an int member A, which occupies 4 bytes (definition);classA//StatementA is a class type (declaration), the class type does not know how many bytes are occupied, class is a local property,    Only for this compilation unit (. o file), the same compilation unit (. o file) cannot have the same name as the class; different compilation units (. o files) can exist in different classes, and no link error a obj_a will occur; //a declaration is definedObj_a is a Class A type (declaration);classType a occupies 4 bytesexternA obj_a;//StatementObj_a is just a type of Class A;

Differences in Declaration and definition in C + +

Related Article

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.