Getting Xcode to enforce datatypes and constants in virtual functions

So, I notice that Xcode (v6.2) doesn't seem to enforce the creation of constants in pure virtual functions where Visual Studio does. A couple of questions: 1) If I have a pure virtual in the base class that has a const argument and is NOT const if it is defined in the derrived class ... since this is ANSI compliant and: 2) Is there a way to force Xcode throw an error like Visual Studio?

In the first block, we have a header file and it has a pure virtual function.

// Some header file (assume include guards and all that fun stuff)
namespace Testing123
{
    class ClassA
    {
    public:
        // ************ NOTICE THAT THE ARGUMENT IS CONST!!! ***************   
        virtual double  somePureVirtualFunk( const double someNumber ) = 0;       

    protected:
        inline double getSomeFunkyNumber(){ return m_something; };
    private:
        double m_something;
    }
}

      

Now, in the second file, we have a header for the derived class with an inline function that defines our pure virtual object from ClassA ...

// Some header file (assume include guards and all that fun stuff)
#include "ClassA.h"

namespace Testing123
{
    class ClassB : public ClassA
    {
    public:
        // ************ NOTICE THAT THE ARGUMENT IS **NOT** CONST!!! *************   
        inline virtual double  somePureVirtualFunk( double someNumber )
        { return this->getSomeFunkyNumber(); }; 
    }
}

      

So I think this demonstrates what I see, in ClassA somePureVirtualFunk is declared as having a const argument, and when it is defined in ClassB there is no constant. Visual Studio kindly warns you about this, Xcode doesn't. Is there a way to get Xcode to warn me about this?

+3


source to share


1 answer


1) Conforms to standards. The top level is const

ignored, so this

virtual double  somePureVirtualFunk( const int someNumber ) = 0; 

      

matches exactly this

virtual double  somePureVirtualFunk( int someNumber ) = 0; 

      



It makes no sense to set const

a declaration in the function, because it is ignored. It can be used as an implementation detail used in a function definition, where it simply means that the local parameter cannot be changed in the function body.

double ClassB::somePureVirtualFunk(const int someNumber )
{
  // someNumber cannot be modified here.
  // But it is local, so nobody needs to know.
}

      

Note that this only applies to the top level const

, as in your example.

2) I'm not sure how helpful a warning would be, given that the function signatures are the same. A good warning is that you are saying that you have a top-level constant qualifier in your function declaration and that it has no effect.

+4


source







All Articles