English 中文(简体)
允许C++对法典作某些改动
原标题:Parsing C++ to make some changes in the code
  • 时间:2011-06-17 17:01:40
  •  标签:
  • c++
  • parsing

我想写一个小的工具,采用C++方案(单一文件),发现“主要”功能,并给它增加2个功能,一个在开始时,一个在最后。

如何做到这一点? 我能否使用g++的教区机制(或任何其他教区)?

最佳回答

正如一些评论家所建议的,请允许我提出我的想法,作为答案:

基本上,这一概念是:

... original .cpp file ...

#include <yourHeader>
namespace { 
  SpecialClass  specialClassInstance;
}

在<条码>特殊目录中,例如:

class SpecialClass {
  public:
    SpecialClass() {
        firstFunction();
    }

    ~SpecialClass() {
        secondFunction();
    }
}

这样,你就不需要把C++的档案整理起来。 由于你正在宣布全球,其建筑商将在<代码>main之前开张,其承载人将在<代码>main/code>后开张。

The downside is that you don t get to know the relative order of when your global is constructed compared to others. So if you need to guarantee that firstFunction is called before any other constructor elsewhere in the entire program, you re out of luck.

问题回答

如果你想要使其坚固,则使用 图书馆

我听到海合会教区既难以使用,也更难以在不援引整个工具链的情况下使用。 我将审判部族C/C++教区(和在

Adding a function at the beginning of main() and at the end of main() is a bad idea. What if someone calls return in the middle?.
A better idea is to instantiate a class at the beginning of main() and let that class destructor do the call function you want called at the end. This would ensure that that function always get called.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签