我在我的程序中制作了一个小内存泄漏查找程序,但我的方法是重载new和delete(以及new []和delete [])似乎没有做任何事情.
void* operator new (unsigned int size, const char* filename, int line)
{
void* ptr = new void[size];
memleakfinder.AddTrack(ptr,size,filename,line);
return ptr;
}
Run Code Online (Sandbox Code Playgroud)
我重载的方式new显示在上面的代码片段中.我想这是操作员返回void*的东西,但我不知道该怎么办.
是否有可以覆盖的所有ruby运算符的列表?(不是那些不能!)
考虑这个问题,这是关于以下不编译的代码:
std::vector<int> a, b;
std::cout << (std::ref(a) < std::ref(b));
Run Code Online (Sandbox Code Playgroud)
它不会编译,因为向量比较运算的vector都是非成员函数模板,隐式转换是不允许考虑.但是,如果运算符被写为非成员非模板,则friend函数:
template <class T, class Allocator = std::allocator<T>>
class vector {
// ...
friend bool operator<(const vector& lhs, const vector& rhs) {
// impl details
}
};
Run Code Online (Sandbox Code Playgroud)
那么这个版本operator<将由ADL找到并被选为最佳可行的重载,并且原始示例将被编译.鉴于此,是否有理由选择我们目前拥有的非成员函数模板,还是应该将其视为标准中的缺陷?
我正在创建一个双链表,并重载了operator =使列表上的另一个相等:
template<class T>
void operator=(const list<T>& lst)
{
clear();
copy(lst);
return;
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译时出现此错误:
container_def.h(74) : error C2801: 'operator =' must be a non-static member
Run Code Online (Sandbox Code Playgroud)
此外,如果有帮助,第74行是定义的最后一行,带有"}".
我试图将自定义类型指定为std :: map的键.这是我用作键的类型.
struct Foo
{
Foo(std::string s) : foo_value(s){}
bool operator<(const Foo& foo1) { return foo_value < foo1.foo_value; }
bool operator>(const Foo& foo1) { return foo_value > foo1.foo_value; }
std::string foo_value;
};
Run Code Online (Sandbox Code Playgroud)
当与std :: map一起使用时,我收到以下错误.
error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Foo' (or there is no acceptable conversion) c:\program files\microsoft visual studio 8\vc\include\functional 143
Run Code Online (Sandbox Code Playgroud)
如果我改变下面的结构,一切都有效.
struct Foo
{
Foo(std::string s) : foo_value(s) {}
friend bool …Run Code Online (Sandbox Code Playgroud) 这里是剧透......
我正在看#1 的答案,我必须承认我从来不知道重载决议是这种情况.但为什么会这样呢.在我的小脑袋里Derived.Foo(int)似乎是合乎逻辑的道路.
这个设计决定背后的逻辑是什么?
奖金时间!
此行为是C#规范,CLR实现还是编译器的结果?
有谁知道为什么istream对象可以用作bool表达式?例如:
ifstream input("tmp");
int iValue;
while (input >> iValue)
//do something;
Run Code Online (Sandbox Code Playgroud)
这里input >> iValue返回对ifstream对象的引用.我想知道为什么这个对象可以用作bool表达式.
我查看ifstream类,发现这可能是由于以下成员函数:
operator void * ( ) const;
Run Code Online (Sandbox Code Playgroud)
有关此功能的详细信息,请参见此处
如果是的话,有人可以向我解释这个功能吗?该函数的原型与通常的运算符重载声明不同.这个函数的返回类型是什么?
如果不是,那么ifstream对象可以用作bool表达式的原因是什么?
期待您的帮助!
程
我试图重载c ++运算符==但我得到一些错误...
错误C2662:'CombatEvent :: getType':无法将'this'指针从'const CombatEvent'转换为'CombatEvent&'
这个错误就在这一行
if (lhs.getType() == rhs.getType())
Run Code Online (Sandbox Code Playgroud)
看下面的代码:
class CombatEvent {
public:
CombatEvent(void);
~CombatEvent(void);
enum CombatEventType {
AttackingType,
...
LowResourcesType
};
CombatEventType getType();
BaseAgent* getAgent();
friend bool operator<(const CombatEvent& lhs, const CombatEvent& rhs) {
if (lhs.getType() == rhs.getType())
return true;
return false;
}
friend bool operator==(const CombatEvent& lhs, const CombatEvent& rhs) {
if (lhs.getType() == rhs.getType())
return true;
return false;
}
private:
UnitType unitType;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮忙吗?
我有一个话题,我很困惑,我需要详细说明.它是使用const版本和非const版本重载的运算符.
// non-const
double &operator[](int idx) {
if (idx < length && idx >= 0) {
return data[idx];
}
throw BoundsError();
}
Run Code Online (Sandbox Code Playgroud)
我理解这个函数的一部分,取一个索引并检查它的逻辑,返回类中数组数据的索引.还有一个具有相同主体的函数,但函数调用为
const double &operator[](int idx) const
Run Code Online (Sandbox Code Playgroud)
为什么我们需要两个版本?
此示例问题也可能有助于详细说明.下面的每个实例使用哪个版本?
Array a(3);
a[0] = 2.0;
a[1] = 3.3;
a[2] = a[0] + a[1];
Run Code Online (Sandbox Code Playgroud)
我的假设是只调用const版本,a[2]因为我们不想冒险修改a[0]或a[1].
谢谢你的帮助.
什么时候operator <<引用插入操作符,什么时候引用按位左移?
这将输出10,并operator <<指向左移.
cout << a.b() << a.a.b << endl;
Run Code Online (Sandbox Code Playgroud)
这将输出11,operator <<指的是插入运算符.
cout << a.b();
cout << a.a.b ;
Run Code Online (Sandbox Code Playgroud)
我很困惑,什么时候operator <<(使用时cout)会参考左移算子?
#include <iostream>
using namespace std;
class A {
public:
A() { a.a = a.b = 1; }
struct { int a, b; } a;
int b();
};
int A::b(){
int x=a.a;
a.a=a.b;
a.b=x;
return x;
};
int main(){
A a;
a.a.a = 0;
a.b(); …Run Code Online (Sandbox Code Playgroud) c++ ×8
c# ×1
class ×1
const ×1
istream ×1
new-operator ×1
non-static ×1
operators ×1
ruby ×1
stdmap ×1
syntax-error ×1
templates ×1