C + + problem--explicit constructors, constant reference return values, shading __jquery

Source: Internet
Author: User
Tags shallow copy
Explicit Constructors

All single parameter constructors must be explicit to avoid background type conversions. Otherwise, some loose rules allow for type conversions without displaying a type conversion operation.

A A;
A = 37;

The above code constructs a object A and assigns a value. The assignment statement does not work because the right side of the assignment symbol is not another a object.
However, C + + has loose rules, usually a single parameter constructor that defines an implicit type conversion (implicit type conversion) that creates a temporary object that makes the assignment (or function arguments) compatible.
In this case, the compiler attempts to convert a = 37 to a temp (37); A = temp;
The construction of a temporary object can also be implemented by using a single parameter constructor. using explicit means that a single parameter constructor cannot be used to create an implicit temporary object. Parameter Pass summary for small objects that are not changed by a function by value invocation of a large object invocation that is not changed by a function applies to all objects that can be changed by a function are returned by a constant reference

If the object returned is a class type, returning with a constant reference can save the cost of replication. The const here means that the returned object cannot be modified by itself.
If you want to return a member of this object by reference from a constant member function, you should use a constant reference to return it, that is, the const x&. That is, if what you want to return by reference is logically part of this object (regardless of whether it is physically embedded in this object), then the constant method needs to be returned by a constant reference or by a value, not by a very literal reference.

An example is given below:

Const string & Findmax (const vector<string> & arr)
{
    int maxindex = 0;
    for (int i=1;i<arr.size (); i++)
        if (Arr[maxindex] < arr[i])
            maxindex = i;
    return arr[maxindex];
}

Here, the vector of the expression Arr[maxindex] index is outside the Findmax and exists longer than the return time of the call, which is still valid when the function returns, so it can be returned with a constant reference. operator[]

The following procedure let's discuss return references and constant references.
What this program means is that if there is a matrix M, then M[i] should return a vector of the row I of the corresponding matrix M.
Operator[] should return the vector entity. Returning references can reduce the consumption of object replication.

#include <vector>
using namespace std;
Template <typename object>
class matrix
{public
    :
    ...
    Const vector<object> & operator[] (int row) const
    {return
        array[row];
    }
    Vector<object> & operator[] (int row)
    {return
        array[row];
    }

    ...
    Private:
    vector< vector<object> > array;
}

If operator[] Returns a constant reference, the returned reference cannot be the left value, and if just to access the value of the matrix, it is not a good design to return a constant vector.
So what's really needed is operator[] to return a constant reference and a normal reference two versions. But the return value is not part of the function signature, and the const is part of the signature, so the access function that returns the constant reference is set to the const member function. So we can get access to the function version of operator[] to return a constant reference, while modifying the version of the function returns a simple reference. data member is a class of pointers

Suppose the class contains only one pointer data type, and the pointer points to an object that dynamically assigns an address. The default destructor does not operate on the pointer. Also, the copy constructor and operator= do not copy the object that the pointer points to, but simply copy the value of the pointer. As a result, you get two class instances that contain pointers that point to the same object. This is referred to as a shallow copy (shallow copy, where the pointer is copied rather than the object being copied by the pointer ).
In general, we expect a deep copy of the entire object to be cloned (deep copy). Thus, when a class contains data members that are pointers and deep replication is important, the general practice is to implement destructors, operator=, and copy constructors. reference materials

Constant function, constant reference parameter, constant reference return value C + +

reprint Please indicate the author Jason Ding and its origin
GitHub Blog homepage (http://jasonding1354.github.io/)
CSDN Blog (http://blog.csdn.net/jasonding1354)
Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)
Baidu Search jasonding1354 access to my blog homepage

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.