Jump to content
  • Advertisement
JoeJ

C++ C++ constructor question

Recommended Posts

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

};
			

 

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Posted (edited)

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.

Edited by Magogan

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!