views:

88

answers:

2

I would like to have declaration like this:

void Date::get_days_name(const Date& = this)

which I would understand that if no argument is provided use this object as an argument. For some reason in VS I'm getting err msg:

'Error 1 error C2355: 'this' : can only be referenced inside non-static member '

Any idea what I'm doing wrong?

+5  A: 

You could make overloaded function:

void get_days_name(const Date&) const;
void get_days_name() const {
  get_days_name(*this);
}

(BTW, this is a pointer, not a reference.)

KennyTM
A: 

I like Kenny's answer, but if you are willing to change the parameter from a reference to a pointer you could do it with one function:

void Date::get_days_name(const Date* value_ = NULL) const
{
  const Data* value =
    value_ != NULL ?
    value_ :
    this;
  // the rest of the code operates on value.
}

Using a pointer more clearly communicates that value_ is an optional parameter, as well.

However, get_days_name should probably be static if it can operate on any Date freely.

Bill
Yes, nice alternative.
There is nothing we can do