创建对对象的引用会导致该引用损坏。这是绝对奇怪的,因为它似乎是简单的C++功能。另一件奇怪的事情是,这个问题似乎是在没有更改任何相关代码的情况下出现的(据我所知(。如果非要我猜测的话,我会说这与所涉及的DirectX代码有关。
这是代码:
// the header file of the class of the problem object
#pragma once
#include<vector>
#include<Windows.h>
#include<xnamath.h>
#include<string>
using namespace std;
#define float2 XMFLOAT2
#define float4 XMFLOAT4
namespace DXLib
{
struct Sprite
{
char* imagename;
float2 pos;
float rot;
float2 scale;
float4 rgba;
};
struct Rect
{
float2 pos;
float2 size;
float4 color;
};
struct Text
{
std::string string;
float2 pos;
};
class D3DDraw
{
public:
D3DDraw(int maxdepth = 0);
~D3DDraw(void);
void DrawSprite(char* imagename, float2 pos, int depth = 0, float rot = 0.0f, XMFLOAT2 scale = XMFLOAT2(1, 1), float4 rgba = float4(1, 1, 1, 1));
//void DrawRect(float2 pos, float2 size, float4 color);
void DrawDXText(std::string string, float2 pos);
void Clear(void);
public:
std::vector<vector<Sprite>> depths_;
std::vector<Text> texts_;
int maxdepth_; // set on initialize, do not change
float2 spriteoffset_;
};
}
// the header file for the base class for the class in which the problem occurs
#pragma once
#include <windows.h>
#include"D3DCore.h"
#include"D3DRender.h"
#include"D3DDraw.h"
#include"DXInput.h"
#include<cassert>
using namespace DXLib;
const UINT uWindowSizeX = 1000;
const UINT uWindowSizeY = 800;
class Application
{
public:
Application();
~Application();
HRESULT Run();
protected:
virtual void Update(void);
private:
static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
protected:
D3DDraw m_cDraw;
DXInput m_cInput;
private:
HWND m_hWnd;
D3DCore m_cCore;
D3DRender m_cRender;
};
// the class header file
#pragma once
#include"stdafx.h"
#include"VGraphics.h"
#include"EntityPool.h"
#include"VUI.h"
#include"Manager.h"
class VApp : public Application
{
public:
VApp(void);
~VApp(void);
virtual void Update(void) override;
private:
void Log(double dTime); // prints log info to console every nth frame
private:
int m_iTime;
VGraphics m_cGraphics;
EntityPool m_cPool;
VUI m_cVUI;
Manager m_cManager;
};
// the function in which the problem occurs
void VApp::Update(void)
{
double dTime = (GetTickCount() - m_iTime) / 1000.0;
D3DDraw& rcDraw = m_cDraw; // that object is initialized and just fine
// that reference now points to an uninitialized object
m_cVUI.Tick(m_cInput, m_cDraw, m_cPool, m_cManager);
m_cManager.Tick();
m_cGraphics.Draw(m_cVUI.GetView(), m_cPool, m_cDraw);
Log(dTime);
m_iTime = GetTickCount();
Sleep(15);
}
我不知道为什么,但在这里更改访问说明符:
protected:
D3DDraw m_cDraw;
DXInput m_cInput;
对于public
解决了这个问题,这是一个破解,但我可以接受:D
无论如何,感谢您尝试帮助
好吧,这是我对SSCCE的尝试,但我不能让它失败(可能与通过指向错误类型对象的指针访问受保护成员有关(:
class Thing
{
public:
int value;
Thing() { value = 42; }
~Thing() { };
};
class Foo
{
public:
Foo() { }
~Foo() { }
public:
void CallMethod()
{
Method();
}
protected:
virtual void Method()
{
}
Thing m_x;
};
class Bar : public Foo
{
public:
Bar() { }
~Bar() { }
virtual void Method() override
{
Thing& x = m_x;
printf("%dn", x.value);
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Foo f;
Foo& rf = f;
printf("%pn", &rf);
Bar bar;
Foo& rfoo = bar;
rfoo.CallMethod();
return 0;
}