C + + operator overloading and some basic concepts

Source: Internet
Author: User
Tags arithmetic operators shallow copy

C++primer Fourth Edition 435

1. Assignment (=), subscript ([]), Call (()), member Access Arrow (-) operator must be defined as member, when defined as non-member, compiler error

2. Like a value assignment. A conforming assignment operator should normally be defined as a member of a class, but no compilation error occurs when a compound assignment operator is defined as a non-member

3. Some other operators, such as self-increment, self-subtract, and dereference, that change the state of an object or a given type, should normally be defined as members of a class

4. Symmetric operations, such as arithmetic operators, equality operators, relational operators, and bit operators. Best defined as a normal non-member function


Operators that cannot be overloaded:

::       .*      . ? :

Basic Attention:

1. member variables are initialized in the same order as they were declared, regardless of the order in the initialization list2The case that must be initialized in the initialization list: Members of class type without default constructors                         &NBSP ;                          ,         &NB Sp;const member variables for type members and reference types                 &NBSP;--&GT;CONST type members call copy constructors and reference types do not invoke Shellfish constructor class B;class a{    friend class b;    public:    A ()     {    &NBSP ;   A_ = 0;        cout << "A ()" << endl;   }    A (int a): A_ (a) &NB Sp   {        cout << "A (int)" << endl;   }    A (const A &s) &NB Sp   {        A_ = s.a_;        cout << "A &" << endl;  &NB Sp }    private:    int a_;}; Class B:public a{    public:        B (a &a2, A &AMP;A3)            : a2_ (A2),            a3_ (A3)     {    }        void Prin ()         {          &NBS P cout << a2_.a_ << "<< endl;       }    private:      &N Bsp A a1_;        A &a2_;        const a a3_;}; int main (int argc, const char *argv[]) {    a A1 (4), A2 (5), A3 (6);//--> A (int) a (int) a (int)     b b (A2, A3);    B.prin ();    return 0;} -Friend class, you can access the private members of Class A

two ways to initialize 3.c++: 1. Direct initialization: Calling constructors2. Copy initialization: Use the = number operator, which is called to the copy constructor4.A A1;fun (a) is copied by the copy constructor when the function parameter or return value is a class type. ---> Call fun (a) calls the copy constructor and, of course, calls the destructor5.The copy constructor is the reason for the reference: prevents the copy constructor from being called indefinitely. A copy constructor of a type is called when copying a constructor, so infinity6.Deep copy shallow copy: Shallow copy: Simple assignment of data members deep copy: For dynamic members in the object, can not be simple assignment, but should be dynamically allocated space7.Spatial C + + objects: Call the base class constructor first, initialize the members on the list, and then call your own constructor---when the destructor is reversed8. Overloaded Overlay Hidden Concepts9. The difference between a conversion constructor and a type conversion function. Type Conversion functions Note the following: 1. member functions, 2. Cannot specify a return type, 3. Cannot have a reference-->operator int ()--in the function to return the same type of out.Polymorphism: Static polymorphism (overloaded (operator). function)) Dynamic polymorphism: virtual functionOne by one .: Use pointers to access non-virtual functions: When you access a pointer's type, use the pointer to visit a virtual function: the type of the object that the pointer refers to---> reference is similar, but the reference cannot be changed once it is declared.Conditions that trigger dynamic binding: 1. virtual function ability to run dynamic Binding 2. A function call must be made through a pointer or reference to a base class type 13. Constructors cannot be virtual: Virtual functions produce different actions for objects of different types, and there are no virtual function pointers. No virtual functions are visited.

13. Dynamic execution-time type recognition (RTTI): typeID Returns a pointer or refers to the actual type of the object being referred to dynamic_cast a pointer to the base class Or a reference to a safe conversion of a pointer to a derived type or reference 14. Explicit Conversions: reinterpret_cast coercion Type conversions--> type conversions similar to C const_cast convert the const attribute or add con The St attribute static_cast Implicit type conversion, the dynamic_cast that can be self-actively converted is mainly used for the downstream conversion with virtual function.


??

C + + operator overloading and some basic concepts

Related Article

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.