简体   繁体   English

与类共享指针

[英]shared pointers with classes

I'm amazed after seeing the example code. 看到示例代码后,我感到惊讶。 Because when I was saying myself, I got eventually what smart pointers are doing. 因为当我说自己的时候,我最终得到了智能指针在做什么。 But seems not yet. 但是似乎还没有。 I really don't understand how the output shows 2014 . 我真的不明白输出如何显示2014 As far as I know, as far as it seems as well, the classes are apart. 据我所知,就看起来而言,这些类是分开的。 So there can't be relationship between them excepting inheritance, polymorphism, nested classing etc. I probably skip over some important points while studying smart pointers. 因此,除了继承,多态性,嵌套分类等之外,它们之间没有其他关系。在研究智能指针时,我可能会跳过一些重要的观点。 Can someone illuminate the student? 有人可以照亮学生吗?

#include <iostream>
#include <memory>

class classA
{
    std::shared_ptr<int> ptA;
public:
    classA(std::shared_ptr<int> p) : ptA(p) {}
    void setValue(int n) {
        *ptA = n;
    }
};

class classB
{
    std::shared_ptr<int> ptB;
public:
    classB(std::shared_ptr<int> p) : ptB(p) {}
    int getValue() const {
        return *ptB;
    }
};

int main()
{
    std::shared_ptr<int> pTemp(new int(2013));
    classA a(pTemp);
    classB b(pTemp);

    a.setValue(2014);
    std::cout << "b.getValue() = " << b.getValue() << std::endl;


}

Raw pointer output 2013 Code: 原始指针输出2013代码:

#include <iostream>
#include <memory>

class classA
{
    int* ptA;
public:
    classA(int * p) {
        ptA = new int(*p);
    }
    void setValue(int n) {
        *ptA = n;
    }
    ~classA() {
        delete ptA;
    }
};

class classB
{
    int* ptB;
public:
    classB(int *p) : ptB(p) {}
    int getValue() const {
        return *ptB;
    }
};

int main()
{
    int* pTemp(new int(2013));
    classA a(pTemp);
    classB b(pTemp);

    a.setValue(2014);
    std::cout << "b.getValue() = " << b.getValue() << std::endl;


}

What happens is that each std::shared_ptr object have it's own pointer (ie it has a member variable that is a pointer), but all of these pointers point to the same place. 发生的情况是每个std::shared_ptr对象都有其自己的指针 (即,它具有一个作为指针的成员变量),但是所有这些指针都指向同一位置。

Lets take an example using normal "raw" pointers: 让我们以使用普通“原始”指针为例:

int* p = new int(2013);

Now you have one pointer p pointing to some memory. 现在,您有了一个指向某些内存的指针p If we do 如果我们这样做

int* p2 = p;

then we have two pointers, but both are pointing to the exact same memory . 那么我们有两个指针,但是两个都指向完全相同的内存

This is similar to how shared_ptr works, each shared_ptr object have an internal pointer (a member variable), and each time the shared_ptr is copied (like when you pass it to a function) the internal member variable is initialized using the source-objects pointer variable (like the when p2 was initialized in the example above), leading to the member pointer variables of both objects pointing to the same memory. 这类似于shared_ptr工作方式,每个shared_ptr对象都有一个内部指针(成员变量),并且每次复制shared_ptr (例如,将其传递给函数时),都会使用源对象指针初始化内部成员变量。变量(如上例中的p2初始化时一样),导致两个对象的成员指针变量指向同一内存。

Let's remove the smart pointer thing, because it seems to be just confusing you. 让我们删除智能指针的内容,因为它似乎只会使您感到困惑。

Your first code is like this, in summary: 总结,您的第一个代码是这样的:

int v = 2013;

int* p1 = &v;
int* p2 = &v;

*p1++;  // Makes v = 2014, so *p2 = 2014.

Your second version is like this: 您的第二个版本是这样的:

int v1 = 2013;
int v2 = 2013;

int* p1 = &v1;
int* p2 = &v2;

*p1++;  // Makes v1 = 2014, but v2 = 2013

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM