摘要: #include "stdafx.h"#include #include #include /*重载成员访问操作符 ->, *,主要用于支持指针型类,如迭代器。一般箭头操作符必须定义为类的成员函数,解引用*一般也定义为类的成员*/namespace operator_test{ class CObject { public: CObject(std::string name, int age) :m_name(name), m_age(age) { } std::string& GetName() { return m_name; } int GetAge() 阅读全文
posted @ 2013-11-24 20:12 sysnap 阅读(212) 评论(0) 推荐(0)
摘要: #include "stdafx.h"#include #include /*重载运算符,选择成员还是非成员实现?1 赋值(=), 下标([]), 调用(()), 成员访问箭头(->)操作符必须定义为成员2 复合赋值操作符( +=, -=, *=, \=, %=, &=, ^=, |= , >=) 一般定义为类的成员,但不是必须这么做,也可以定义为非成员3 自增,(++),自减(--)和解引用(*)一般会定义为类成员4 算术运算符(+, -, *, /, %, ),相等操作符( == ),关系操作符(>, =, m_a = obj.Get(); r 阅读全文
posted @ 2013-11-24 15:10 sysnap 阅读(163) 评论(0) 推荐(0)
摘要: #include "stdafx.h"#include #include ////函数重载//namespace operator_test{ // //先定义一个类 // class CObject { friend std::ostream& operatorm_a += obj2.Get(); return *this; } private: int m_a; }; std::ostream& operator<<(std::ostream& out, CObject& object) { out << obje 阅读全文
posted @ 2013-11-24 12:55 sysnap 阅读(155) 评论(0) 推荐(0)
摘要: #include "stdafx.h"#include #include namespace friend_test{ class CObject { friend std::ostream& operator<<(std::ostream& os, CObject& object); public: CObject(std::string name, int age) : m_age(age), m_name(name) { } std::string& getname() { return m_name; } int geta 阅读全文
posted @ 2013-11-24 12:48 sysnap 阅读(183) 评论(0) 推荐(0)
摘要: #include "stdafx.h"#include ////函数重载//namespace operator_test{ // //先定义一个类 // class CObject { public: CObject(int a) : m_a(a) { } int Get() { return m_a; } int Add(CObject& obj2) { return (this->Get() + obj2.Get()); } int operator+(CObject& obj2) { return (this->Get() + obj2. 阅读全文
posted @ 2013-11-24 11:26 sysnap 阅读(165) 评论(0) 推荐(0)