在成员函数中使用shared_ptr作为私有变量C++
Use shared_ptr among member functions as private variable C++
我想在 class 的几个成员函数中重用 shared_ptr
。
#include <iostream>
#include <memory>
class TestClass
{
private:
std::shared_ptr<int> int_shared_ptr;
public:
TestClass()
{
std::cout << "I will create a shared ptr object here.";
std::shared_ptr<int> int_ptr (new int(10));
std::shared_ptr<int> int_shared_ptr(int_ptr);
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
void check_if_shared()
{
std::cout << "The created shared ptr has value of " << int_shared_ptr << "\n";
}
};
int main(){
auto tc = TestClass();
tc.check_if_shared();
}
输出
I will create a shared ptr object here.The created shared ptr has value of 10
The created shared ptr has value of 0
int_shared_ptr
一旦离开构造函数似乎就被破坏了。谁能建议一种在离开构造函数后保留共享指针的方法?
行
std::shared_ptr<int> int_shared_ptr(int_ptr);
创建一个与成员变量同名的函数局部变量。成员变量保持默认初始化。使用:
TestClass() : int_shared_ptr(new int(10))
{
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
使用std::make_shared
更符合习惯:
TestClass() : int_shared_ptr(std::make_shared<int>(10))
{
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
所以我认为您正在尝试使用多个共享指针(实例成员)保存对同一个 int
的引用。这个简单的解决方案怎么样:
#include <memory>
class TestClass
{
private:
// you can have any number of shared pointers here
std::shared_ptr<int> shared_0;
std::shared_ptr<int> shared_1;
std::shared_ptr<int> shared_2;
public:
TestClass()
{
// create new int once
shared_0 = std::make_shared<int>(10);
// copy references only
shared_1 = shared_0;
shared_2 = shared_0;
std::cout << "shared_0: " << *shared_0
<< "\nshared_1: " << *shared_1
<< "\nshared_2: " << *shared_2 << "\n";
}
void check_if_shared()
{
std::cout << "shared_0: " << *shared_0
<< "\nshared_1: " << *shared_1
<< "\nshared_2: " << *shared_2 << "\n";
}
};
int main()
{
auto tc = TestClass();
tc.check_if_shared();
// now all shared pointers point to the same int
}
输出:
shared_0: 10
shared_1: 10
shared_2: 10
shared_0: 10
shared_1: 10
shared_2: 10
我想在 class 的几个成员函数中重用 shared_ptr
。
#include <iostream>
#include <memory>
class TestClass
{
private:
std::shared_ptr<int> int_shared_ptr;
public:
TestClass()
{
std::cout << "I will create a shared ptr object here.";
std::shared_ptr<int> int_ptr (new int(10));
std::shared_ptr<int> int_shared_ptr(int_ptr);
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
void check_if_shared()
{
std::cout << "The created shared ptr has value of " << int_shared_ptr << "\n";
}
};
int main(){
auto tc = TestClass();
tc.check_if_shared();
}
输出
I will create a shared ptr object here.The created shared ptr has value of 10
The created shared ptr has value of 0
int_shared_ptr
一旦离开构造函数似乎就被破坏了。谁能建议一种在离开构造函数后保留共享指针的方法?
行
std::shared_ptr<int> int_shared_ptr(int_ptr);
创建一个与成员变量同名的函数局部变量。成员变量保持默认初始化。使用:
TestClass() : int_shared_ptr(new int(10))
{
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
使用std::make_shared
更符合习惯:
TestClass() : int_shared_ptr(std::make_shared<int>(10))
{
std::cout << "The created shared ptr has value of " << *int_shared_ptr << "\n";
}
所以我认为您正在尝试使用多个共享指针(实例成员)保存对同一个 int
的引用。这个简单的解决方案怎么样:
#include <memory>
class TestClass
{
private:
// you can have any number of shared pointers here
std::shared_ptr<int> shared_0;
std::shared_ptr<int> shared_1;
std::shared_ptr<int> shared_2;
public:
TestClass()
{
// create new int once
shared_0 = std::make_shared<int>(10);
// copy references only
shared_1 = shared_0;
shared_2 = shared_0;
std::cout << "shared_0: " << *shared_0
<< "\nshared_1: " << *shared_1
<< "\nshared_2: " << *shared_2 << "\n";
}
void check_if_shared()
{
std::cout << "shared_0: " << *shared_0
<< "\nshared_1: " << *shared_1
<< "\nshared_2: " << *shared_2 << "\n";
}
};
int main()
{
auto tc = TestClass();
tc.check_if_shared();
// now all shared pointers point to the same int
}
输出:
shared_0: 10
shared_1: 10
shared_2: 10
shared_0: 10
shared_1: 10
shared_2: 10