操作员重载中的行号 [英] Line number in an operator overloading

查看:102
本文介绍了操作员重载中的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想为may应用程序做一个调试器,处理运算符(+,-,/,*,...)

例如
(此示例已复制到: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref %2Fcplr318.htm )

 // 此示例说明了加号(+)运算符的重载.

#include   ><  > 
 使用 命名空间 std;

 class  complx
{
       double 真实的,
             形象
公共:
      complx( double  real =  0 ., double  imag =  0 .); // 构造函数
      complx运算符+( const  complx&) const ; // 运算符+()
};

// 定义构造函数
complx :: complx( double  r, double  i)
{
      实= r; imag = i;
}

// 定义重载+(加号)运算符
complx complx :: operator +( const  complx& c) const 
{
      结果
      result.real =(-> real + c.real);
      result.imag =( this -> imag + c.imag);
      cout<< " <<行<<恩德尔
      // 此行引用主函数中的行
      返回结果;
}

 int  main()
{
      complx x( 4  4 );
      y( 6  6 );
      混合z = x + y; // 调用complx :: operator +()-> ******这就是线******* 
} 



我想在主函数中打印加号运算符的行号.

我在Google上搜索了好几次,都没反应.
有什么方法可以获取发生该操作的行号?

在此先感谢

解决方案

这不是一件小事.可能您必须使用调试器API,在操作符中使用当前上下文,将调用堆栈移出一步,找到调用者地址,并使用调试符号打印源名称和行号.如果事实上您必须从内部操作员那里知道,您将永远不知道谁是呼叫者.实际程序有很多来源,您的运算符可以在任何.cpp/obj/lib/dll中使用.这些模块很可能会将您的运算符用作已编译的,热门的源代码.

但是,如果您可以控制运算符的所有使用,则可以使用断点,到达它们时这些断点不会停止.在那里,您可以在输出中打印消息或执行一些宏.
其他方法可能是使用C ++宏包装您的运算符,而不是直接使用它.当您控制所有源代码时,此方法也可用.


您知道______宏吗?


hi I want to make a little debugger for may application, dealing with operators (+, -, /, *, ...)

for exemple
(this example was copied in: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr318.htm)

// This example illustrates overloading the plus (+) operator.

#include <iostream>
using namespace std;

class complx
{
      double real,
             imag;
public:
      complx( double real = 0., double imag = 0.); // constructor
      complx operator+(const complx&) const;       // operator+()
};

// define constructor
complx::complx( double r, double i )
{
      real = r; imag = i;
}

// define overloaded + (plus) operator
complx complx::operator+ (const complx& c) const
{
      complx result;
      result.real = (this->real + c.real);
      result.imag = (this->imag + c.imag);
      cout << "the line when occurs this operation was: " << line << endl;
      //this line refers to the line in main function
      return result;
}

int main()
{
      complx x(4,4);
      complx y(6,6);
      complx z = x + y; // calls complx::operator+()  -> ******THIS IS THE LINE*******
}



i want to print the number of the line of the plus operator in main function.

I google it several times and nothing cames up.
there is any way to get the line number where occurred the operation?

thanks in advance

解决方案

This is not a trivial thing. Probably you have to use the debugger API, take the current context inside the operator, walk the callstack one step out, find the caller address and use debug symbols to print the name of the source and the line number. If fact you have to know that from inside operator you will never know who is the caller. Real programs have many sources, and your operator can be used in any .cpp/obj/lib/dll. Is very likely that the modules will use your operator as compiled one, hot having source.

But if you have control over all the uses of the operator, then you may just use breakpoints which do not stop when reaching them. There you may print messages in the output or execute some macros.
Other way could be using C++ macro wrapping your operator, instead of directly using it. This approach is also usable when you have control over all the source.


do you know about __LINE__ macro?


这篇关于操作员重载中的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆