Method 1:- Overloaded '&' operator and keep it private
#include <iostream>
using namespace std;
class Base
{
int x;
public:
Base(){}
private:
Base* operator &()
{
return this;
}
};
int main()
{
Base b;
Base *bp = &b;
cout << &b << endl;
cout << bp << endl;
return 0;
}
O/P: error: 'Base* Base::operator&()' is private within this context
Method 2:- delete '&' operator from your class, C++11
#include <iostream>
using namespace std;
class Base
{
int x;
public:
Base(){}
Base* operator &() = delete;
};
int main()
{
Base b;
Base *bp = &b;
cout << &b << endl;
cout << bp << endl;
return 0;
}
O/P: error: use of deleted function 'Base* Base::operator&()'
Comments
Post a Comment