C++继续让我感到惊讶.今天我发现了 - >*运算符.它是可重载的,但我不知道如何调用它.我设法在课堂上重载它,但我不知道如何调用它.
struct B { int a; };
struct A
{
typedef int (A::*a_func)(void);
B *p;
int a,b,c;
A() { a=0; }
A(int bb) { b=b; c=b; }
int operator + (int a) { return 2; }
int operator ->* (a_func a) { return 99; }
int operator ->* (int a) { return 94; }
int operator * (int a) { return 2; }
B* operator -> () { return p; }
int ff() { return 4; }
}; …Run Code Online (Sandbox Code Playgroud) 为什么C#需要运算符重载是静态方法而不是成员函数(如C++)?(也许更具体地说:这个决定的设计动机是什么?)
我试图像这样实现一个泛型运算符:
class Foo
{
public static T operator +<T>(T a, T b)
{
// Do something with a and b that makes sense for operator + here
}
}
Run Code Online (Sandbox Code Playgroud)
我真正要做的就是优雅地处理继承.使用Foo中的标准运算符+,其中T代替"Foo",如果有人来自Foo(比如Bar继承Foo),那么Bar + Bar操作仍将返回Foo.我希望用泛型运算符+来解决这个问题,但是我只是得到了上面的语法错误(在<)让我相信这样的代码是不合法的.
有没有办法制作通用运营商?
我有一个课我希望能够比较平等.这个类很大(它包含一个位图图像),我会多次比较它,所以为了提高效率,我要对数据进行哈希处理,只检查哈希是否匹配.此外,我将只比较我的对象的一小部分,所以我只是在第一次完成相等性检查时计算哈希值,然后使用存储的值进行后续调用.
class Foo
{
public:
Foo(int data) : fooData(data), notHashed(true) {}
private:
void calculateHash()
{
hash = 0; // Replace with hashing algorithm
notHashed = false;
}
int getHash()
{
if (notHashed) calculateHash();
return hash;
}
inline friend bool operator==(Foo& lhs, Foo& rhs)
{
if (lhs.getHash() == rhs.getHash())
{
return (lhs.fooData == rhs.fooData);
}
else return false;
}
int fooData;
int hash;
bool notHashed;
};
Run Code Online (Sandbox Code Playgroud)
根据这个答案的指导,平等运算符的规范形式是:
inline bool operator==(const X& lhs, const X& rhs);
此外,操作符重载以下一般建议 …
看看这些功能签名:
class Number {
public:
Number& operator++ (); // prefix ++
Number operator++ (int); // postfix ++
};
Run Code Online (Sandbox Code Playgroud)
前缀不带任何参数,但后缀确实如此.为什么?我认为我们可以用不同的返回类型识别它们.
c++ language-design operator-overloading prefix postfix-operator
C++允许超载operator new-全局和每个类-通常operator new,operator new[]所使用new[]的语句和位置operator new分开.
这三个中的前两个通常因使用自定义分配器和添加跟踪而过载.但是放置operator new似乎非常简单 - 它实际上什么也没做.例如,在Visual C++中,默认实现只返回传递给调用的地址:
//from new.h
inline void* operator new( size_t, void* where )
{
return where;
}
Run Code Online (Sandbox Code Playgroud)
它还能做什么?为什么以及如何明智地超载放置operator new?
当我们重载类的new运算符时,我们将该函数声明为成员函数.例如:
class OpNew {
public:
OpNew() { cout << "OpNew::OpNew()" << endl;}
void* operator new(size_t sz) {
cout << "OpNew::new: "
<< sz << " bytes" << endl;
return ::new char[sz];
}
};
Run Code Online (Sandbox Code Playgroud)
声明如何在幕后OpNew *obj = new OpNew工作?因为重载new是OpNew类的成员而不是静态的.那么编译器如何确保对new成员函数的调用成功呢?
可能重复:有
什么合理的理由使一元运算符超载?
我刚看了这个问题,我不禁想知道:
为什么有人可能想要重载&("address-of")运算符?
some_class* operator&() const { return address_of_object; }
有没有合法的用例?
是否有适用于一元和运营商的特殊规则?
例如,代码:
#include <iostream>
struct X
{
X() {}
void* operator &() { return NULL; }
};
int main()
{
const X x;
std::cout << &x << std::endl;
X y;
std::cout << &y;
}
Run Code Online (Sandbox Code Playgroud)
产生输出
0xbfbccb33
0
Run Code Online (Sandbox Code Playgroud)
我知道这会像这样编译和运行,因为之前我曾在这里进行过讨论,但我不知道这一点,我原本预计这会无法编译,因为operator &没有声明const.
因此,operator &() const无论是否operator &()过载,编译器似乎都会生成.很好,这很有意义,尤其是样本和输出.
问题是标准中详细说明了这种行为在哪里?
我不是在寻找重复我在问题中已经说明的答案的答案,所以请不要解释我的重载操作符是如何在一个const对象上调用的,因为我已经知道了.
看一下我的同事的一些代码,我得到了以下内容:
friend bool operator==<>(ValueIter<Type> const &rhs, ValueIter<Type> const &lhs);
Run Code Online (Sandbox Code Playgroud)
它在模板类中声明:
template<typename Type>
class ValueIter: public std::iterator<std::bidirectional_iterator_tag, Type>
Run Code Online (Sandbox Code Playgroud)
有人能告诉我==<>符号表示什么吗?我希望它与!=运营商有关.
c++ ×9
c# ×2
c++11 ×1
comparison ×1
const ×1
equality ×1
generics ×1
new-operator ×1
overloading ×1
prefix ×1
static ×1
templates ×1
visual-c++ ×1