I would like to create a c++ type that mimic the build-in type exactly. Below is an example of an “integer” type that “boxed in” an “int” type. Problem I have is, I want to show the value of “integer” using only the stand alone “integer” object d, such that cout << d will show the value, not cout << d.show ( ); how would I do that ?
#include <iostream>
class integer {
public:
integer (int x) { i = x; };
integer ( ) { }; // default constructor
integer operator+ (integer& c ){
return integer(i + c.i);
}
int show ( ) { return i; }
private:
int i;
};
int main ( ) {
integer i = 5;
integer c (10);
integer d;
d = i + c;
std::cout << d.show() << std::endl;
std::cin.get();
return 0;
}
You can overload
operator <<to do that:and make
showconst.