operator overloading in c++

Possible Duplicate: Operator overloading Hi guys can some one please suggest a good tutorial on operator overloading? I was going through this code on operator overloading and I have the following doubts code: #include <iostream> using namespace std; class Array { int *a; int size; int capacity; public: Array (int c) {a=new int[c]; size=0; capacity =c; } ; Array &

运算符在c ++中重载

可能重复: 运算符超载 嗨,大家可以请一个人提出一个关于运算符重载的好教程吗? 我正在通过这个关于运算符重载的代码,我有以下怀疑 码: #include <iostream> using namespace std; class Array { int *a; int size; int capacity; public: Array (int c) {a=new int[c]; size=0; capacity =c; } ; Array & operator << (int x); int operator [] (int i) { if (i<

Why is no return type specified in this function clearly returns?

Possible Duplicate: Operator overloading I am seeing this in a piece of sample code: operator Vector2<float>() const { return Vector2<float>(x, y); } My 2 questions about this: 1) The function clearly returns, but there's no return type specified? 2) It's not clear exactly what is getting overloaded here, which operator. 它是一个转换运算符,返回类型是

为什么没有明确返回该函数中指定的返回类型?

可能重复: 运算符超载 我在一段示例代码中看到了这一点: operator Vector2<float>() const { return Vector2<float>(x, y); } 我有两个问题: 1)函数明确返回,但没有指定返回类型? 2)目前还不清楚在这里哪个运营商负载过重。 它是一个转换运算符,返回类型是Vector2<float> 。

I have problems implementing the ++ increment operator

This question already has an answer here: What are the basic rules and idioms for operator overloading? 7 answers operator++() is the prefix increment operator. Implement the postfix operator as operator++(int) . The canonical implementations are have the prefix operator return a reference and the postfix operator return by value. Also, you would typically implement the postfix operator

我执行++增量运算符时遇到问题

这个问题在这里已经有了答案: 运算符重载的基本规则和习惯用法是什么? 7个答案 operator++()是前缀增量运算符。 以operator++(int)实现postfix运算operator++(int) 。 规范实现有前缀运算符返回引用,后缀运算符返回值。 此外,您通常会根据前缀运算符来实现postfix运算符,这是为了让人惊奇且易于维护。 例: struct T { T& operator++() { this->increment(); return *this; } T operator++(int)

C++ operator== overloading

Possible Duplicate: Operator overloading What is the differences between the following ways to overload operator== ? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); and // as taught in other places, including caltech bool MyClass::operator== (MyClass &rhs); Which way is better? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rh

C ++运算符==重载

可能重复: 运算符超载 下面的方式来重载operator ==有什么不同? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); 和 // as taught in other places, including caltech bool MyClass::operator== (MyClass &rhs); 哪种方法更好? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); 参数应该是const : friend bool operator==(const MyClass&

Creating an assignment (=) operator for class in C++

Possible Duplicate: Operator overloading EDIT 2 I was using insert(...) incorrectly, I didn't actually need a '=' operator. Sorry to waste peoples' time. I have voted to close.. 2 votes remain. Please vote. EDIT The reason I want an '=' operator is so I can use the insert(...) function on a vector of Derivation objects. At the moment my compiler says: /usr/

在C ++中为类创建赋值(=)运算符

可能重复: 运算符超载 编辑2 我正在使用插入(...)不正确,我实际上并不需要'='运算符。 抱歉浪费人们的时间。 我已投票结束..仍然有2票。 请投票。 编辑 我想要'='运算符的原因是我可以在Derivation对象的矢量上使用insert(...)函数。 目前我的编译器说: /usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'

operator<< overloading

Possible Duplicate: Operator overloading I didn't find any thing that could help me in this subject... I'm trying to over load the << operator , this is my code: ostream& Complex::operator<<(ostream& out,const Complex& b){ out<<"("<<b.x<<","<<b.y<<")"; return out; } this is the declaration in the H file: ostream&am

运算符<<重载

可能重复: 运算符超载 我没有找到任何可以帮助我解决这个问题的东西......我试图过载<< operator ,这是我的代码: ostream& Complex::operator<<(ostream& out,const Complex& b){ out<<"("<<b.x<<","<<b.y<<")"; return out; } 这是H文件中的声明: ostream& operator<<(ostream& out,const Complex& b); 我得到这个错误: e

How to overload the conditional operator?

Possible Duplicate: Operator overloading I was wonder how can I over load the Conditional operator in cpp? int a,b,c; a=10; b=11; c = (a>b) ? a : b; Is it possible? Several operators cannot be overloaded. These operators take a name, rather than an object, as their right operand: Direct member access (.) Deference pointer to class member (.*) Scope resolution (::) Size

如何重载条件运算符?

可能重复: 运算符超载 我很想知道如何在cpp中加载条件运算符? int a,b,c; a=10; b=11; c = (a>b) ? a : b; 可能吗? 几个操作员不能超载。 这些操作员将一个名称而不是一个对象作为它们的右操作数: 直接成员访问(。) 类成员(。*)的引用指针 范围分辨率(::) (sizeof)的大小 条件运算符(?:)也不能重载。 另外,新的类型转换运算符:static_cast <>,dynamic_cast <>,rei

overloading the extraction operator >> in C++

Possible Duplicate: Operator overloading I have to code a clock program in which I could enter the hours, minutes and seconds while overloading the extraction operator. These are my codes: clockType.h #include<iostream> using namespace std; class clockType { public: clockType(); void getTime(); friend istream& operator>>(istream&, const clockType); private:

在C ++中重载提取操作符>>

可能重复: 运算符超载 我必须编写一个时钟程序,在该程序中,我可以输入小时,分钟和秒钟,同时超载提取操作员。 这些是我的代码: clockType.h #include<iostream> using namespace std; class clockType { public: clockType(); void getTime(); friend istream& operator>>(istream&, const clockType); private: int hr, min, sec; } clockType.cpp #include<iostream> #i

C++ chaining of the operator << for std::cout like usage

Possible Duplicate: std::endl is of unknown type when overloading operator<< Operator overloading I'm currently programming a logger class, but the operator<< method causes a compiler error. Here's a minimized version of the class, in file "logger.h": #include <iostream> class Logger { public: Logger() : m_file(std::cout) {} template <typena

用于std :: cout的运算符<< C ++链接

可能重复: 当运算符<<重载时,std :: endl是未知类型 运算符超载 我目前正在编写一个记录器类,但operator<<方法会导致编译器错误。 这是一个最小化的类,在文件“logger.h”中: #include <iostream> class Logger { public: Logger() : m_file(std::cout) {} template <typename T> Logger &operator<<(const T &a) { m_file<<a; return *thi

Return value of operator++

This question already has an answer here: What are the basic rules and idioms for operator overloading? 7 answers You attempt to apply the second ++ to the temporary object returned by the first invocation. However, the operand must be passed by reference, and you can't bind a temporary to a non-constant lvalue reference. You probably don't want to "fix" this, since ther

运算符++的返回值

这个问题在这里已经有了答案: 运算符重载的基本规则和习惯用法是什么? 7个答案 您尝试将第二个++应用于第一个调用返回的临时对象。 但是,操作数必须通过引用传递,并且不能将临时值绑定到非常量左值引用。 你可能不想“修复”这个,因为没有必要修改这样的临时值。 但是,您应该在增加值之前返回该值的副本,以提供预期的后增量行为。 前缀运算符应返回一个引用,该引用可以愉快地绑定到另一个引用,以便++++x; 应