Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ constructor question

Started by JoeJ Aug 28, 2019 at 7:09 PM 5 replies 3k views
Original Post
JoeJ
JoeJ

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

};
			


Bregma
Bregma

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

Stephen M. Webb
Professional Free Software Developer
JoeJ
JoeJ
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?

Beosar
Beosar

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.

Bregma
Bregma

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
JoeJ
JoeJ

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 ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.