Differences between statements and definitions in C ++

Source: Internet
Author: User

Questions from csdn:

I always thought that definition = Declaration + initialization, that is, int I; Is declaration, int I = 0; Is definition.
But according to Hou Jie's TranslationValid C ++The string PS is also a definition (in the section where the clause definition should be as backward as possible). if the definition is too early, the constructor burden will be introduced too early.
Have I ever been wrong with the concepts of declarations and definitions?
If the Declaration does not cause memory allocation, how can we differentiate definitions and declarations?

I have selected several reliable answers in my reply downstairs to summarize them, in case I forget them in the future.

The rigorous C ++ semantics of the statement is used to tell the compiler type and its details, for example:

 
ClassMyclass
 
{
 
// Data member details...
 
// Member function details...
 
};

The above statement only tells the compiler that there is a custom type of myclass. The Compiler only performs vocabulary analysis and name resolution on it, and does not occupy the memory!

The strict C ++ semantics of "Definition", that is, memory occupation. The compiler will address the object in the relative memory address!

Note that we cannot simply say

 
StringMystring;

Whether it is a declaration or definition. The determination principle is to check whether the memory is occupied. For example:

 
ClassMyclass// Class declaration, no memory occupied{StringMystring;// String Declaration};

However

 
# Include <iostream>
 
 
 
// Global scope
 
StringMystring;// Definition. mystring is the instantiated string!
 
 
IntMain ()
 
{
 
// Main function body
 
StringMyanotherstring;// Definition. myanotherstring is the instantiated string!
 
Return0;
 
}

Therefore, I replied:

Variables and objects without extern are always defined, except in classes.

Only function headers are declarations, and function bodies are definitions.

Class is always a declaration. The function body of a class member function is defined.

 
ClassMyclass
 
{
Static IntX;// Here, X is the Declaration
 
Static Const IntA;// Here a is the statement
 
// Non-static variables are allocated during class instantiation.
 
Myclass ();// The function here is the Declaration
 
};
 
IntMyclass: X;// This is the definition
 
Const IntMyclass: A = 11;// This is the definition

How is the distinction made? Hope you can answer your questions.

Original post: Click here!

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.