C ++ initialization and assignment

xiaoxiao2021-03-06  23

The initialization and assignment operation in C is very large.

There is no difference in basic data types:

such as:

INT A = 12; // Initialization, Copy 0x000c to aa = 12; // Assignment, Copy 0x000c to A But the user-defined data type is much larger than the String initialization and assignment.

Class string {public: String (const char * init); // intendsaLiNIT); String (); string (const string & That); string & operator = (const string & That); string & operator = (const char * STR); Void Swap (String & That); Friend Const String // Concatenate Operator ; Friend Bool Operator <(Const String &, Const String &); // ... private: string (const char *, const gran *); // computational char * s_;}; initialization construction process is relatively simple: allocate a large space and then fill the data: string :: string (const char * init) {if (! init INIT = ""; s_ = new char [strlen (init) 1]; strcpy (s_, init);} Destructure process is simpler: String :: ~ String () {delete [] s_;}

But if the assignment is more complicated: string & string :: Operator = (const char * str) {

IF (! Str) str = ""

Char * TMP = STRCPY (New Char [Strlen (STR) 1], STR);

delete [] s_;

S_ = TMP;

RETURN * THIS;

}

It seems that it is best to assign a value when it is initialized, and avoids the value of = number. Related Links: C Common Knowledge: Assignment and Initialization Are Different Other Robots Chat

Network pick

Recently mood

Love

Ctrlc source code search engine

Image transformation into characters

Bai family name, name query

Tang Poetry, Song Word, Yuanqu Search Engine

Xiaoyu home discussion

转载请注明原文地址:https://www.9cbs.com/read-40098.html

New Post(0)