13/11/2009 C++ tips: It's an instantiation! No a function declaration!

C++

C++, I love this language, is it the best one? I don't know, I don't care much, all I know it's the one I have the most fun with and always something left to learn...

Today, I wrote this simple code that open my eyes wide and hit my head on my desk a couple of time.

Class declaration:
  • class Class
  • {
  • public:
  • Class(glm::uvec2 const & TileSize);
  • void Func(int Param1, int Param2);
  • };
Use case:
  • int func(glm::uvec3 const & TileSize)
  • {
  • Class MyClass(glm::uvec2(TileSize));
  • MyClass.Func(76, 76);
  • }

But unfortunately I have got an error:

error C2660: 'MyClass' : function does not take 2 arguments. Visual Studio 2005

I have to say that when the code is simplified to this extreme, it's quite easier to understand what is happening: MyClass isn't a Class instance, it is a function declaration that takes a glm::vec2 is parameter and return a Class. This is because of the cast from glm::vec3 to glm::vec2.

Build fix:
  • int func(glm::uvec3 const & TileSize)
  • {
  • glm::uvec2 TileSize2(TileSize));
  • Class MyClass(TileSize2);
  • MyClass.Func(76, 76);
  • }

Thanks to Thomas Thiriez, the C++ God I asked for deep and accurate answers. He even gives me a perfect reference about this C++ twist.

GLM 0.8.4.3 released >
< Creation process: When photography made me a better programmer.
Copyright © Christophe Riccio 2002-2016 all rights reserved
Designed for Chrome 9, Firefox 4, Opera 11 and Safari 5