C++复制指针

发布于 2024-11-06 12:10:11 字数 309 浏览 5 评论 0 原文

您好,我想知道如何将 C++ 中的二维数组指针的内容复制到另一个位置并设置另一个指向它的指针,以便当我对复制的指针进行更改时原始数据不会发生任何变化?

基本上它是一个指向棋盘上棋子的数组指针。所以它就像Piece * oldpointer = board[8][8]。现在我想复制这个指针中的所有内容,包括 Pieces 头文件中的 getvalue()、getcolor() 等方法到另一个位置,并设置一个指向它的指针,以便我可以执行操作在那里进行测试而不影响原始数据?我在某处读到我必须使用 allocate() 但我不确定。请帮忙

Hi i would like to know how i could copy the contents of a 2d Array pointer in C++ to another location and set another pointer to it so that when i make changes on the copied pointer nothing happens to the original data?

Basically its an array pointer to pieces on a chessboard. so it goes like Piece * oldpointer = board[8][8]. now i want to copy all the contents in this pointer including methods like getvalue(), getcolor() etc which are in the Pieces header file to another location and set a pointer to it so i can do operations there and test it without it having to affect this orginal data? I read somewhere i had to use allocate() but im not sure. please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌吟 2024-11-13 12:10:12

在 C++ 中,您可以按如下方式定义 2D 数组类型(您需要现代 C++ 编译器):

#include <array>
typedef std::array<std::array<Piece, 8>, 8> board_t;

如果您的编译器不支持 std::array,您可以使用 boost::array 代替:

#include <boost/array.hpp>
typedef boost::array<boost::array<Piece, 8>, 8> board_t;

现在您可以使用上面的类型。正如我所看到的,您需要复制指针指向的对象:

board_t* oldpointer = new board_t;

// do some with oldpointer

// now make a copy of the instance of the object oldpointer points to
// using copy-constructor
board_t* newpointer = new board_t( *oldpointer );
// now newpointer points to the newly created independent copy

// do more

// clean up
delete oldpointer;

// do more with newpointer

// clean up
delete newpointer;

In C++ you could define 2D array type as follows (you need modern C++ compiler):

#include <array>
typedef std::array<std::array<Piece, 8>, 8> board_t;

If your compiler doesn't support std::array you can use boost::array instead:

#include <boost/array.hpp>
typedef boost::array<boost::array<Piece, 8>, 8> board_t;

Now you can use the type above. As I can see you need to copy the object to which the pointer points:

board_t* oldpointer = new board_t;

// do some with oldpointer

// now make a copy of the instance of the object oldpointer points to
// using copy-constructor
board_t* newpointer = new board_t( *oldpointer );
// now newpointer points to the newly created independent copy

// do more

// clean up
delete oldpointer;

// do more with newpointer

// clean up
delete newpointer;
陈年往事 2024-11-13 12:10:12

既然您使用的是 C++,为什么不为 Piece 类定义一个复制构造函数呢?那么

Piece copied_piece(*board[8][8]);

如果你的类是 POD,你甚至应该能够使用默认的复制构造函数。

Since you're using C++, why not define a copy constructor for your Piece class? Then just

Piece copied_piece(*board[8][8]);

If your class is POD, you should even be able to get by with the default copy constructor.

你如我软肋 2024-11-13 12:10:12

您可以通过在目标分配内存然后 memcopy 来进行复制

dest_pointer = (<<my type>>*) malloc(sizeof(<<my type>>);
memcpy(dest_pointer, src_pointer, sizeof(<<my type>>);

顺便说一句,这些方法永远不会被复制。它们不属于某个对象。

You can copy by allocating memory at the destination and then memcopy

dest_pointer = (<<my type>>*) malloc(sizeof(<<my type>>);
memcpy(dest_pointer, src_pointer, sizeof(<<my type>>);

Btw, the methods are never copied. they don't belong to an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文