:: .* . ?:
cout << operator+(item1,item2) << endl;
cout << item1 + item2 << endl
ostream&
operator<<(ostream& out,const Sales_item& s)
{
out << s.isbn << "\t" << s.units_sold << "\t"
<< s.revenue << "\t" << s.avg_price();
return out;
}
istream&
operator<<(istream& in, Sales_item& s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if(in)
s.revenue = s.unit_sold * price;
else
s = Sale_item();
return out;
}
inline bool
operator==(const Sale_item& c1,Sale_item& c2)
{
return c1.units_sold == c2.units_sold &&
c1.revenue == c2.revenue &&
c1.same_isbn(c2);
}
inline bool
operator!=(const Sale_item& c1,Sale_item& c2)
{
return !(c1 == c2);
}
using namespace std;
class A{
public:
void action(){
cout << "Action in class A!" << endl;
}
};
class B{
A a;
public:
A* operator->(){
return &a;
}
void action(){
cout << "Action in class B!" << endl;
}
};
class C{
B b;
public:
B operator->(){
return b;
}
void action(){
cout << "Action in class C!" << endl;
}
};
int main(int argc, char *argv[])
{
C* pc = new C;
pc->action();
C c;
c->action();
getchar();
return 0;
}
c.operator++(0);
class absInt
{
int operator()(int val)
{
return val > 0 : -val : val;
};
};
bool GT6(const string &s)
{
return s.size() >= 6;
}
vector<string>::size_type wc =
count_if(words.begin(),words.end(),GT6);
class GT_cls
{
public:
GT_cla(int val = 0):bound(val){}
bool operator()(const string &s)
{
return s.size() >= bound;
}
private:
string::size_type bound;
};
cout << count_if(words.begin(),words.end(),GT_cla(6))
<< " words 6 characters or longer" << endl;
类型 函数对象 所应用的操作符
算术函数对象类型 plus<Type> +
minus<Type> -
multiplies<Type> *
divides<Type> /
modulus<Type> %
negate<Type> -
关系函数对象类型 equal_to<Type> ==
not_equal_to<Type> !=
greater<Type> >
greater_equal<Type> >=
less<Type> <
less_equal<Type> <=
逻辑函数对象类型 logical_and<Type> &&
logical_or<Type> |
logical_not<Type> !
plus<int> intAdd;
int sum = intAdd(10,20);
sort(svec.begin(),svec.end(),greater<string>());
count_if(vec.begin(),vec,end(),bind2nd(less_equal<int>(),10)));
count_if(vec.begin(),vec,end(),not1(bind2nd(less_equal<int>(),10))));
class SmallInt
{
public:
SmallInt(int i = 0):val(0)
{
if(i < 0 || i > 255)
throw std::out_of_range("Bad SmallInt Initialize")
}
operator int() const{return val;}
private:
std::size_t val;
};
SmallInt si;
double dval;
si >= dval;
if(si)
class Intergral
{
Intergral(int i = 0):val(i){}
operator SmallInt() const {return val % 256;}
private:
std::size_t val;
};
int calc(int);
Integral inVal;
SmallInt si(intVal);
int i = calc(si)
int j = calc(intVal)