我已经看到了operator()
STL容器的使用,但它是什么,你什么时候使用它?
在"The C++ Programming Language"一书中,作者给出了以下示例以及几个语句:
如果仅仅返回引用并让用户决定如何处理它是不可接受的,那么定义用于读取和写入的操作符(例如[])是很困难的.
Cref,是帮助实现区分阅读和写作的下标操作符.
为什么[]难以定义何时用于读写?类Cref的定义如何帮助解决这个问题?
class String{
struct Srep;
Srep *rep;
public:
class Cref;
// some definitions here
void check (int i) const { if (i<0 || rep->sz<=i) throw Range( );}
char read( int i) const {return rep->s[i];}
void write(int i, char c){ rep=rep->get_own_copy(); rep->s[i]=c;}
Cref operator[] (int i){ check(i); return Cref(*this, i);}
char operator[] (int i) const{check(i); return rep->s{i];}
}
class String::Cref{
friend class String;
String& s;
int i;
Cref(String& ss, int ii): s(ss),i(ii) {}
public:
operator …
Run Code Online (Sandbox Code Playgroud) 根据Bjarne Stroustrup的说法,引用了C++以支持运算符重载:
引用的引用主要是为了支持运算符重载.
C按值传递每个函数参数,并且按值传递对象的效率低或不合适,用户可以传递指针.在使用运算符重载的情况下,此策略不起作用.在这种情况下,符号方便是必不可少的,因为如果对象很大,则不能期望用户插入地址运算符.例如:
Run Code Online (Sandbox Code Playgroud)a = b - c;
是可接受的(即常规的)符号,但是
Run Code Online (Sandbox Code Playgroud)a = &b - &c;
不是.无论如何,
&b - &c
在C中已经有了意义,我不想改变它.
在语言中使用指针和引用是C++新手混淆的常见原因.
Bjarne不能通过引入一种特殊的语言规则来解决这个问题,如果存在需要这样的指针的用户定义的运算符函数,它允许对象参数衰减成指针吗?
减法的声明和用法将如下所示:
Foo operator-(const Foo* x, const Foo* y);
a = b - c;
Run Code Online (Sandbox Code Playgroud)
有没有提出/考虑过这样的解决方案?它有任何严重的缺点吗?
是的我知道,由于它们的限制,引用提供了其他优点,但这不是重点.
有趣的是,带有类的C的赋值运算符似乎完全相同:
通过声明一个名为的成员函数来更改类[...]对象的赋值含义
operator=
.例如:Run Code Online (Sandbox Code Playgroud)class x { public: int a; class y * p; void operator = (class x *); };
下面的代码无法使用-std=c++0x
交换机使用g ++版本4.5.0进行编译.我收到以下错误消息:
error: no match for 'operator+' in 'std::pow [with _Tp = float, _Up = int, typename __gnu_cxx::__promote_2<_Tp, _Up>::__type = double](((const std::complex<float>&)((const std::complex<float>*)(& x))), ((const int&)((const int*)(&2)))) + y'
Run Code Online (Sandbox Code Playgroud)
我认为这与此处提到的可分配要求有关.我应该为复杂定义我自己的复制赋值运算符吗?如果是这样,怎么样?
#include <complex>
using namespace std;
int main(int argc, char *argv[]) {
complex<float> x,y;
x = pow(x,2); // ok
x = x + y; // ok
x = pow(x,2) + y; // error
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的对象:
public class Tags
{
int mask;
public static bool operator !=(Tags x, Tags y)
{
return !(x == y);
}
public static bool operator ==(Tags x, Tags y)
{
return x.mask == y.mask;
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于将实例相互比较,但我也希望能够处理如下表达式:
if(tags1 == null)
Run Code Online (Sandbox Code Playgroud)
这样做会导致以下行出现异常:
return x.mask == y.mask;
Run Code Online (Sandbox Code Playgroud)
既然y
是null
.
我已经尝试将此功能更改为:
public static bool operator ==(Tags x, Tags y)
{
if (x == null && y == null) return true;
if (x == null || y == null) …
Run Code Online (Sandbox Code Playgroud) 我试图在C#中重载一个运算符(不要问为什么!)Lists
.例如,我希望能够写:
List<string> x = // some list of things
List<string> y = // some list of things
List<string> z = x + y
Run Code Online (Sandbox Code Playgroud)
所以'z'包含'x'的所有内容,后跟'y'的内容.我知道已经有办法组合两个列表,我只是想了解运算符重载如何与泛型结构一起工作.
(顺便说一下,这是List
班级Systems.Collections.Generic
.)
很难想出一个标题......(我不是英语母语人士.)
struct A
{
int value;
A operator+(int i) const
{
A a;
a.value=value+i;
return a;
};
};
int main(){
A a;
a.value=2;
a=a+2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码按预期编译/工作,但是当我将a = a + 2更改为a = 2 + a时,它将不再编译.GCC给了我这个错误:
no match for ”operator+” in ”2 + a”
Run Code Online (Sandbox Code Playgroud)
有没有办法以某种方式使2 +工作就像+ 2?
我需要能够将我的一个类(其中包含多于一个整数)与整数进行比较,即使这可能会延伸到相等的一点,但它足够接近......
如何为不同类型重载相等运算符?
我基本上有这样一个班级
struct MyClass {
int start;
int middle;
int threequarters;
};
Run Code Online (Sandbox Code Playgroud)
和重载的运算符
inline bool operator==(const MyClass& lhs, const MyClass& rhs) {
return lhs.middle == rhs.middle;
}
Run Code Online (Sandbox Code Playgroud)
所以,当我有需要比较靠中间变量以及整数比较,但我不知道我是否需要两套操作功能,其中一个整数LHS和一个地方的整数是RHS?
inline bool operator==(const int& lhs, const MyClass& rhs) {
return lhs == rhs.middle;
}
inline bool operator==(const MyClass& lhs, const int& rhs) {
return lhs.middle == rhs;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <iostream>
#include <boost\lexical_cast.hpp>
struct vec2_t
{
float x;
float y;
};
std::istream& operator>>(std::istream& istream, vec2_t& v)
{
istream >> v.x >> v.y;
return istream;
}
int main()
{
auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");
std::cout << v.x << " " << v.y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我从Boost收到以下编译错误:
错误1错误C2338:目标类型既不是std :: istream
able nor std::wistream
能够的
这看起来很简单,过去一小时我一直在桌子上打我的头.任何帮助,将不胜感激!
编辑:我正在使用Visual Studio 2013.
我最近回答了关于使用-lt
或-gt
使用字符串的问题.我的回答是基于我之前读过的一些内容,它说-lt
每次比较每个字符串中的一个字符,直到ASCII值不等于另一个字符串.此时结果(低/等/更大)决定.通过该逻辑,"Less" -lt "less"
应该返回True
因为L
具有比ASCII更低的ASCII字节值l
,但它不会:
[System.Text.Encoding]::ASCII.GetBytes("Less".ToCharArray())
76
101
115
115
[System.Text.Encoding]::ASCII.GetBytes("less".ToCharArray())
108
101
115
115
"Less" -lt "less"
False
Run Code Online (Sandbox Code Playgroud)
似乎我可能错过了一个关键部分:测试不区分大小写
#L has a lower ASCII-value than l. PS doesn't care. They're equal
"Less" -le "less"
True
#The last s has a lower ASCII-value than t. PS cares.
"Less" -lt "lest"
True
#T has a lower ASCII-value than t. PS doesn't care
"LesT" -lt "lest" …
Run Code Online (Sandbox Code Playgroud) c++ ×7
c# ×2
.net ×1
boost ×1
c++11 ×1
class ×1
complextype ×1
history ×1
istream ×1
lexical-cast ×1
operators ×1
powershell ×1
reference ×1
stl ×1