标签: auto-ptr

关于auto_ptr :: reset的问题

请任何人从C++参考网站解释这段代码:

#include <iostream>
#include <memory>
using namespace std;

int main () {
  auto_ptr<int> p;

  p.reset (new int);
  *p=5;
  cout << *p << endl;

  p.reset (new int);
  *p=10;
  cout << *p << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers smart-pointers auto-ptr

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

指针和智能指针之间的区别

你能告诉我这段代码有什么问题吗?我在接受采访时被问到这一点,我不确定它有什么问题

tClass是一个带有printSomething方法的测试类,可以打印tClass的成员.

tClass * A = new tClass();
f(A);
A->printSomething();

auto_ptr<tClass> * B = new tClass();
f(B);
B-> printSomething();
Run Code Online (Sandbox Code Playgroud)

或者这是一个棘手的问题.

c++ auto-ptr

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

将原始指针分配给auto_ptr

我正在阅读一篇关于有效使用的文章auto_ptr.在那里,以下代码被建议为正确的代码段:

// Example 10(c): Correct (finally!)
//
auto_ptr<String> f()
{
  auto_ptr<String> result = new String;
  *result = "some value";
  cout << "some output";
  return result;  // rely on transfer of ownership;
                  // this can't throw
}
Run Code Online (Sandbox Code Playgroud)

但据我所知,赋值运算符auto_ptr只接受另一个auto_ptr作为rhs- 以避免意外误用.那么,下面的一行是文章中的拼写错误,还是它真的能够起作用?

auto_ptr<String> result = new String;
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers auto-ptr

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

使用C++将指针传递给auto_ptr

我有一个功能,这样做:

static MyClass* MyFunction(myparams)
{
    return new MyClass(myparams)
}
Run Code Online (Sandbox Code Playgroud)

我可以在另一个具有以下签名的函数内调用此函数:

void MyFunction2(std::auto_ptr<MyClass> myparam)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这样做时,我有一个编译器错误:

无法将第一个参数从MyClass*转换为std :: auto_ptr <_Ty>

为什么?感谢您的任何帮助

编辑1 根据要求,myparams类型是正常的,但也有一个T param,因为该函数在模板类中

c++ auto-ptr

0
推荐指数
1
解决办法
1886
查看次数

将auto_ptr <Base>转换为auto_ptr <Derived>

请帮我理解以下问题.

看下面的代码示例:

#include <iostream>

class Shape {
public:
  virtual wchar_t *GetName() { return L"Shape"; }
};
class Circle: public Shape {
public:
  wchar_t *GetName() { return L"Circle"; }
  double GetRadius() { return 100.; }
};

int wmain() {
  using namespace std;

  auto_ptr<Shape> aS;
  auto_ptr<Circle> aC(new Circle);

  aS = aC;
  wcout << aS->GetName() << L'\t' << static_cast<auto_ptr<Circle>>(aS)->GetRadius() << endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么我不被允许这样做:

static_cast<auto_ptr<Circle>>(aS)->GetRadius()
Run Code Online (Sandbox Code Playgroud)

编译器(MSVCPP 11):

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xmemory(911): error C2440: 'initializing' : cannot convert from 'Shape *' …
Run Code Online (Sandbox Code Playgroud)

c++ casting auto-ptr

0
推荐指数
1
解决办法
3314
查看次数

构造函数采用auto_ptr

我想编写一个带有构造函数的C++类,该构造函数将其auto_ptr作为参数,以便我可以将类实例从auto_ptrs 初始化为另一个实例:

#include <memory>

class A
{
public:
  A() {}
  A(std::auto_ptr<A> other) {}
};

std::auto_ptr<A> create()
{
  return std::auto_ptr<A>(new A());
}

void foo()
{
  A x = create();
  // A y ( create() );    // works
}
Run Code Online (Sandbox Code Playgroud)

使用g++ -c test.cppgcc 4.6 编译此代码会产生以下错误消息:

test.cpp: In function ‘void foo()’:
test.cpp:17:16: error: no matching function for call to ‘std::auto_ptr<A>::auto_ptr(std::auto_ptr<A>)’
test.cpp:17:16: note: candidates are:
/usr/include/c++/4.6/backward/auto_ptr.h:260:7: note: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = A]
/usr/include/c++/4.6/backward/auto_ptr.h:260:7: note:   no known conversion for argument …
Run Code Online (Sandbox Code Playgroud)

c++ initialization auto-ptr

0
推荐指数
1
解决办法
470
查看次数

std :: auto_ptr在按值传递给函数后变为无效

我有以下示例代码:

#include <iostream>
#include <auto_ptr.h>

class A
{
public:
    A(){ std::cout << "A ctor" << std::endl;}
    ~A() {std::cout << "A dtor" << std::endl;}
    void bar(){std::cout << "bar()" << std::endl;}
};

void foo(std::auto_ptr<A> a)
{
    std::cout << "foo()" << std::endl ;
}

int main()
{
    std::auto_ptr<A> a(new A());
    a->bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

A ctor
bar()
A dtor
Run Code Online (Sandbox Code Playgroud)

现在,如果我打电话foo(a),a将在致电之前被破坏bar():

int main()
{
    std::auto_ptr<A> a(new A());
    foo(a);
    a->bar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

A ctor
foo()
A …
Run Code Online (Sandbox Code Playgroud)

c++ auto-ptr pass-by-reference

0
推荐指数
1
解决办法
156
查看次数

如何访问std :: auto_ptr指向的对象

在我的TicTacToe游戏中,我遇到了虚拟功能的问题.以下代码在Dev C++中引发错误:"class std :: auto_ptr"没有名为'makeAMove'的成员.

根据错误,问题与makeAMove函数有关,但我看不出有什么问题.另外,我应该提一下,我正在使用已弃用的auto_ptr函数而不是unique_ptr,因为显然教师对我的代码进行分级的辅助工具没有符合C++ 11的编译器.

现在playGame和makeAMove函数都没有做任何事情,但我想在继续之前找出导致错误的原因.

谢谢你的任何建议.

这是相关的代码:

Game.h(充当游戏的控制器)

#include "Player.h"
#include "AIPlayer.h"
#include "HumanPlayer.h"
#include <memory>

class Game 
{
    public: 
        Game(unsigned int players)
        {
            if (players == 1)
            {
                player1.reset (new HumanPlayer()); 
                player2.reset (new AIPlayer());
            }
            else 
            {
                player1.reset (new HumanPlayer());
                player2.reset (new HumanPlayer());
            }
        }

        void playGame()
        {
            player1.makeAMove();
        }

    private:
        std::auto_ptr<Player> player1; // pointer to a Player object (C++11 has more secure unique_ptr)
        std::auto_ptr<Player> player2; // pointer to a Player object
};
Run Code Online (Sandbox Code Playgroud)

Player.h(HumanPlayer.h和AIPlayer.h的基类)

class Player …
Run Code Online (Sandbox Code Playgroud)

c++ auto-ptr

0
推荐指数
1
解决办法
270
查看次数

为什么unique_ptr可以工作但auto_ptr不能与STL一起使用

我在这些问题上提到了很多StackOverflow链接,其中auto_ptr不能与STL很好地配合 的原因std::auto_ptr<>不能满足可复制构造和可分配的要求(因为auto_ptr有一个伪造的复制构造函数,它基本上可以转移所有权)。

但是,甚至unique_ptr没有复制ctor和赋值运算符(已禁用),那么如何满足可复制构造和可赋值的要求呢?

c++ stl auto-ptr copy-constructor unique-ptr

0
推荐指数
1
解决办法
86
查看次数