Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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)

C++
// 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
Posted
Updated 6-Jul-12 12:31pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jul-12 18:41pm    
"complx"? Well...
--SA
lewax00 6-Jul-12 18:46pm    
There's none I'm aware of, and honestly it makes no sense. The line number is an artifact of the source code, and has nothing to do with the final program while it's executing.
Sergey Alexandrovich Kryukov 6-Jul-12 22:25pm    
Right.
--SA

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.
 
Share this answer
 
Comments
Filipe Marques 10-Jul-12 6:39am    
thanks for your answer.
your answer made me re-thinking of the approach of my problem. i really appreciate it. :)
best regards
Filipe
do you know about __LINE__ macro?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Jul-12 22:26pm    
And..? :-)
--SA
Mohibur Rashid 6-Jul-12 22:30pm    
i expect him to find out by himself
Filipe Marques 7-Jul-12 9:33am    
thanks for your answer
yes, I know about __LINE__ macro and I tried a lot of combinations with it, like in 'new' operator that i found in some forums, but I think the best way is to use a TRACE macro or something like that.
best regards, Filipe

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900