简体   繁体   English

如何在c ++的构造函数中使用__FILE__和__LINE__作为默认参数?

[英]How do I use __FILE__ and __LINE__ as default parameters in a constructor in c++?

I'm trying to use the __FILE__ and __LINE__ macros in a constructor as default parameters, but I can't seem to get the macros to use the right files. 我试图在构造函数中使用__FILE____LINE__宏作为默认参数,但我似乎无法让宏使用正确的文件。 They keep expanding from my header file. 他们不断扩展我的头文件。

In more detail: I'd like to have the file and line number from where an object is instantiated as members of my class. 更详细:我想要将对象实例化为我的类成员的文件和行号。 But I don't want to have to go and put the parameters in by hand every time I want to use the objects. 但是每次我想使用这些对象时,我都不想手动输入参数。 I know there's a way to do this, but I can't for the life of me figure it out. 我知道有办法做到这一点,但我不能为我的生活弄清楚。 What I'm currently doing is 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 get expanded as if they're from my header file, not the actual location I use the mnNumber. 但是, FILELINE会扩展,好像它们来自我的头文件,而不是我使用mnNumber的实际位置。

To answer the question of why I would like to do this, I want to have the code read it's own codepage. 要回答我为什么要这样做的问题,我想让代码读取它自己的代码页。 The particular values that I use are being registered in a manager, and their value is allowed to be edited by the end user. 我使用的特定值正在管理器中注册,并且最终用户可以编辑它们的值。 When the end user is done editing the value, the value is written back into the code page. 当最终用户完成编辑值后,该值将被写回代码页。 So, I need to know where the value came from. 所以,我需要知道价值来自哪里。 I also allow the end user to say that they'll never need to edit this value again, and when they click that button, the value is converted from an mnNumber back into a float, and the type on the codepage is rewritten as a float. 我还允许最终用户说他们永远不需要再次编辑这个值,当他们单击该按钮时,该值将从mnNumber转换回float,并且代码页上的类型将被重写为float 。 Or, will be...hopefully. 或者,希望......

Any advice for me? 对我有什么建议吗?

You can do this with the preprocessor. 您可以使用预处理器执行此操作。 Create a macro which expands to __LINE__ and use it: 创建一个扩展为__LINE__并使用它的宏:

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";
}       

你不能这样做 - 这两个宏在遇到它们时被预处理器代替,所以它们将被交换到头文件名和亚麻。

The OP wrote in an edit: OP在编辑中写道:

It wasn't easy, but R. Martinho Fernandes set me on the right path. 这并不容易,但R. Martinho Fernandes让我走上了正确的道路。 And I don't think it's threadsafe, but it works so far. 而且我不认为这是线程安全的,但它到目前为止是有效的。

What I wanted was the ability to track and update floats just by changing the type to mnFloat. 我想要的是只需将类型更改为mnFloat就可以跟踪和更新浮点数。 And I set up a define that calls a function in my manager to add the file, line, and function names, then changes the float to my special type. 我设置了一个定义,在我的管理器中调用一个函数来添加文件,行和函数名称,然后将浮点数更改为我的特殊类型。 Inside the manager they're all linked together with an ID. 在经理内部,他们都与一个ID链接在一起。 When I call the register function, I create an object internally that I store. 当我调用register函数时,我在内部创建了一个存储的对象。 On the same line my special type is also created, and it registers itself with the manager. 在同一行上,我的特殊类型也被创建,并且它与管理器一起注册。 Both objects use the same kind of ID system (ID's get generated by copying from a static number that I increment each time a new object is created). 两个对象都使用相同类型的ID系统(通过从每次创建新对象时增加的静态数字进行复制来生成ID)。 Since they appear on the same code page, the ID's are always the same, and never get out of sync. 由于它们出现在同一代码页上,因此ID始终相同,并且永远不会失去同步。 Assuming I don't go multithreaded, I suppose. 我想,假设我没有多线程。 It feels like cheating, but it works :) 这感觉就像作弊,但它的工作:)

Here's how it works. 这是它的工作原理。 I take this: 我接受这个:

 float test = 0.5; 

And I change it to this: 我把它改成了这个:

 mnFloat test = 0.5; 

In my header file, mnFloat is defined like so: 在我的头文件中,mnFloat的定义如下:

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

So, the codepage changes to two instructions on that line, and the line number doesn't increment. 因此,代码页会更改为该行上的两条指令,并且行号不会递增。 And it works! 它的工作原理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM