C + + Learning-living the Devil's details

Source: Internet
Author: User

13 weeks of C + + courses in the blink of an eye 5 weeks, C + + standards have basically covered. Plus coding hundreds of lines, finally, C + + has a basic understanding. The next study will be about STL, so here is a small summary of what is being learned.

  The Devil is in the detail, which is my biggest and greatest experience. It's about what this is called a standard. More so.

1, assignment, reference, pointer

These three concepts are common in many programming languages, in order to achieve the high efficiency of C + +, these 3 axe is to be played incisively and vividly. To summarize, there are two situations: "Pass" and "return".

The meaning of "pass" assignment is to copy the variable fun (int a) (5). MATLAB is all such calls, MATLAB does not pass the reference. The benefit of this is that the function does not modify the incoming values, and the disadvantage is that the incoming parameters need to be copied, and if the parameters are large (E10 point clouds), replication can be costly.

"Pass" refers to the reference to the variable is passed in, such as fun (int & a), int b; Fun (b). This way, you can change the value of B in a function. The advantage is that the address is transmitted directly, no duplication overhead is required, and the disadvantage is not conducive to variable protection.

The "pass" pointer is similar to a reference, in general, the pointer is a very ugly parameter, but sometimes you have to use pointers as arguments. such as point cloud, often point cloud size is dynamic, memory is also dynamically allocated, so many point cloud processing function parameters are pointcloud<pointxyz>::p TR, the pass pointer changes the content that the pointer points to.

The "return" value means that the function returns a value, often a = Fun (b), and the returned value can only do the right value.

The "return" reference means to return a variable that can have a value or be uninitialized. It can be used as an lvalue, such as a.getobj () = Createobj (a).  or Ostream & operator << (Ostream & O) {return o; } cout<<my_obj<<endl;

"Return" pointer, return a pointer is also helpless, such as the input is a point cloud, return its split subset.

In summary, the return reference is mostly for lvalue, and return is mostly due to overhead concerns.

2. Const and Data protection

There are three usages of const, namely, the combination of several, and the combination of pointers, and functions.

The combination of "sum and sum" is very simple, meaning that the variable cannot be changed. int c, const int a = 5; const int & b = c; In this case a = 1, b= 1 will be error, if you want B = 1, you can make C = 1;

The case of "binding with pointers" and pointers is a bit more complex, with two const int * p = & c; denotes a constant int, which means that the value of C cannot be modified by P, and the usual reference above is the same meaning!! the int const *P = & C means constant *p, meaning that p cannot point to other data.

The combination of "and functions" is a little more complicated than a function. In two cases, the fun (const int & A) indicates that a cannot be modified. Fun (int a) const{},const decorated {}--constant member function, which means that member functions are not allowed to modify member variables. A regular object can only call this constant member function.

3. Construction and destruction

Construction and destruction are the object-oriented path. The main structure is a bit complex, the default structure, common structure, copy construction.

"Default Construction" If we do not write the constructor, the default constructor is automatically generated. But once written, there is no default constructor. If you want a constructor with no parameters, either use the default value to disguise it or write your own MyClass () {}. Suggest the latter, sometimes do not remember the default value will be an accident.

The normal construct constructor is the function of initializing member variables, and if the member variables are private, it is likely that there will be only one chance to initialize.

"Copy Construction" copy construction is the most verbose, it has two forms of invocation: 1, MyClass B; MyClass A (b); 2, MyClass a = B. The second seems to use the operator =, which is not actually, it has the copy constructor, not the operator overload. If you reload =, it's best not to have two semantics.

Destructor destructor The most important thing is that if you apply a variable with new, you must delete [] in the destructor. Of course, if you use boost smart pointers, you don't have to release them manually.

4. Operator overloading

Operator overloading is better understood, remember a detail, the left value to return a reference.

5. Inheritance

Inheritance is the operation of is, if a class inherits its husband class, then the object of the subclass, in memory, will have all the member variables of the parent class! and automatically gets all the member functions of the parent class. However, the obtained member function can only manipulate those member variables of the parent class in memory.

The most important thing about design inheritance is that subclasses must get all the properties of the parent class. If the parent class has a behavior that is not required by the subclass, then even the design is not good.

The difficulty in sub-classes is the design of the constructor, because the member variables of the parent class in memory are assigned values, so its constructor is written as follows: Son (int a,int b,int c): Father (A, b), C_ (c) {}. Use the class name of the parent class directly to complete the assignment of the member variable. A simple example of inheritance is as follows:

1#include <iostream>2#include <cstring>3#include <cstdlib>4 using namespacestd;5 6 classMyString: Public string7 {8  Public:9MyString (ConstMyString & S):string(s) {};TenMyString (Const string& S):string(s) {}; OneMyString (Const Char* s):string(s) {}; A MyString () {}; -  -MyStringoperator()(intStart_index,intlength) { the  -MyString substr = This-substr (start_index,length); -     returnsubstr; -  + } -  +~MyString () {}; A }; at  -  -  - intCompareString (Const void* E1,Const void*E2) { -MyString * S1 = (MyString *) E1; -MyString * s2 = (MyString *) E2; in     if(*s1 < *S2)return-1; -     Else if(*s1 = = *s2)return 0; to     Else if(*s1 > *s2)return 1; + } -  the intMain () { *MyString S1 ("abcd-"), S2,S3 ("efgh-"), S4 (S1); $MyString sarray[4] = {"Big","Me"," About"," Take"};Panax Notoginsengcout <<"1."<< s1 << S2 << s3<< s4<<Endl; -S4 = S3; S3 = S1 +S3; thecout <<"2."<< S1 <<Endl; +cout <<"3."<< S2 <<Endl; Acout <<"4."<< S3 <<Endl; thecout <<"5."<< S4 <<Endl; +cout <<"6."<< s1[2] <<Endl; -s2 = S1; S1 ="ijkl-"; $s1[2] ='A' ; $cout <<"7."<< S2 <<Endl; -cout <<"8."<< S1 <<Endl; -S1 + ="Mnop"; thecout <<"9."<< S1 <<Endl; -S4 ="qrst-"+S2;Wuyicout <<"."<< S4 <<Endl; theS1 = s2 + S4 +"UVW"+"XYZ"; -cout <<"One by one ."<< S1 <<Endl; WuQsort (Sarray,4,sizeof(MyString), comparestring); -      for(inti =0; I <4;++i) Aboutcout << Sarray[i] <<Endl; $     //output S1 from subscript 0 starting with a substring of length 4 -cout << S1 (0,4) <<Endl; -     //output S1 from a sub-string with a length of 5 starting from subscript 10 -cout << S1 (5,Ten) <<Endl; A     return 0; +}
View Code

Here, MyString did not say, but got all the string operator overloads (=,+ ...). There are also all the functions (SUBSTR). The only thing that needs to be well designed is the constructor.

  

C + + Learning-living the Devil's details

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.