🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ constructor question

Started by
5 comments, last by JoeJ 4 years, 10 months ago

I hava a class (PathMapping) with static const references, so need to init them in its constructor.

I nest it into another class (LOD), and add the necessary logic to its constructor as well.

This works as expected for a sinple object like so: 


	LOD test (templateMesh, templateConn, templateEdgeLengths);
	

 

But for multiple objects in a vector it does not:


	std::vector<LOD> lods;
	lods.resize(10, LOD (templateMesh, templateConn, templateEdgeLengths));
	

In this case the const references from pathMapping end up wrong. The remesh in nested pathMapping is not the same as in its enclosing LOD object. :(

I guess instead it uses the data contained in the single temporary LOD variable created while initializing the vector.

 

I could work around this by using a vector of pointer instead objects, but i wonder if this could be fixed properly somehow?

 

 


struct LOD
{
				HEMesh remesh;
				PathMapping pathMapping;
  
				LOD (	const HEMesh &templateMesh,
						const MeshConnectivity &templateConn,
						const std::vector<float> &templateEdgeLengths)
					: pathMapping (
						remesh, templateMesh, templateConn, templateEdgeLengths, 
						remeshVertexToTemplate) {};

};
			

 

Advertisement

What do your LOD copy constructor and assignment operators look like?

Stephen M. Webb
Professional Free Software Developer

10 minutes ago, Bregma said:

What do your LOD copy constructor and assignment operators look like?

I have neither, but already thought this could give a solution?

Here is some code from the PathMapping, just to show the problematic remesh reference:


class PathMapping
{
	const HEMesh &remesh;
	// vectors of other data, e.g. paths

	PathMapping (const HEMesh &remesh) : remesh(remesh) {}
};

So within custom operators in LOD, likely i have to recreate the pathMapping object so i can fix the reference? 

Sounds cumbersome. Likely it would be better to use pointer instead reference. I use a const reference member the first time in such a situation, and now i'm unsure if this is bad practice in general, leading to unnecessary copies / moves of the other data?

Pointer won't solve this problem. You need to add a copy constructor and an assignment operator to your struct LOD that initialize the reference correctly.

You can have const reference members, but you have to follow the rules.  Be aware that having a const reference member means you no longer have value semantics in your object, and that means things might not work the way you intuitively expect them to.

If you use pointers, you still have to follow the rules.  It takes the same amount of space in memory, but what it tells the reader is subtly different.

Stephen M. Webb
Professional Free Software Developer

Thanks, guys. Unfortunately i have to pospone my C++ learning experience a bit. I have some serious failure case i need to fix first, and may update the post in some days i hope ...

This topic is closed to new replies.

Advertisement