I do not remember the CPP knowledge

Source: Internet
Author: User

From the network.

Http://www.cnblogs.com/fangyukuan/archive/2010/09/18/1829871.html

Often by the pit

string foo (); void Bar (string&s)//  then the following expression will be illegal:bar (foo ()); Bar (  "HelloWorld");

The reason is that both the Foo () and the "Hello World" strings produce a temporary object, whereas in C + + These temporary objects are const types. So the expression above is an attempt to convert an object of a const type to a non-const type, which is illegal.

I see

What are the formats, benefits, and rules that you need to follow to return a value type as a function?

Format:

Type identifier & function name (formal parameter list and type description)
{
function body
}

Benefit: No copy of the returned value is generated in memory; (Note: It is for this reason that it is undesirable to return a reference to a local variable.) Because with the end of the local variable's lifetime, the corresponding reference will also be invalidated, resulting in runtime error!

Failure to release is key

You cannot return a reference to the memory that is allocated inside the function . This article can refer to effective c++[1] Item 31. Although there is no passive destruction of local variables, there are other embarrassing situations in which the case (a reference to new allocation of memory within the function) is returned. For example, a reference returned by a function is only present as a temporary variable, not given an actual variable, and the space pointed to by the reference (allocated by new) cannot be freed, resulting in memory leak.

Reference again

A reference is another means of producing a polymorphic effect, in addition to pointers. This means that a reference to a base class can point to its derived class instance

Readability and danger

" What is the difference between a reference and a pointer?

The pointer indirectly operates on the variable it points to after it points to an object through a pointer variable. The use of pointers in programs, the readability of the program is poor;

Does not produce a copy (copy constructor called "Copy constructors"?) )

When do I need to "quote"?

Reference is recommended for the stream operator << and >>, the return value of the assignment operator =, the parameter of the copy constructor, the parameter of the assignment operator =, and other cases.

Classic, not tired of watching, fresh students must test

 #include <assert.h> #include  <stdio.h> char  *strcpy (char  *strdest, constchar*< Span style= "color: #000000;" >STRSRC) {assert ((strdest !=null) && (strsrc!=null)); //  2 min    char  * address = strdest; //  2 min  while  ((*strdest++=*strsrc++)!=  ' ) //  2 min  null;     return  address; // } 

An example illustrates the white

classstring{ Public: String (Constchar*STR = NULL);//Common ConstructorsString (ConstString &other);//copy Constructor~ String (void);// DestructorsString &operator=(ConstString &other);//Assignment Function   Private:Char*m_data;//used to save strings}; Answer://Common ConstructorsString::string (constchar*str) {if(str==NULL) {m_data=newchar[1];//score points: Automatically apply for empty string to hold the end flag '//Add points: null judgment on M_data*m_data =' /'; }Else{intLength =strlen (str); M_data=newchar[length+1];//Better if you can add a NULL judgmentstrcpy (m_data, str); }}//destructor for stringString::~string (void) {delete [] m_data;//or Deletem_data;}//copy ConstructorString::string (ConstString &other)//scoring point: input parameter is const type{intLength =strlen (Other.m_data); M_data=newchar[length+1];//Add points: null judgment on M_datastrcpy (m_data, other.m_data);}//Assignment FunctionString & string::operator=(ConstString &other)//scoring point: input parameter is const type{if( This==&other)//score points: Check self-assignment    return* This;     delete [] m_data; //scoring points: releasing the original memory resources   intLength =strlen (Other.m_data); M_data=newchar[length+1];//Add points: null judgment on M_datastrcpy (m_data, other.m_data); return* This;//score points: Returns a reference to this object}

The principle of overloading

void foo (int x, int y);

The function is compiled by the C compiler in the symbol library with the name _foo, while the C + + compiler produces names like _foo_int_int (different compilers may generate different names, but all use the same mechanism, the resulting new name is called "mangled Name ").

_foo_int_int such a name includes the function name, function parameter number and type information, C + + is this mechanism to implement function overloading.

The included header file is actually part of the source file

In the header file of the C language, only the extern type is specified for its external function , and the extern "C" declaration is not supported in theC language, and a compile syntax error occurs when the. c file contains the extern "C".

Aggregation combinations

What are the differences between associations, aggregations (Aggregation), and combinations (composition)?

Involves some of the concepts in UML:

Association is a general relationship that represents two classes, such as "student" and "teacher", which is an association;

aggregations represent has-a relationships, which are relatively loosely related, and aggregate classes do not need to be responsible for the aggregated classes, as shown in an empty diamond representation of the aggregation relationship:

From an implementation perspective, aggregations can be expressed as:

Class A {...} Class B { A * A; ...}

The combination represents the relationship of CONTAINS-A, which is stronger than aggregation: The combined class has the same life cycle as the grouped class, the combined class is responsible for the combined class, and the composite relationship is represented by a solid diamond:

The form of implementation is:

Class a{...} class b{ a a; ...}

Wonder

Rewrite (overried, some books are also called "overlays")

The most feared concept problem

The role of polymorphism?

The main is two:

1. Hide the implementation details, so that the code can be modular, extension code module, implementation of code reuse;

2. Interface reuse: For classes to be inherited and derived , it is guaranteed to use the correct invocation of a property of an instance of any class in the family.

Have a glimpse

What are some of the things that can be used intializationlist and not with assignment?

Answer: When a class contains a const, reference member variable, the constructor of the base class needs to initialize the table.

What is " type safety "?

C + + is it type safe?
Answer: No. Between two different types of pointers can be cast (cast with reinterpret). C # is type-safe.

There was a written examination completely did not understand, originally is this content ~ ~

describe how memory is allocated and how they differ ?
1) allocation from a static storage area . Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example , global variables,static variables .
2) create on the stack . When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the instruction set of the processor.
3) allocation from the heap , also known as dynamic memory allocation . When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by programmers and is very flexible to use, but the problem is the most.

Reminds me of the Olympiad.

when a class A There is no life in any member variable and member function , At this sizeof (A) The value is, if not 0, explain why the compiler didn't let it be zero. (Autodesk)
Answer: Definitely not zero. For example, if it is 0, declare a class A[10] object array, and each object occupies a zero space, then there is no way to differentiate a[0],a[1] ... The

Remember the name, never.

Compare C + + in the 4 type of conversion method?

The emphasis is on the differences and applications of static_cast, dynamic_cast and reinterpret_cast. (Make it up later)

Classic

Float:const EXPRESSION EXP =0.000001
if (a < exp&& a >-exp)

It's a concept question.

Describe the difference between an array and a pointer?
Arrays are either created in a static store (such as a global array) or created on a stack . Pointers can point to any type of memory block ( heap ) at any time.
P[0] = ' X '; The compiler cannot find the error, run-time error

Dizzy

What is the difference between overloading, overriding, and hiding a class member function?
Answer:
A. Features that are overloaded with member functions:
(1) The same range (in the same class );
(2) The function has the same name;
(3) different parameters ;
(4) The virtual keyword is optional.
B. Overwrite refers to a derived class function overriding a base class function, characterized by: (That is, " override ")
(1) different ranges ( in the derived and base classes, respectively );
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword (not being "hidden").
C. " Hide refers to a function of a derived class that masks a base class function with the same name as the following rule:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different . At this point, the function of the base class is hidden, regardless of the virtual keyword (Note that it is not confused with overloading).
(2) If the function of the derived class has the same name as the function of the base class, and the parameters are the same, the base class function does not have the virtual keyword. At this point, the function of the base class is hidden (be careful not to confuse the overlay)

The first time I heard ~

Main after the main function is executed, is it possible to execute a piece of code and give a description?
Answer: Yes, you can register a function with _onexit, it will execute after main;

////

int* p1,int* p2) {  int*p; *p =*p1; *P1 =*p2; *P2 =*p;}

In the Swap function, p is a "wild" pointer, which may point to the system area, causing the program to run in a crash. In VC + + Debug Runtime prompt error "Accessviolation". The procedure should read: int p;

////

MIN (*p++, B) can produce macro side effects

Looks like I'm a rookie.

Please say the static and const keywords as many functions as possible

Answer: The
Static keyword has at least the following n functions:
(1) function body The inner static variable is scoped to the function body, and unlike the auto variable, the memory of the variable is allocated only once, so its value is maintained the last value on the next call, and
(2) in within the module can be accessed by functions used within the module, but cannot be accessed by other functions outside the module;
(3) within the module static function The can only be called by other functions within this module, and the use of the function is limited to the module in which it is declared;
(4) in in class Member variables are owned by the entire class and have only one copy of all objects of the class;
(5) static member functions in a class are owned by the entire class, and this function does not receive the this pointer, so it can access only the static member variables of the class.  

The CONST keyword has at least the following n functions:
(1) To prevent a variable from being changed, you can use the Const keyword. when defining the const variable, it is often necessary to initialize it , since there is no chance to change it again;
(2) for pointers , you can specify that the pointer itself is a const, or you can specify that the pointer refers to a const data, or both of which are specified as const;
(3) in a function declaration , the const can modify the formal parameter, indicating that it is an input parameter, the value cannot be changed inside the function;
(4) For a class member function, if it is specified as a const type, it indicates that it is a constant function and cannot modify the member variables of the class;
(5) For a member function of a class, sometimes it is necessary to specify that its return value is a const type so that its return value is not "left value". For example:
Const ClassA operator* (const classa& a1,const classa& A2);
The return result of the operator* must be a const object. If not, such a perverted code will not compile an error:

ClassA A, B, C;
(A * b) = C; Assigning values to the results of a*b
Operation (A * b) = C clearly does not conform to the intended purpose of the programmer, nor does it make any sense.

Have a glimpse

To explicitly call a destructor

myclass* Pmyclass =new MyClass;
Pmyclass->~myclass ();

To explicitly call a constructor

First: Pmyclass->myclass::myclass ();
Second: New (Pmyclass) MyClass ();

I do not remember the CPP knowledge

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.