Compiler behavior in case of an erroneous method description

 

The following code compiles without errors or warnings:

#property strict

class CTest
  {
private:
   int    memberA;
   double memberB;
public:
   void   initialize(const int a, const double b);
  };

void CTest::initialize(int a, double b)
  {
   a++;
   memberA = a;
   memberB = b;
  }

How normal is this? Personally, I would like to see a compilation error in such a case.


 
The description of the arguments does not match the declaration. But if I have to check it manually, then I can just as well remember that the argument must not be modified inside the method (and not use const)
 

When passed by reference, a match check is performed:

class CTest
  {
private:
   int    memberA;
   double memberB;
public:
   void   initialize(const int &a, const double b);
  };

void CTest::initialize(int &a,double b) // 'initialize' - member function already defined with different parameters
  {
   memberA = a;
   memberB = b;
  }
 
Vladislav Boyko #:

When passed by reference, a match check is performed:

Because in this case it would (could) change the caller state. While with by value, const or not const is without any impact outside the function.

See (C++) : https://stackoverflow.com/questions/3681188/why-does-a-function-declaration-with-a-const-argument-allow-calling-of-a-functio

You have the choice to be consistent or not.

Why does a function declaration with a const argument allow calling of a function with a non-const argument?
Why does a function declaration with a const argument allow calling of a function with a non-const argument?
  • 2010.09.09
  • Alerty Alerty 5,935 7 7 gold badges 38 38 silver badges 62 62 bronze badges
  • stackoverflow.com
Take note of the following C++ code: Notice that the prototype of takes a and that the definition takes an . This compile without any errors... Why are there no compilation errors?
 
Alain Verleyen #:

Because in this case it would (could) change the caller state. While with by value, const or not const is without any impact outside the function.

See (C++) : https://stackoverflow.com/questions/3681188/why-does-a-function-declaration-with-a-const-argument-allow-calling-of-a-functio

You have the choice to be consistent or not.

Thanks a lot for the explanation!