Sign in to follow this

About constant reference

Recommended Posts    424

Today,l was met a strange problem.

 l have a buffer called mWavesVB:

ID3D11Buffer* mWavesVB;


And a class called wave:


Waves mWaves;


The declaration like this:


class Waves

	// Returns the solution at the ith grid point.
	const XMFLOAT3& operator[](int i)const { return mCurrSolution[i]; }

	// Returns the solution normal at the ith grid point.
	const XMFLOAT3& Normal(int i)const { return mNormals[i]; }

	// Returns the unit tangent vector at the ith grid point in the local x-axis direction.
	const XMFLOAT3 TangentX(int i)const { return mTangentX[i]; }

	void Init(UINT m, UINT n, float dx, float dt, float speed, float damping);
	XMFLOAT3* mCurrSolution;
	XMFLOAT3* mNormals;
	XMFLOAT3* mTangentX;


The member variable all initialize and assgin the suitable value.


But when the following call happened,the project crashed:


md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData);

Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData);
for(UINT i = 0; i < mWaves.VertexCount(); ++i)
	v[i].Pos    = mWaves[i];
	v[i].Normal = mWaves.Normal(i);

md3dImmediateContext->Unmap(mWavesVB, 0);


l know the problem is happened in this sentences:

v[i].Normal = mWaves.Normal(i);

Function Normal's value can't assignment to v[i].Normal.


but the things really bother me is,when the Normal's definition changed into:


const XMFLOAT3 Normal(int i)const { return mNormals[i]; }


Then the project suddenly work well. It‘s just like some magical's So odd.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this