java - C++返回对象的指针或者引用有什么区别?
阿神
阿神 2017-04-18 10:10:12
0
3
530
阿神
阿神

闭关修行中......

reply all(3)
Peter_Zhu

References in C++ are actually just restricted pointers, but there are semantic restrictions on the restricted pointer that cannot be used to perform some operations that pointers can do: reassignment, copy, ++, -- (I haven’t thought of others yet. ), and restricts the null value to improve security. It can be understood as a protection mode of the compiler for pointers.

So, consider when should you use references and when should you use pointers? I think there are a few points:

  1. Citation first

    Use references wherever references can be used instead of pointers.

  2. Pointers are allowed to be null, but references are not.

    const stock* top(...)
    {
        return NULL;
    }

    Of course, in this case you can still stick to using references, and in the case of empty objects, you can express it by throwing exception:

    const stock& top(...)
    {
        throw null_top_stock_exception();
    }
  3. Reasonable use semantics implies, pointers imply possession (has), references imply association (use):

    car has a wheel

    class wheel {};
    class car
    {
        wheel* _wheel;
    public:
        car() : _wheel(NULL) {};
        void set_wheel(wheel* wheel) { _wheel = wheel; };
    };

    trip use a car:

    class trip
    {
        const car& _car
    public:
        trip(const car& car) : _car(car) {};
    };

    Of course, there are exceptions. If the associated _car is required to be variable during the lifetime of the trip, then you can only use pointers (because references cannot be reassigned):

    class trip
    {
        const car* _car
    public:
        trip(const car* car) : _car(car) {};
        void set_car(const car* car) : _car(car) { _car = car; };
    };

    Otherwise, if you insist on using references and want to change the pointer of _car, you can only reconstruct a new trip.

  4. References cannot be saved in containers, only pointers can be used (I guess it is mainly because references are restricted in reassignment and copy operations):

    std::vector<car> cars;  // OK 
    std::vector<car*> cars; // OK
    std::vector<car&> cars; // ERROR
  5. Finally, you can convert the reference into a pointer at any time:

    class maintainer
    {
    public:
        void maintain(car& car)
        {
            car* p = &car;
            // play with pointer 'p'
        }
    };

    But you must pay attention to the situation. Here car is only passed in for use by the maintain method (the reference implies use relationship). Although the pointer operation is not elegant, it is still acceptable, but you must pay attention to saving the pointer for use:

    class maintainer
    {
        car* _last_car;
    public:
        void maintain(car& car)
        {
            car* _last_car = &car;
            // ...
        }
        void review_last_maintain()
        {
            // play with "_last_car"
        }
    };

    After exiting the maintain method, the object pointed to by _last_car is likely to be released.

伊谢尔伦

const is like a bottomless pit
Let’s give an example

#include <iostream>
#include<string>
using namespace std;

class Stu
{
public:
    Stu(string name, int age) // :name(name),age(age)
    {
        this->name = name;
        this->age = age;
    }
    Stu & Add()
        //Stu * const & Add()
    {
        this->age++;
        return *this; 
        // return this;//this 指针是const类型
    }
    void display()
    {
        cout << name << " : " << age << endl;
    }
private:
    string name;
    int age;
};
int main()
{
    Stu s("xiaoming", 20);
    s.display();
    s.Add().Add().Add().Add().Add();
    //Add函数返回引用的好处是,你可以进行连加或者比如运算符重载返回引用类型进行连等于运算 
    //s.Add()->Add()->Add()->Add();
    s.display();
    return 0;
}
巴扎黑

Return NULL is allowed for pointers, but not for references.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!