我有一些关于在boost库中实现的智能指针的问题.是shared_ptr的和scoped_ptr的没有拷贝构造函数和shared_ptr的有它scoped_ptr的之间的唯一diffrence?当对象不调用复制构造函数时,我应该总是使用scoped_ptr而不是shared_ptr吗?我也不明白共享/作用域数组的想法.我不能只使用std :: vector而不是它吗?
我想使用std :: vector.push_bask在for循环中存储20个shared_ptr.但从输出结果来看:它似乎只推了一个.并且vector :: push_back> 20次按设计工作.
有些身体可以帮助我吗?
class IObserver{
public:
int virtual notify() = 0;
};
class CAdsUser
{
public:
int do_ads() {cout << __FUNCTION__<<endl;m_observer->notify(); return 0;}
CAdsUser(tr1::shared_ptr<IObserver> b):m_observer(b){cout << __PRETTY_FUNCTION__<<endl;};
~CAdsUser() {cout << __PRETTY_FUNCTION__<<endl;}
private:
tr1::shared_ptr<IObserver> m_observer;
};
class CMgr:public IObserver
{
public:
int virtual notify() { cout << __PRETTY_FUNCTION__<<endl; return 0;};
virtual ~CMgr(){cout << __PRETTY_FUNCTION__<<endl;};
};
using namespace std;
typedef vector<tr1::shared_ptr<CAdsUser> >VecType;
int main() {
tr1::shared_ptr<CMgr> g(new CMgr());
VecType m_online_user;
int i = 0;
for(i = 0; i …Run Code Online (Sandbox Code Playgroud) class a
{
private:
std::shared_ptr <std::string> sptr;
public:
void set(std::string & ref)
{
sptr = &ref; //error
}
};
Run Code Online (Sandbox Code Playgroud)
解决方案是什么?我需要保持引用作为参数,我需要私有指针为shared_ptr.
所以我有一些代码:
class Thing
{
public:
Thing() = default;
};
class DbOfThings
{
public:
DbOfThings() = default;
std::weak_ptr<Thing> GetEntry(int someKey) const
{
// returns a weak_ptr from mThings, or null if a Thing
// that has someKey was not found
return std::weak_ptr<Thing>(nullptr);
}
private
// Idea is that this object owns these things, but other objects can grab a thing from here - but the thing can be killed off at any time
std::vector<std::share_ptr<Thing>> mThings;
Run Code Online (Sandbox Code Playgroud)
但这无法编译:
参数1从'std :: nullptr_t'到'const std :: …
有人可以解释为什么以下崩溃?我正在使用enable_shared_from_this以便bob不会被删除.
class Person : public std::enable_shared_from_this<Person> {
private:
std::string name;
public:
Person (const std::string& name) : name(name) {}
std::string getName() const {return name;}
void introduce() const;
};
void displayName (std::shared_ptr<const Person> person) {
std::cout << "Name is " << person->getName() << "." << std::endl;
}
void Person::introduce() const {
displayName (this->shared_from_this());
}
int main() {
Person* bob = new Person ("Bob");
bob->introduce(); // Crash here. Why?
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用C++中的唯一标识符编号为派生类创建通用接口.
这就是我的代码的样子(至少需要C++ 11来编译它):
#include <iostream>
#include <memory>
class Base
{
protected:
int ident;
Base(int newIdent) : ident(newIdent) { }
public:
Base() : ident(0x01) { }
virtual ~Base() { } //needed to make class polymorphic
int getIdent() { return ident; }
};
class Derived : public Base
{
protected:
int answer;
Derived(int newIdent, int newAnswer) : Base(newIdent), answer(newAnswer) { }
public:
Derived(int newAnswer) : Base(0x11), answer(newAnswer) { }
int getAnswer() { return answer; }
};
int main()
{
std::shared_ptr<Base> bPtr = …Run Code Online (Sandbox Code Playgroud) 我一直在用这个:
struct Bar;
struct Foo
{
virtual Bar * GetBar() { return nullptr; }
}
struct Bar : public Foo
{
virtual Bar * GetBar() { return this; }
}
Foo * b = new Bar();
//...
b->GetBar();
Run Code Online (Sandbox Code Playgroud)
我用这个而不是慢dynamic_cast.现在我已经改变了我要使用的代码std::shared_ptr
std::shared_ptr<Foo> b = std::shared_ptr<Bar>(new Bar());
Run Code Online (Sandbox Code Playgroud)
如何更改GetBar返回的方法std::shared_ptr并获得与原始指针相同的功能(不需要dynamic_cast)?
我试过这个:
struct Foo : public std::enable_shared_from_this<Foo>
{
virtual std::shared_ptr<Bar> GetBar() { return nullptr; }
}
struct Bar : public Foo
{
virtual std::shared_ptr<Bar> GetBar() { return shared_from_this(); } …Run Code Online (Sandbox Code Playgroud) 我正在重新设计游戏引擎以使用smart-pointers。我有一Object门课,所有东西都从那里继承。我有一个可渲染的GameObject,因此它是从IRenderable(定义纯虚拟渲染函数的类)继承的,而不是从Object继承的。我有一个RenderSystem应该shared_ptr在场景中容纳所有IRenderable的对象。
我遇到的问题是如何将我的GameObject shared_ptr投射到RenderSystem的IRenderable?
我尝试过的想法:
这完全可以使用原始指针完成,因此,我觉得可以通过智能指针找到某种方法来达到相同的结果
例:
// Object.h
class Object : public enable_shared_from_this<Object> { ... }
// GameObject.h
class GameObject : public Object { ... }
// MeshRenderer.h
class MeshRenderer : public GameObject, IRenderable {
public:
void initialize()
{
// Not able to cast Object to IRenderable
RenderSystem::instance().addRenderable(getShared());
// AND
// Not able to cast Object to IRenderable
RenderSystem::instance().addRenderable(std::static_pointer_cast<IRenderable>(getShared()));
}
}
// RenderSystem.h
class …Run Code Online (Sandbox Code Playgroud) 我试图以类的朋友的身份重载ostream运算符以构建电路的组件,但是它不断返回地址。
在“ Circuit_classes.h”文件的串联电路类中:
friend ostream& operator<< (ostream& os, series_circuit const& myCircuit);
Run Code Online (Sandbox Code Playgroud)
在文件“ Circuit_classes.cpp”中:
ostream& operator<<(ostream& os, series_circuit const& myCircuit){
os << "Output: " << myCircuit.frequency << endl;
return os;
}
Run Code Online (Sandbox Code Playgroud)
在frequency类头文件中定义为60的位置。
在我的主程序中,“ AC Circuits.cpp”
vector<shared_ptr<circuit>> circuit_vector;
circuit_vector.push_back(shared_ptr<circuit>(new series_circuit));
cout << circuit_vector[0] << endl;
Run Code Online (Sandbox Code Playgroud)
运行程序时在命令行中输出:
0325E180
Run Code Online (Sandbox Code Playgroud) 通常,如果您使用a std::shared_ptr指向对象,并且要创建另一个指向该对象的不共享所有权的指针,则会创建一个std::weak_ptr。
// Create a shared pointer to own the object
std::shared_ptr<int> p = std::make_shared<int>(42);
// Create a weak pointer (that does not own the object)
std::weak_ptr<int> q(p);
// Use the weak pointer some time later
if (std::shared_ptr ptr = q.lock()) {
// use *ptr
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,涉及到该如何处理std::unique_ptr?
使用唯一指针可确保当前资源由其std::unique_ptr自身专有。但是,如果我想创建一个指向不拥有该资源的资源的指针,该怎么办?我不能使用a,std::weak_ptr因为弱指针旨在与的引用计数一起使用std::shared_ptr。我会在这里使用原始指针吗?还是有更好的选择?
// Create a unique pointer to own the object
std::unique_ptr<int> p = std::make_unique<int>(42);
// Create a non-owning pointer to the same …Run Code Online (Sandbox Code Playgroud) c++ ×10
shared-ptr ×10
c++11 ×5
pointers ×2
boost ×1
class ×1
for-loop ×1
friend ×1
inheritance ×1
push-back ×1
reference ×1
scoped-ptr ×1
unique-ptr ×1
vector ×1
weak-ptr ×1