• Advertisement
Sign in to follow this  

About constant reference

This topic is 1784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

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

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

	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.Pos    = mWaves;
	v.Normal = mWaves.Normal(i);

md3dImmediateContext->Unmap(mWavesVB, 0);


l know the problem is happened in this sentences:

v.Normal = mWaves.Normal(i);

Function Normal's value can't assignment to v.Normal.


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


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


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


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement