有一个包含这些语句的文件:
public:
boost::shared_ptr<TBFControl::TbfCmdHandler> _tbfCmdHandlerPtr;
// will be private later...
boost::shared_ptr<TBFControl::TbfCmdHandler> getTBFCmdHandler()
{ return _tbfCmdHandlerPtr; }
Run Code Online (Sandbox Code Playgroud)
我可以这样使用它:
boost::shared_ptr<TBFControl::TbfCmdHandler>myTbfCmdHandlerPtr(
this->getTBFInstallation()-> _tbfCmdHandlerPtr );
Run Code Online (Sandbox Code Playgroud)
但不是,就像我想要的那样:
boost::shared_ptr<TBFControl::TbfCmdHandler>myTbfCmdHandlerPtr(
this->getTBFInstallation()->getTBFCmdHandler() );
Run Code Online (Sandbox Code Playgroud)
使用getter函数,会发生以下错误:
'Housekeeping :: TBFInstallation :: getTBFCmdHandler':无法将'this Housekeeping :: TBFInstallation'中的'this'指针转换为'Housekeeping :: TBFInstallation'
这里出了什么问题?
所以我们有(伪代码):
class A
{
A(shared_ptr parent){}
}
class B
{
A *a;
B()
{
a = new A(boost::shared_ptr(this));
}
}
Run Code Online (Sandbox Code Playgroud)
是否可以在C++中使用shared_ptr进行此类操作以及如何在真正的C++代码中执行此操作?
我有以下代码:
namespace {
class CatBondEvent
{
public:
CatBondEvent(Date date) : date(date){}
virtual ~CatBondEvent(){}
Date date;
bool operator< (CatBondEvent &other) { return date<other.date;}
};
class CatEvent : CatBondEvent
{
public:
Real loss;
CatEvent(Date date, Real loss) : CatBondEvent(date), loss(loss) {}
};
class CollateralCoupon : CatBondEvent
{
public:
Real unitAmount;
CollateralCoupon(Date date, Real unitAmount) : CatBondEvent(date), unitAmount(unitAmount) {}
};
class CatBondCoupon : CatBondEvent
{
public:
CatBondCoupon(Date date) : CatBondEvent(date) {}
};
void fillEvents(std::vector<boost::shared_ptr<CatBondEvent> > &events,
Date startDate,
Date endDate,
boost::shared_ptr<std::vector<std::pair<Date, Real> > …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <memory>
using namespace std;
template<typename U> class A;
template<typename U>
class B
{
private:
shared_ptr<const A<U>> _a;
public:
B (shared_ptr<const A<U>> __a) {
_a = __a;
}
};
template<typename U>
class A
{
public:
B<U> foo () const {
return { make_shared<const A<U>> (this) };
}
};
int
main ()
{
A<int> a;
B<int> b = a.foo ();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
G ++ 4.8.0和Clang 3.3svn报告该类A没有复制或移动构造函数.例如,G ++打印以下消息:
/home/alessio/Programmi/GCC/include/c++/4.8.0/ext/new_allocator.h:120:4: error: no matching function for call to ‘A<int>::A(const A<int>* …Run Code Online (Sandbox Code Playgroud) 专业人士在那里
这是我第一次向董事会发布问题,这在过去对我有很大帮助。我仍然是编程新手,并且在过去的几个月里一直在尝试学习有关编程和软件开发的很多知识。
我最近解决了“ c ++ 11-Smart Pointers”主题,并决定重写几周前学到的所有设计模式,包括使用shared_ptr和weak_ptr进行资源管理的“观察者模式”。
当我尝试编写观察者模式的简单实现时(例如在Head First的设计模式或E.Gamma的设计模式中),同时为可观察的主题和观察者使用接口类,并且为每个观察者分别使用一个具体的类,我收到了来自Visual Studio的各种错误消息。我只是不了解如何在此模式下组合shared_ptr和weak_ptr,这可能是由于我仍然不完全了解两者的组合。
不幸的是,即使是在Internet上进行彻底的搜索也没有给我提供一个简单的“使用智能指针的观察者模式”示例,它可以为我提供指导。
因此,我想知道,你们中的一个人是否会知道在哪里可以找到这种简单的实现方式,或者愿意为我提供自己的一个。两者都将非常有帮助:-)
非常感谢
编码器
class GameBoy{
public:
GameBoy()
:cart(new Cartridge()),
mmu(new MMU(cart)),
cpu(new CPU(mmu))
{}
void step(); //steps through one instruction
const CPU::State &getCPUState() const noexcept;
void loadROM(std::string fileName);
private:
std::shared_ptr<Cartridge> cart;
std::shared_ptr<MMU> mmu;
std::shared_ptr<CPU> cpu;
};
Run Code Online (Sandbox Code Playgroud)
我正在编写一个Game Boy模拟器,这个类是Qt GUI将与之交互的.也就是说,对于GUI中绘制的每个帧,它将调用GameBoy :: step().显然,由于我还没有开始在GPU上工作,所以还没有完成.
我目前正在质疑这个设计,因为这3个类(Cartridge,CPU,MMU)只会被实例化一次.单身是一个比这更好的设计,还是这个当前的设计是最好的?
如果这个最好,我应该继续使用shared_ptr吗?我已经读到应该谨慎使用它,但是在这里,它似乎有意义,因为所有3个类都相互依赖.
非常感谢你!
编辑:人们问我为什么要使用指针.从初始列表中可以看出,MMU需要对Cartridge的引用,CPU需要对MMU的引用.
Cartridge将由GameBoy :: loadROM()和大多数MMU方法使用.我同意MMU可能不需要由GameBoy拥有,但它将在CPU和GPU之间共享(我认为).
在下面的示例中,是否有更好的替代购物车共享指针?
类之间的示例用法:
byte MMU::readByte(word address) {
....
value = cart->readROM(address);
....
}
void GameBoy::loadROM(std::string fileName){
cart->loadROM(fileName);
}
Run Code Online (Sandbox Code Playgroud)
编辑2:谢谢大家!我有一个最后的问题.如果GPU和CPU都使用MMU并且GameBoy同时使用GPU和CPU,那么哪种设计更好:
class GameBoy{
public:
GameBoy()
: CPU(&mmu), gpu(&mmu)
...
private:
MMU mmu; //never used by GameBoy
CPU cpu; …Run Code Online (Sandbox Code Playgroud) 我想通过智能指针初始化两个类的实例:
std::shared_ptr< myQueue > _pA ;
std::shared_ptr< myQueue > _pB ;
_pA.reset( new myQueue() ) ;
_pB.reset( new myQueue() ) ;
Run Code Online (Sandbox Code Playgroud)
我是否已使用上述复位函数初始化了两个不同的myQueues或同一对象上的两个指针?
因此,我想对类中的向量进行一些错误检查,以便在将新项添加到向量之前查看该项是否已存在.
ClassA cpp
void ClassA::func(std::shared_ptr<ClassB> new_item)
{
for(auto items : vector_)
{
if(items = new_item)
{
return;
}
vector_.push_back(new_item);
}
}
Run Code Online (Sandbox Code Playgroud)
vector_是成员类成员std :: vector.使用此当前实现,所有new_item都被忽略,即使它不是重复的.我知道'if(items = new_item)'是有问题的一行,但我不知道为什么.
我目前有以下结构
class A
class B : public A
class C : public A
我在A和中定义了虚方法,B并且C正在重写它们。该方法是那种
bool C::CheckCollision(shared_ptr<B> box);
bool B::CheckCollision(shared_ptr<C> triangle);
我还有一个向量,shared_ptr<A>其中存储了所有游戏对象。问题是我无法执行以下操作
for (int i = 0; i < objects.size(); i++)
{
for (int j=i; j < objects.size(); j++
{
objects[i]->CheckCollision(objects[j]);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误消息,说参数列表与重载函数不匹配。在尝试传递shared_ptr<A>期望值时,shared_ptr<B>还是很有道理的shared_ptr<C>,但是如何解决这个问题呢?还有另一种方法吗?
我正在经历shared_ptr并遇到了这个问题。
class A
{
public:
A() { cout << "In constructor" << endl; }
~A() { cout << "Destructor" << endl; }
void fun() { cout << "In fun... " << endl; }
};
int main()
{
shared_ptr<A> a;
a->fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是-有趣的是...
我想了解这是如何给上面的输出。
在进一步的实验中,如果存在成员变量并在该函数中使用它,则会抛出SIGSEGV。
class A
{
public:
A() { cout << "In constructor" << endl; }
~A() { cout << "Destructor" << endl; }
void fun() { a = 5 ; cout << "In fun... " << …Run Code Online (Sandbox Code Playgroud) c++ ×10
shared-ptr ×10
c++11 ×3
boost ×2
class ×1
inheritance ×1
polymorphism ×1
raii ×1
templates ×1
vector ×1
weak-ptr ×1