Search code examples
c++methodscompiler-errorsdefault-parameters

Defining the default value for a ULONG& optional parameter as 0


The following function declaration:

void Foo:DoSomething( ULONG &Count = 0) { .... }

Results in the following compile time error

error C2440: default argument cannot convert from 'int' to 'ULONG &'

What is the correct way of creating the signature so that when there is no parameter provided for Count its value will be zero.


Solution

  • You're taking a non-const reference to Count, so it can't be assigned by default with r-value.

    Use

    void Foo:DoSomething( const ULONG &Count = 0) { .... }

    void Foo:DoSomething( ULONG Count = 0) { .... }