Original Post
I'm writing code for a simple heightmap class and I'm just curious why one version of a method works but another doesn't. The following sections of code are just snippets, if it would help to see both definitions in full, they can be found here (for the next 24 hours): http://rafb.net/p/cx2IRf99.html Anyway, the situation at hand involves one constructor calling another. The first takes a string to a bitmap file, creates a bitmap object, and passes the bitmap to the second. Below are two different definitions for that first constructor. One works, the other does not. Can anyone tell me why? What's even more peculiar is that I do almost the same thing in another section: It seems to work with a method but not with a constructor. Maybe that's the distinction? EDIT: Also, is the working version more efficient, or is it too creating a temporary variable like the non-working version?
class Terrain
{
public:
Terrain(const string& fileName);
Terrain(const Bitmap& bitmap);
};
// Works
Terrain::Terrain(const string& fileName)
{
Terrain(Bitmap(fileName));
}
// Error: error C2371: 'bitmap' : redefinition; different basic types
Terrain::Terrain(const string& fileName)
{
Bitmap bitmap(fileName);
Terrain(bitmap);
}
void Terrain::ProcessGeometry(const string& fileName)
{
Bitmap bitmap(fileName);
ProcessGeometry(bitmap);
}
void Terrain::ProcessGeometry(const Bitmap& bitmap)
{
m_MapWidth = bitmap.GetWidth();
m_MapLength = bitmap.GetHeight();
InitializeMap();
LoadHeightmap(bitmap);
CalculateSurfaceNormals();
CreateDisplayLists();
}