[英]Can't get the same value of a pointer using the value of a pointer to pointer?
The program crash.程序崩溃。 I'm trying to implement a
Brick ** create_bricks()
function that return a pointer to pointer of Brick (because I want to create an array of Bricks and return the position of the first).我正在尝试实现一个
Brick ** create_bricks()
function,它返回一个指向 Brick 指针的指针(因为我想创建一个 Bricks 数组并返回第一个的 position)。 I'm pulling my hair out and the last thing I do after several try and fails, erros and research, was to simplify my code and check value of the pointers line by line.我把头发拉出来,在几次尝试失败、错误和研究之后,我做的最后一件事是简化我的代码并逐行检查指针的值。 When I dereference
the_brick
as *the_brick
in the draw function game loop it doesn't get the same value as the one that new Brick()
assign to ptr_brick
inside create_bricks()
function.当我在 draw function 游戏循环中将
the_brick
取消引用为*the_brick
时,它不会获得与new Brick()
在create_bricks()
function 中分配给ptr_brick
的值相同的值。 I'm using SFML, but the problems is about pointers.我正在使用 SFML,但问题在于指针。
Note : If I return a Brick *
from create_bricks
with proper modifications it works fine (and draw the brick), but I need to create multiple bricks, no only one.注意:如果我从
create_bricks
返回Brick *
并进行适当修改,它可以正常工作(并绘制砖),但我需要创建多个砖,而不仅仅是一个。
#include "Brick.h"
int main()
{
...
while (window.isOpen())
{
Brick ** ptr_bricks = create_bricks();
Brick * ptr_brick = *ptr_bricks;
window.draw(*the_brick);
}
return 0;
}
Brick ** create_bricks()
{
Brick * ptr_brick = new Brick();
ptrBrick->setPosition(150, 20);
return &ptrBrick;
}
#include "Brick.h"
Brick::Brick()
{
LOG(INFO) << "Brick constructor";
setPosition(10, 10);
setSize(sf::Vector2f(100, 20));
setFillColor(sf::Color::Green);
setOutlineThickness(1);
}
Brick::~Brick()
{
LOG(INFO) << "Brick destructor";
//dtor
}
Thanks谢谢
The problem is the return
statement in the create_bricks
function:问题是
create_bricks
function中的return
语句:
return &ptrBrick;
Here you return a pointer to the local variable ptrBrick
.在这里,您返回一个指向局部变量
ptrBrick
的指针。 The life-time of this variable will end when the function ends, and any pointer you have to it will become invalid as soon as the function ends.此变量的生命周期将在 function 结束时结束,并且您拥有的任何指向它的指针将在 function 结束后立即失效。
The natural C++ solution to return an "array" from a function is to return a std::vector
instead:从 function 返回“数组”的自然 C++ 解决方案是返回
std::vector
:
std::vector<Brick*> create_bricks()
{
Brick* brick = new Brick;
brick->setPosition(150, 20);
return { brick };
}
If Brick
is not a polymorphic class you don't even need to use a vector of pointer, but a vector of plain Brick
objects ( std::vector<Brick>
).如果
Brick
不是多态 class 您甚至不需要使用指针向量,而是使用普通Brick
对象的向量( std::vector<Brick>
)。
If you persist in using pointers you must allocate an array of pointers to Brick
, which means new Brick*[number_of_bricks]
(or new Brick[number_of_bricks]
if polymorphism isn't needed).如果您坚持使用指针,则必须分配一个指向
Brick
的指针数组,这意味着new Brick*[number_of_bricks]
(如果不需要多态性,则为new Brick[number_of_bricks]
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.