我得到例外:当我这样做时std :: bad_weak_ptr-> shared_from_this()
template<typename TChar>
class painter_record_t
{
.......
private:
std::shared_ptr<i_painter_t> _owner;
}
我想设置&#34;问题&#34;构造函数中的对象:
template<typename TChar>
class stream_record_t : public painter_record_t<TChar>
{
public:
stream_record_t(std::shared_ptr<i_painter_t> owner) : painter_record_t(owner)
{
//...
}
}
我有基类:
class i_painter_t
{
public:
virtual std::unique_ptr<painter_record_t<char>> get_entry() = 0;
}
派生类,我希望将智能指针发送到基本抽象类,但得到异常:
class painter_t : public i_painter_t, public std::enable_shared_from_this<painter_t>
{
public:
std::unique_ptr<painter_record_t<char>> get_entry()
{
return std::unique_ptr<painter_record_t<char>>(new stream_record_t<char>(static_cast< std::shared_ptr<i_painter_t> >(this->shared_from_this())));
}
}
我也试过这个,但也有同样的问题:
class painter_t : public i_painter_t, public std::enable_shared_from_this<painter_t>
{
public:
std::unique_ptr<painter_record_t<char>> get_entry()
{
return std::unique_ptr<painter_record_t<char>>(new stream_record_t<char>(this->shared_from_this()));
}
}
答案 0 :(得分:3)
要使enable_shared_from_this
生效,您需要在调用#include "tools.h"
static string vertexSource = getFile("D:/test.vsh");
static string fragmentSource = getFile("D:/test.fsh");
int main(int argc, char *argv[])
{
//SDL///////////////////////////////////////////////
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
SDL_Window *window = SDL_CreateWindow("Test", 50, 50, 1600, 900, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext context = SDL_GL_CreateContext(window);
//GLEW///////////////////////////////////////////////
glewExperimental = GL_TRUE;
GLint result = glewInit();
if (result != GLEW_OK)
{
cout << glewGetErrorString(result) << "\n";
cout << "GLEW initialization failed.\n";
}
//OpenGL///////////////////////////////////////////////
glEnable(GL_DEPTH);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
//Object///////////////////////////////////////////
//Vertex Array
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//Vertex Buffer
GLuint vbo;
glGenBuffers(1, &vbo);
GLfloat vertices[] = {
-0.5f, 0.5f,
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
};
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//Element Buffer
GLuint ebo;
glGenBuffers(1, &ebo);
GLuint elements[] = {
0, 1, 2,
2, 3, 0
};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
//Shader
GLuint shaderProgram = createShader(vertexSource, fragmentSource);
glUseProgram(shaderProgram);
GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
//Loop///////////////////////////////////////////////
float x = 0.0f;
int step = 0;
int steps = 4;
SDL_Event eve;
while (true)
{
//Test stuff
x += 0.005f;
GLfloat newVerts[] = {
-0.5f + x, 0.5f,
0.5f + x, 0.5f,
0.5f + x, -0.5f,
-0.5f + x, -0.5f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(newVerts), newVerts, GL_STATIC_DRAW);
//Normal stuff
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//Accumulation///////////////////////
if (step == 0)
{
glAccum(GL_LOAD, 1.0f / steps);
}
else
{
glAccum(GL_ACCUM, 1.0f / steps);
}
step++;
if (step < steps)
{
continue;
}
step = 0;
glAccum(GL_RETURN, 1.0f);
//Accumulation///////////////////////
SDL_GL_SwapWindow(window);
SDL_PollEvent(&eve);
}
}
之前将指针存储在this
中的std::shared_ptr
。
请注意,在对象t上调用shared_from_this之前,必须有一个拥有t的std :: shared_ptr。
您可能希望将shared_from_this()
构造函数设为私有,并公开工厂方法,以确保创建的每个painter_t
都由painter_t
管理:
shared_ptr