User Tools

Site Tools


c_-_c_operator_overloading:conversion_operator_overloading

C - C++ Operator Overloading - Conversion operator overloading

#include <iostream>
 
using namespace std;
 
class Forgetcode
{
public:
  Forgetcode();
  Forgetcode(int val);
  ~Forgetcode(){}
  int getval()const { return val; }
  void setval(int x) {val = x; }
  operator unsigned long();
 
private:
  int val;
};
 
 
Forgetcode::Forgetcode():val(0) {}
 
Forgetcode::Forgetcode(int y): val(y) {}
 
Forgetcode::operator unsigned long()
{
  return ( int (val) );
}
 
 
int main()
{
  Forgetcode ctr(50000000);
  int longvar = ctr;
  cout << "longvar " << longvar << std::endl;
  return 0;
}
c_-_c_operator_overloading/conversion_operator_overloading.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki