Question

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.

Was it helpful?

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) { .... }

OTHER TIPS

You can't do this for non-const references. A reference is pointing to an address in memory. Unless you specifically need reference semantics, I'd recommend just passing by value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top