Reputation: 101
Code will probably better explain my problem better than words:
#include <string>
struct Something {};
struct Context
{
std::string GetUniqueInentifier()
{
return "";
}
// ERROR
void Register(const Something& something, const std::string& key = GetUniqueInentifier())
{
}
};
int main()
{
Context c;
c.Register(Something{}); //<- want to be able to do this
// and a unique identifier will
// be automatically assigned
c.Register(Something{}, "Some Key"); //<- want to be able to let the user
// pick an identifier if they want
}
That is clearly not allowed but how can I simulate this behaviour ?
Upvotes: 1
Views: 152
Reputation: 117298
You can't use non-static
member functions or variables as a default value to member functions.
Since the value returned by GetUniqueInentifier()
doesn't require an instance of Context
, make it static
and you can then use it as you tried using it.
static std::string GetUniqueInentifier()
{
return "";
}
Upvotes: 2