我有两个资源管理类DeviceContext和OpenGLContext都是class DisplayOpenGL的成员。资源生存期与DisplayOpenGL相关联。初始化如下(伪代码):
DeviceContext m_device = DeviceContext(hwnd);
m_device.SetPixelFormat();
OpenGLContext m_opengl = OpenGLContext(m_device);问题是对SetPixelFormat()的调用,因为在DisplayOpenGL c‘’tor的初始化程序列表中不能这样做:
class DisplayOpenGL {
public:
DisplayOpenGL(HWND hwnd)
: m_device(hwnd),
// <- Must call m_device.SetPixelFormat here ->
m_opengl(m_device) { };
private:
DeviceContext m_device;
OpenGLContext m_opengl;
};我能看到的解决办法:
m_dummy(m_device.SetPixelFormat()) -将不能工作,因为SetPixelFormat()没有retval。(你应该这样做吗?)unique_ptr<OpenGLContext> m_opengl;而不是OpenGLContext m_opengl;。
然后初始化为m_opengl(),在c‘’tor主体中调用SetPixelFormat()并使用m_opengl.reset(new OpenGLContext);SetPixelFormat() C‘’tor调用DeviceContext这些解决方案中哪一种更可取,为什么?我遗漏了什么吗?
如果重要的话,我在Windows上使用VisualStudio2010Express。
编辑:,我最感兴趣的是在决定其中一种方法时所涉及的权衡。
m_dummy()不工作,而且看起来很不雅致,即使它会unique_ptr<X>对我来说很有趣--我什么时候才能用它来代替“普通”X m_x成员呢?除了初始化问题之外,这两种方法在功能上似乎或多或少是等价的。SetPixelFormat()‘tor打电话给DeviceContext当然有效,但我觉得不干净。DeviceContext应该管理资源并启用它的使用,而不是向用户强加一些随机像素格式的策略。InitDev()看起来是最干净的解决方案。在这种情况下,我几乎总是想要一个基于智能指针的解决方案吗?
发布于 2012-11-09 19:18:57
逗号接线员去救援!表达式(a, b)将首先计算a,然后计算b。
class DisplayOpenGL {
public:
DisplayOpenGL(HWND hwnd)
: m_device(hwnd),
m_opengl((m_device.SetPixelFormat(), m_device)) { };
private:
DeviceContext m_device;
OpenGLContext m_opengl;
};发布于 2012-11-09 20:55:02
在这种情况下,我几乎总是想要一个基于智能指针的解决方案吗?
不是的。避免这种不必要的并发症。
有两种直接的办法没有提到:
方法A:
干净的方式。
为m_device的存储创建一个小容器对象,在构造函数中调用SetPixelFormat()。然后用该类型的实例替换DisplayOpenGL ::m_device。获得初始化顺序,目的非常清楚。插图:
class DisplayOpenGL {
public:
DisplayOpenGL(HWND hwnd)
: m_device(hwnd),
m_opengl(m_device) { }
private:
class t_DeviceContext {
public:
t_DeviceContext(HWND hwnd) : m_device(hwnd) {
this->m_device.SetPixelFormat();
}
// ...
private:
DeviceContext m_device;
};
private:
t_DeviceContext m_device;
OpenGLContext m_opengl;
};方法B:
快速而肮脏的方式。在这种情况下,可以使用静态函数:
class DisplayOpenGL {
public:
DisplayOpenGL(HWND hwnd)
: m_device(hwnd),
m_opengl(InitializeDevice(m_device)) { }
private:
// document why it must happen this way here
static DeviceContext& InitializeDevice(DeviceContext& pDevice) {
pDevice.SetPixelFormat();
return pDevice;
}
private:
DeviceContext m_device;
OpenGLContext m_opengl;
};发布于 2012-11-10 06:10:45
首先,你做错了。永远不会。使那些必须传递给构造函数的助手对象上的操作函数。最好是在类之外构造复杂的对象,并将它们完全创建,这样,如果需要将它们传递给其他类,也可以同时将它们传递到它们的构造函数中。另外,这样您就有机会检测错误,添加合理的日志,等等。
class OpenGLInitialization
{
public:
OpenGLInitialization(HWND hwnd)
: mDevice(hwnd) {}
void SetPixelFormat (void) { mDevice.SetPixelFormat(); }
DeviceContext const &GetDeviceContext(void) const { return mDevice; }
private:
DeviceContext mDevice;
};
class DisplayOpenGL
{
public:
DisplayOpenGL(OpenGLInitialization const &ogli)
: mOGLI(ogli),
mOpenGL(ogli.GetDeviceContext())
{}
private:
OpenGLInitialization mOGLI;
OpenGLContext mOpenGL;
};https://stackoverflow.com/questions/13311184
复制相似问题