标签: auto-ptr

auto_ptr内容上的三元运算符无法正常工作

我将auto_ptr初始化为NULL,稍后在游戏中我需要知道它是否为NULL以返回它或新副本.

我试过这个

auto_ptr<RequestContext> ret = (mReqContext.get() != 0) ? mReqContext : new RequestContext();
Run Code Online (Sandbox Code Playgroud)

和其他几个类似的东西一样,但是g ++试图调用auto_ptrs不存在的运算符?(三元运算符)而不是使用RequestContext*进行三元比较.

即使我施展它也行不通.

任何提示?

编辑等于不平等

c++ stl auto-ptr

3
推荐指数
1
解决办法
475
查看次数

为什么auto_ptr的接口指定了两个类似复制构造函数的构造函数

我正在浏览此链接auto_ptr上的auto_ptr文档 有些东西我无法完全理解为什么要这样做.在接口部分中,它的复制构造函数有两个声明

1)

auto_ptr(auto_ptr<X>&) throw (); 
Run Code Online (Sandbox Code Playgroud)

2)

template <class Y> 
     auto_ptr(auto_ptr<Y>&) throw(); 
Run Code Online (Sandbox Code Playgroud)

这是为了什么目的.

c++ stl auto-ptr

3
推荐指数
2
解决办法
245
查看次数

Qt和auto_ptr

我刚刚发现了auto_ptr的概念,我很喜欢它!由于Qt经常需要QList或QVector <(某些QObject或QWidget)*>,是否应该避免使用auto_ptr的具体原因.如果我是对的,它允许你替换它:

std::vector<MyClass*> vec;
/* add several elements to the vector and do stuff with them */
for(size_t i=0; i<vec.length(); ++i)
{
    delete vec[i];
}
vec.clear();
Run Code Online (Sandbox Code Playgroud)

用更短的东西(即没有清理)

std::vector<auto_ptr<MyClass>> vec;
/* add several elements to the vector and do stuff with them */
// no need for the delete loop
Run Code Online (Sandbox Code Playgroud)

... Qt仍然可以使用auto_ptr进行共享内存魔术吗?父子自动记忆管理是否仍然透明地运作?谢谢

c++ qt auto-ptr

3
推荐指数
1
解决办法
4399
查看次数

auto_ptr和containers - C++

我目前正在研究2D游戏引擎,我已经阅读了auto_ptr以及你应该如何将它们放在标准容器中.

我的引擎有这样的结构:

StateManager - 有很多 - > State's.

在引擎外部的main中创建和分配状态.我希望引擎存储所有状态的列表/向量,以便我可以在命令之间进行更改.

例如: __CODE__

假设我有下表:

SomeState *test = new SomeState();
StateManager->registerState(test);
Run Code Online (Sandbox Code Playgroud)

(这不是我正在做的事情,但它通过我的设置抽象出很多其他并发症)

假设我想得到一张桌子,告诉我有多少游戏适合不同年龄:

std::auto_ptr<SomeState> test(new SomeState());
StateManager->registerState(test.get());

// Inside StateManager State *activeState; // State manager then maintains a vector std::vector<State*> stateList; // and upon registerState it adds the pointer to the vector void registerState(State *state) { stateList.push_back(test); }

Run Code Online (Sandbox Code Playgroud)

我当然可以获得单一年龄的价值:

SomeState *test = new SomeState();
StateManager->registerState(test);
Run Code Online (Sandbox Code Playgroud)

而且我知道如何获得具有给定项目的项目数量 __CODE__

std::auto_ptr<SomeState> test(new SomeState());
StateManager->registerState(test.get());

// Inside StateManager State *activeState; // State manager …

c++ auto-ptr

3
推荐指数
1
解决办法
617
查看次数

引用容器对象的子集

我有一个关于引用集合子集的快速问题.考虑我有一个对象矢量.现在我想创建另一个向量,它是此向量的子集,我不想创建对象子集的副本.

我正在考虑的方法之一是创建一个vector<auto_ptr<MyClass> >.这是一个好方法吗?如果您认为在这种情况下任何其他容器或习语或模式会有所帮助,请建议.谢谢

c++ auto-ptr

3
推荐指数
1
解决办法
1058
查看次数

使用std :: auto_ptr的意义

感觉是auto_ptr什么?看看这段代码:

#include <iostream>
#include <memory>

class A
{
public:
    ~A()
    {
        std::cout << "DEST";
    };
};

void func(A* pa)
{
    std::cout << "A pointer";
}

void test()
{
    A a;
    std::auto_ptr<A> pA(new A);
    //func(pA);  //compiler error here cannot convert parameter 1 from 'std::auto_ptr<_Ty>' to 'A *' 
    std::cout << "end";
}

int main(int argc, char* argv[])
{
    test();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用它有auto_ptr什么意义?

  • 它在超出范围时调用类析构函数作为普通类初始化变量(a).
  • 我不能将此指针传递给具有类指针(func)的函数
  • 我不能使用指针auto_ptrA[]char[]因为auto_ptr的电话删除不delete[].

唯一的想法是我不必编写删除,但是当我超出范围时,如果它将被销毁,那么指针的意义是什么.我使用指针来控制变量的实时.

普通变量初始化是在堆栈上的指针和堆上的指针,但告诉我使用auto_ptr正常指针的意义是什么?

c++ pointers smart-pointers auto-ptr

3
推荐指数
1
解决办法
4168
查看次数

关于auto_ptr的问题

假设,我们有以下代码

auto_ptr<T> source() 
{
  return auto_ptr<T>( new T(1) );
}
void sink( auto_ptr<T> pt ) { }
void f()
{
  auto_ptr<T> a( source() );
  sink( source() );
  sink( auto_ptr<T>( new T(1) ) );
  vector< auto_ptr<T> > v;
  v.push_back( auto_ptr<T>( new T(3) ) );
  v.push_back( auto_ptr<T>( new T(4) ) );
  v.push_back( auto_ptr<T>( new T(1) ) );
  v.push_back( a );
  v.push_back( auto_ptr<T>( new T(2) ) );
  sort( v.begin(), v.end() );
  cout << a->Value();
}
class C
{
public:    /*...*/
protected: /*...*/
private: …
Run Code Online (Sandbox Code Playgroud)

c++ auto-ptr

3
推荐指数
1
解决办法
368
查看次数

函数中的C++ auto_ptr(作为参数和返回值)

我试图在我的代码中使用auto_ptr,但显然出了问题.

auto_ptr<ClassType> Class(s.Build(aFilename)); //Instantiation of the Class object
int vM = s.GetM(Class);
int vS = s.Draw(Class);
Run Code Online (Sandbox Code Playgroud)

奇怪的是,在实例化Class之后,Class对象存在,因此通过调用s.GetModelMean(Class),Class不为空.但退出函数GetM后,Class为空,因此不再可用.调用Draw函数时发生崩溃.

我按以下方式声明了这些函数:

int GetM(auto_ptr<ClassType> aM); 
Run Code Online (Sandbox Code Playgroud)

似乎班级被摧毁了,但我不明白为什么......

c++ auto-ptr

3
推荐指数
1
解决办法
4998
查看次数

使用指向派生类的指针初始化auto_ptr是否安全?

假设我有一个基类和一个派生类:

class Base
{
    public:
        virtual ~Base() {}
        virtual void DoSomething() = 0;
};


class Child : public Base
{
    public:
        virtual void DoSomething()
        {
            // Do Something
        }
};
Run Code Online (Sandbox Code Playgroud)

使用指向派生类实例的指针初始化基类类型的std :: auto_ptr是否安全?IE会像这样创建一个对象:

std::auto_ptr<Base> myObject(new Derived());
Run Code Online (Sandbox Code Playgroud)

正确调用派生类的析构函数而不是基类而不泄漏内存?

c++ smart-pointers auto-ptr

3
推荐指数
1
解决办法
749
查看次数

auto_ptr无法正常工作 - 编译错误


大规模编辑:

在juanchopanza建议之后,我设法得到了这个最小的例子:

#include <memory>

struct a{
    int b;
};

int main()
{

    typedef std::auto_ptr<a> ArgAutoPtr;

    ArgAutoPtr floatingArg;

    floatingArg = ArgAutoPtr( new a );

}
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

no match for 'operator=' in 'm_floatingArg = std::auto_ptr<a>(((a*)operator new(4u)))'
Run Code Online (Sandbox Code Playgroud)

QNX 6.4.1与GCC 4.3.3

编辑

我设法像这样编译它.这是否按预期工作或将产生......无论邪恶auto_ptr产生什么?

ArgAutoPtr floatingArg2 = ArgAutoPtr( new a );
floatingArg = floatingArg2;
Run Code Online (Sandbox Code Playgroud)

c++ auto-ptr qnx

3
推荐指数
1
解决办法
252
查看次数

标签 统计

auto-ptr ×10

c++ ×10

smart-pointers ×2

stl ×2

pointers ×1

qnx ×1

qt ×1