考虑两个类
class A{
public:
int i;
A(){}
explicit A(const int ii):i(ii){}
virtual ~A(){
cout<<"~A - "<< i <<endl;
}
virtual void inc(){
i++;
cout<<"i: "<<i<<endl;
}
};
class B: public A{
public:
i
下面的代码导致运行时错误。
每个shared_ptr拥有相同的内存,但每个one的计数仍然是1。
因此,每个共享指针都是不同的,所以当它们超出作用域时,它们会尝试释放块,这会导致堆损坏。我的问题是如何避免这种情况?
我只想像这样添加声明
shared_ptr<int> x(p);
是没有商量余地的我必须声明。
#include <iostream>
#include <memory>
using namespace std;
int main ()
{
int* p = new int (10);
shared_ptr<int> a (p
(编辑)环境:
plee@sos-build:/usr/local/include/boost$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.10
Release: 11.10
Codename: oneiric
plee@sos-build:/usr/local/include/boost$ uname -a
Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7
我已经读了一些关于智能指针的指南,我想我知道它的诀窍(不是双关语),但仍然有一些东西困扰着我。
智能指针旨在使用new处理指向堆上分配的数据的指针,一旦智能指针超出作用域,其析构函数将释放指针所指向的信息。但是,假设我犯了一个错误,让智能指针指向堆栈上分配的对象,而不是堆上的对象。在这种情况下,我会得到一个严重的分段错误,因为我会尝试在堆栈对象上使用delete。
为了说明这个问题,这里有两个代码片段。在本演示中,我使用了智能指针的实现。首先,使用智能指针的正确方式
int main(void)
{
int my_num = 17;
int *p = new int(my_num);
也就是说,如果我不使用复制构造函数、赋值操作符或移动构造函数等。
int* number = new int();
auto ptr1 = std::shared_ptr<int>( number );
auto ptr2 = std::shared_ptr<int>( number );
会有两个强有力的推荐信吗?
我有自己的智能指针实现,它使用引用计数作为所有权机制(注意:我已经测试过它,它没有bug)。
下面是我的代码流。
创建对象并创建指向对象调用函数的智能指针,该函数具有以下定义:void Func(void* param) (注意:该函数运行在不同的线程上下文中)。
- In parameter evaluation: It creates another smart pointer using copy constructor
- Both smart pointer has same pointee and ref count of pointee is 2.
- Before funct
我有一个包含指针的类。我希望防止类的用户访问指针的地址(这样他们就不能将它设置为另一个地址、删除它或其他什么-不是)。但是,我希望用户能够修改指针数据(或者成员数据(如果不是POD)以及调用指针的方法(假设它有)。
是否有任何方法返回指针或引用,允许您更改指针所指向的数据而不更改指针值本身?
所以:
class A
{
public:
int Value;
void Method();
};
class Wrapper
{
public:
Wrapper()
{
Pointer = new A;
}
// Method that
所以,我有一个函数,它应该返回未知类型的对象,比如C或B。我决定添加一个公共基类A并返回它是一个很好的解决方案。我已经在A中添加了int type字段,这样我们就可以区分B对象和C对象。
例如,函数本身看起来像这样:
A f(int x) {
if (x==0)
return B();
else
return C();
}
作为它的用法的一个例子:
A a=f(0);
B b; C c;
if (a.type==OBJ_TYPE_B)
b=a;
else
c=a;
问题很明显:不允许从基类到派生类的显式转换。我看到了两种解决方案:指针和引用。指针太像C语言了,我
如何将智能ptr传递给引用指针作为参数的函数?
smart_ptr<T> val; // I have this smart pointer
// And I want to pass it to this function, so that this function will fill the smart pointer with proper value
void Foo(T*& sth)
{
sth = memoryAddress;
}
编辑现在我明白了。谢谢各位给出的答案!
考虑以下代码:
class Base{
};
class Derived : public Base{
};
int main(int argc, char **argv)
{
std::unique_ptr<Base> b(new Derived());//1 // b is Base object but holds Derived pointer
Base *b1 = new Derived();//Base obj points to Derived //3
//std::unique_ptr<Base*>
代码:
class Base {
...
};
class Derived : public Base {
void OnlyOwnedByDerived{
...
}
};
问题是: 1.如果我使用基类的智能指针来引用派生的指针,那么我这么做的原因是我希望获得只适合虚拟函数的动态绑定的好处。但是,如果我想使用只属于派生类的函数,我应该做什么?不同类的智能指针之间的static_cast给我一个错误.
我能想到的最直接的方法是使用原始指针而不是智能指针.
我想要一个静态2D数组,它接受一个接口类指针。
使用原始指针可以很好地工作,但是我想使用智能指针,并且只将原始指针作为参数传递。
如何在不将args更改为智能指针的情况下使代码工作?
class Base {};
class Child : public Base {};
void Foo(Base* array[5][5])
{
// Stuff
}
void OtherFoo(std::unique_ptr<Base> array[5][5])
{
// Stuff
}
int main()
{
std::unique_ptr<Base>