Tamim Addari
Tamim Addari

Reputation: 7841

error in reference qualifier

Why I cant define this function,

int *clone() const &
{ 
    return new int(10); 
}

or

int x;
int *clone() const &&
{ 
    return new int(std::move(x)) ; 
}

I should be able to add const qualifier functions. Should I include any headers?

Upvotes: 2

Views: 289

Answers (3)

Tim Zhang
Tim Zhang

Reputation: 73

I'm new to c++ and I am in the same error with you. I compiled the sample code, from C++ Primer 5th Edition, which described the reference qualifier. However, my GNU compiler showed me error. I suppose that current compilers do not support this new feature introduced in C++11. And it seems not many people know this because very few information can be found on the Internet. Maybe later compilers will support this feature.

Many c++11 features haven't been supported... I've met some before.

This is part of the sample code, similar to yours:

Foo sorted() &&;
Foo sorted() const &

Upvotes: 3

doctorlove
doctorlove

Reputation: 19272

You can use the r-value reference on parameters, for example in a move assignment or move constructor. It seems clang has been trying an extension called 'called "rvalue reference for *this"', but I suggest you work through the move constructors and assignment operators first.

Upvotes: 0

Anand Rathi
Anand Rathi

Reputation: 786

Because any qualifier after function name applies to this pointer. if you want to make constant this pointer
you should just do overloading by : int *clone() const

Upvotes: 0

Related Questions