C + + (C + + returns objects with application differences, class member storage)

Source: Internet
Author: User
Tags clear screen

The return object differs from the app:

The time when the copy constructor occurred:

1. Construct a new object A, a b = A;
2. Sending or returning objects

For ordinary variables, the effect of the citation is not very obvious, for the class object, the effect of the object is very high.
A pass-through reference is equivalent to expanding the scope of the original object.

#include <iostream>using namespaceStdclassa{ Public: A ()//No parameter constructor{cout<< This<<"Constructor"<<endl; } ~a ()//destructor{cout<< This<<"destructor"<<endl; AConstA & Another)//Copy Builder{cout<< This<<"cpy constructor from"<<&another<<endl; } A &operator=(ConstA & Another)//Operator overloading{cout<< This<<"operator"<<&another<<endl; }};voidFunc (a A) {}voidFunc1 (A &a) {}intMain () {A x;//Call constructorA y = x;//Copy BuilderFunc (x);//Call copy Builder/*0x61fe9e Constructor0x61fe9d cpy Constructor from0x61fe9e0x61fe9f cpy Constructor from0x61fe9e0x61fe9f destructor0x61fe9d destructor0x61fe9e destructorThere's a constructor, there's a destructor .*/FUNC1 (x);//Do not call any constructors/*0x61fe9e Constructor0x61fe9d cpy Constructor from0x61fe9e0x61fe9f cpy Constructor from0x61fe9e0x61fe9f destructor0x61fe9d destructor0x61fe9e destructor*/        return 0;}

Objects on the stack can be returned, but they cannot return a reference on the stack (unless the object itself is returned).

#include <iostream>using namespaceStdclassa{ Public: A ()//No parameter constructor{cout<< This<<"Constructor"<<endl; } ~a ()//destructor{cout<< This<<"destructor"<<endl; AConstA & Another)//Copy Builder{cout<< This<<"cpy constructor from"<<&another<<endl; } A &operator=(ConstA & another) {cout<< This<<"operator"<<&another<<endl; }}; A func (a &a) {returnA;}intMain () {A x;//Call constructorFunc (x);//A copy construction has occurred, but there is no constructor when the above value is not returned/*0x61feae Constructor0x61feaf cpy Constructor From0x61feae0x61feaf destructor0x61feae destructor*/    return 0;}
int  Main () {A x; Call constructor  A T; //call constructor  t = func1 (x); //x is passed as a parameter and does not call the constructor, but returns a large time when the copy constructor is called, (the value returned by the FUNC1 function is copied to the temporary variable, and then copied from the temporary variable to T, which is used for operator overloading)  cout<<  "&t"  <<&t<<endl; /*  0x61fe9e constructor  0x61fe9d constructor  0x61fe9f cpy constructor from0x61fe9e  0x61fe9d operator0x61fe9f  0x61fe9f destructor  &t 0x61fe9d  0x61fe9d destructor  0x61fe9e destructor     */ return  0 ;}  
A func(){    A b;    return b;}int main(){    A t;    t = func();    cout<<"&t "<<&t<<endl;/*0x61fe9e constructor0x61fe9f constructor0x61fe9e operator0x61fe9f0x61fe9f destructor&t 0x61fe9e0x61fe9e destructor*/    return0;}
A& func(){    A b;    returnthis;//是可以的,因为本身对象还没有销毁,如果返回的是b,返回后func销毁,有时候会正确,有时候会错误。}

Implementation of string-type character concatenation

mystring mystring::operator+(const mystring & another){    mystring tmp;//    delete []tem._str;    int len = strlen(this->_str);    len += strlen(another._str);    newchar[len+1];    memset(tem._str,0,len+1);//必须要的    strcat(tem._str,this->_str);    strcat(tem._str,another._str);    return tmp;}

C vs. C + + about string processing
C based on the character array, using some methods Strcpy,strlen,strcat and other operations.
All C + + operations revolve around character pointers. Through this pointer operation, the whole design idea is the object-oriented idea.

Object-oriented thought practice
#include <iostream>#include <unistd.h>#include <iomanip>#include <time.h>using namespaceStdclassclock{Private:intHourintMinintSec Public: Clock () {time_t t = time (NULL);structTM Ti = *localtime (&t);        hour = Ti.tm_hour;        min = ti.tm_min;    SEC = ti.tm_sec; }voidRun () { while(1) {Show ();//Display functionTick ();//Data with new function}    }Private:voidShow () {System ("CLS");Clear Screen cls under//windowsCOUT&LT;&LT;SETW (2) <<setfill (' 0 ') <":"; COUT&LT;&LT;SETW (2) <<setfill (' 0 ') <<min<<":"; COUT&LT;&LT;SETW (2) <<setfill (' 0 ') <<sec; }voidTick () {Sleep (1);if(++sec = = -) {sec =0; Min + =1;if(++min = = -) {min =0; Hour + =1;if(++hour = = -) {hour =0; }            }        }    }}intMain () {Clock C; C.run ();return 0;}
Storage of Class Members
#include <iostream>using namespaceStdclasstime{Private:intHourintMinintSec Public: Time (intHintMints): Hour (h), Min (M), SEC (s) {}voidDisplay () {cout<//<=>        //cout<<this->hour<<this->min<<this->sec<<endl;}/*void Display (Time *t)    {cout<<t->hour<<t->min<<t->sec<<endl;//yourself by explicitly passing the argument.     }*/};intMain () {Time t (1,2,3), T1 (2,3,4), T2 (4,5,6); cout<<sizeof(time) <<"----"<<sizeof(t) <<endl;    T.display ();    T1.display (); T2.display ();return 0;}

The above code display () function, three objects common. The object passes its own object at the time of the call.
The object has its own storage space, and the function part code is common. The form of representation is that each object is passed into its own object at the time of invocation.

Precautions

1, regardless of whether member functions are defined within a class or outside the class, the code snippets for member functions are stored in the same way.
2, do not confuse this storage method of member functions with the concept of inline (built-in) functions. The logical meaning of inline is to embed the function into the calling code, reducing the cost of the stack and the stack.
3, it should be noted that "the member function of a certain object" is from the logical point of view, and the storage of member functions is from the physical point of view, the two are not contradictory. Similar to two-dimensional arrays are logical concepts, while physical storage is the same as linear concepts.

C + + (C + + returns objects with application differences, class member storage)

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.