Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我丢失了这些带有智能指针的构造对象,而不是新的?_C++_Smart Pointers - Fatal编程技术网

C++ 为什么我丢失了这些带有智能指针的构造对象,而不是新的?

C++ 为什么我丢失了这些带有智能指针的构造对象,而不是新的?,c++,smart-pointers,C++,Smart Pointers,我想我对智能指针有些误解。看看下面的例子。当我使用new/*时,我得到的正是我所期望的,但当我使用std::shared\u ptr时,我得到一个空指针错误。智能指针的实现是否与我使用new/*时所做的相同 另外,我是否可以调整另一个给定者以避免多次指针解引用 #include <memory> #include <iostream> class numGiver { public: virtual int giveNum(void) = 0; virtu

我想我对智能指针有些误解。看看下面的例子。当我使用new/
*
时,我得到的正是我所期望的,但当我使用
std::shared\u ptr
时,我得到一个空指针错误。智能指针的实现是否与我使用new/
*
时所做的相同

另外,我是否可以调整另一个给定者以避免多次指针解引用

#include <memory>
#include <iostream>
class numGiver
{
public:
    virtual int giveNum(void) = 0;
    virtual int othNum(void) = 0;
};


class constGiver : public numGiver
{
public:
    int giveNum(void)
    {
        return 5;
    }
    int othNum(void)
    {
        return 5;
    }
};


class othAddGiver : public numGiver
{
public:
    int giveNum(void)
    {
        return myNum + ng->giveNum();
    }
    int othNum(void)
    {
        return ng->othNum();
    }
    othAddGiver(std::shared_ptr<numGiver> ng, int num) : ng(ng), myNum(num) {};
private:
    std::shared_ptr<numGiver> ng;
    int myNum;
};

class AnotherGiver : public numGiver
{
public:
    int giveNum(void)
    {
        return myNum + ng->giveNum();
    }
    int othNum(void)
    {
        return ng->othNum();
    }
    AnotherGiver(numGiver* ng, int num) : ng(ng), myNum(num) {};
private:
    numGiver* ng;
    int myNum;
};


int main()
{
    std::shared_ptr<numGiver> ng = std::make_shared<constGiver>();
    std::shared_ptr<numGiver> og;
    numGiver* anotherGiver = 0;
    for (int i = 0; i < 25; ++i)
    {
        if (i == 0)
        {
            anotherGiver = new AnotherGiver(&*ng, 3);
            std::shared_ptr<numGiver> og = std::make_shared<othAddGiver>(ng, 3);
        }
        else
        {

            anotherGiver = new AnotherGiver(anotherGiver, 3);
            std::shared_ptr<numGiver> og = std::make_shared<othAddGiver>(og, 3);
        }

    }
    std::cout << anotherGiver->giveNum() << std::endl;
    std::cout << anotherGiver->othNum() << std::endl;
    std::cout << og->giveNum() << std::endl;
    std::cout << og->othNum() << std::endl;
    return 0;
}
#包括
#包括
numGiver类
{
公众:
虚拟整数giveNum(void)=0;
虚int othNum(void)=0;
};
类构造者:公共numGiver
{
公众:
int giveNum(void)
{
返回5;
}
int othNum(无效)
{
返回5;
}
};
类别othAddGiver:公共numGiver
{
公众:
int giveNum(void)
{
返回myNum+ng->giveNum();
}
int othNum(无效)
{
返回ng->othNum();
}
othAddGiver(std::shared_ptr ng,int num):ng(ng),myNum(num){};
私人:
std::共享的ptr ng;
int myNum;
};
第二类其他给予者:公共numGiver
{
公众:
int giveNum(void)
{
返回myNum+ng->giveNum();
}
int othNum(无效)
{
返回ng->othNum();
}
另一个给予者(numGiver*ng,int num):ng(ng),myNum(num){};
私人:
numGiver*ng;
int myNum;
};
int main()
{
std::shared_ptr ng=std::make_shared();
std::共享\u ptr og;
numGiver*另一个给定者=0;
对于(int i=0;i<25;++i)
{
如果(i==0)
{
另一个给予者=新的另一个给予者(&*ng,3);
std::shared_ptr og=std::make_shared(ng,3);
}
其他的
{
另一个给予者=新的另一个给予者(另一个给予者,3);
std::shared_ptr og=std::make_shared(og,3);
}
}

std::cout giveNum()您正在使用定义隐藏外部作用域的
og

std::shared_ptr<numGiver> og = std::make_shared<othAddGiver>(ng, 3);
std::shared_ptr og=std::make_shared(ng,3);

std::shared_ptr og=std::make_shared(og,3);

如果从这些行中删除
std::shared_ptr
,则它将
std::shared_ptr og=std::make_shared(ng,3);
您正在重新声明
og
,而不是分配给原始变量。这是因为您使用了三个不同的
og
变量。您应该使用
-Wshadow
/W4
进行编译,以使编译器对这些事情发出警告。谢谢,我没有意识到我做了这么明显的事情。
std::shared_ptr<numGiver> og = std::make_shared<othAddGiver>(og, 3);