How to use __FILE__ and __LINE__ as default parameters in constructor in C ++?

I am trying to use the __FILE__ and __LINE__ in the constructor as the default parameters, but I cannot force the macros to use the correct files. They continue to expand from my header file.

In more detail: I would like to have a file number and a line from which an object instance is created as members of my class. But I don’t want me to have to enter the parameters manually, every time I want to use objects. I know that there is a way to do this, but I cannot make life understand me. I am currently doing the following:

In my header file:

 mnNumber( float x, const char* filename = __FILE__, int linenumber = __LINE__ ): value( x ), mFileName( filename ), mFunctionName( nullptr ), mLineNumber( linenumber ), mID( 0 ) 

But FILE and LINE expand as if they were from my header file, not the actual location that I am using mnNumber.

To answer the question why I would like to do this, I want the code to read its own code page. The specific values ​​that I use are recorded in the manager, and their value is allowed to be edited by the end user. When the end user edits the value, the value will be written back to the code page. So, I need to know where this value came from. I also allow the end user to say that they no longer have to edit this value, and when they click this button, the value is converted from mnNumber back to float, and the type in the code page is rewritten as float, Or, let's hope.

Any tips for me?

+6
source share
3 answers

You can do this with a preprocessor. Create a macro that expands to __LINE__ and uses it:

 struct S { S(int line, const std::string& file) : line(line), file(file) { } std::string file; int line; }; #define SCons() S(__LINE__, __FILE__) int main () { S s1 = SCons(); S s2 = SCons(); std::cout << s1.line << "\n"; std::cout << s2.line << "\n"; } 
+6
source

You cannot do this - these two macros are replaced by the preprocessor when it encounters them, so they will be replaced by the header file name and linenumber.

+2
source

OP wrote in edit:

It was not easy, but R. Martigno Fernandez set me on the right track. And I don’t think it is thread safe, but it still works.

What I wanted was the ability to track and update the float by simply changing the type to mnFloat. And I set a definition that calls a function in my manager to add the names of files, strings and functions, and then changes the float to my special type. Inside the manager, they are all associated with the identifier. When I call the register function, I create the object that I stored. My special type is created on the same line, and it is registered with the manager. Both objects use the same identifier system (the ID is generated by copying from a static number, which I increment each time I create a new object). Since they appear on the same code page, the identifier is always the same and never goes out of sync. Assuming I don't become multithreaded, I suppose. This seems like a hoax, but it works :)

Here's how it works. I accept this:

 float test = 0.5; 

And I change it to this:

 mnFloat test = 0.5; 

In my header file, mnFloat is defined as follows:

 #define mnFloat myManager::getInstance()->register(__FILE__,__FUNCTION__,__LINE__);mnNumber 

So, the code page is changed to two commands in this line, and the line number does not increase. And it works!

0
source

Source: https://habr.com/ru/post/920551/


All Articles