Is there a way I can make my custom class be passed by reference only?
Basically, I have the following class:
class A{
private:
int _x;
public:
A(int y){
_x = y;
}
};
Can I make it to where I can ONLY pass it by reference? And it will throw a compile time error otherwise?
Several people have suggested making the copy constructor private. This is a mostly good solution to the problem however it’s not complete. It still allows the type itself to accidentally pass itself by value. A more thorough solution is to declare the copy constructor private and then never implement it.
Note: As @DeadMG points out, in C++11 using
deleteis preferred.