Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ “如何超载”;删除“;C+中的运算符+;打印使用该行的文件的行和名称?_C++_Compiler Errors_Macros_Overloading_Delete Operator - Fatal编程技术网

C++ “如何超载”;删除“;C+中的运算符+;打印使用该行的文件的行和名称?

C++ “如何超载”;删除“;C+中的运算符+;打印使用该行的文件的行和名称?,c++,compiler-errors,macros,overloading,delete-operator,C++,Compiler Errors,Macros,Overloading,Delete Operator,我试图重载“delete”操作符,以便在控制台中打印使用它的文件的行号和名称。我尝试了以下方法: #include <iostream> void operator delete(void* adress, char* file, int line) { printf("%s: line %d -> ", file, line); delete(adress); } #define delete delete(__FILE__, __LI

我试图重载“delete”操作符,以便在控制台中打印使用它的文件的行号和名称。我尝试了以下方法:

#include <iostream>

void operator delete(void* adress, char* file, int line) {
    printf("%s: line %d -> ", file, line);
    delete(adress);
}

#define delete delete(__FILE__, __LINE__)

int main() {
    int* x = new int;
    delete x;
}
我找不到任何解决办法。我怎样才能解决这个问题

编辑:对于“新”操作符,这里有一些类似的功能:

#include <iostream>

using namespace std;

void* operator new(std::size_t size) {
    void* ptr = malloc(size);
    /* ... */
    return ptr;
}

void* operator new(std::size_t size, const int line, const char* const file) {
    printf("%s: line %d -> ", file, line);
    // BOTH RETURNS WORK
    //return malloc(size);
    return ::operator new(size);
}

#define new new(__LINE__, __FILE__)

int main() {
    int* x = new int;
    //delete x;
}
#包括
使用名称空间std;
void*运算符新(标准::大小\u t大小){
void*ptr=malloc(尺寸);
/* ... */
返回ptr;
}
void*运算符新(std::size\u t size,常量int行,常量char*const文件){
printf(“%s:行%d->”,文件,行);
//两人都返回工作岗位
//返回malloc(大小);
返回::运算符新(大小);
}
#定义新的(行、文件)
int main(){
int*x=新的int;
//删除x;
}

这是一个非常糟糕的解决方案,但可能对您的具体情况有所帮助:

#define delete cout << __FILE__ << " " << __LINE__ << endl, delete
然后必须使用类似的方法:

class SomeClass
{
  ...
#undef delete
  void fn() = delete;
#define delete cout << __FILE__ << " " << __LINE__ << endl, delete
  ...
};
class-SomeClass
{
...
#未定义删除
void fn()=删除;

#定义删除无法尝试使用-E编译,请查看输出是否有意义。请尝试在大脑中展开宏。
delete x;
-->
delete(\uuu文件,\uu行)x;
。它在语法上有效吗?定义一个名为关键字的宏会产生未定义的行为。我知道它在语法上无效,但我可以向您保证,我已经为“new”做了类似(几乎相同)的事情运算符,它工作得很好。我考虑了语法/定义,但找不到任何解决方案,但它同样适用于“new”。@AndreiBădescu宏添加的内容是相同的(尽管顺序相反),但其中一个使用关键字
new
,而另一个使用关键字
delete
。这使得它们非常不同,因为关键字具有不同的语法规则。
if(something\u或其他)删除其他内容;
——这将以眼泪结束。好的,将
更改为
,如果我有一个特殊的成员函数=delete,它将不工作;你是对的,在这种情况下它将不工作。
class SomeClass
{
  ...
  void fn() = delete;  // This will not compile
  ...
};
class SomeClass
{
  ...
#undef delete
  void fn() = delete;
#define delete cout << __FILE__ << " " << __LINE__ << endl, delete
  ...
};