Finding current executable's path without /proc/self/exe

It seems to me that Linux has it easy with /proc/self/exe. But I'd like to know if there is a convenient way to find the current application's directory in C/C++ with cross-platform interfaces. I've seen some projects mucking around with argv[0], but it doesn't seem entirely reliable. If you ever had to support, say, Mac OS X, which doesn't have /proc/, what would you have

在没有/ proc / self / exe的情况下查找当前可执行文件的路径

在我看来,Linux使用/ proc / self / exe很容易。 但是我想知道是否有一种方便的方式来用C / C ++在跨平台接口中查找当前应用程序的目录。 我看到一些项目使用argv [0],但它看起来并不完全可靠。 如果您曾经需要支持Mac OS X(没有/ proc /),您会做什么? 使用#ifdefs隔离平台特定的代码(例如NSBundle)? 或者尝试从argv [0],$ PATH以及其他任何方式推断可执行文件的路径,从而在边缘情况下发现错误的风险? 一些

Determine if two rectangles overlap each other?

I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges will have slopes of 0 or infinity. I've tried to implement what is mentioned in this question but I am not having very much luck. My current

确定两个矩形是否相互重叠?

我正在尝试编写一个C ++程序,它接受来自用户的以下输入来构建矩形(2到5之间):高度,宽度,x-pos,y-pos。 所有这些矩形将平行于x和y轴存在,即它们的所有边将具有0或无穷大的斜率。 我试图实现这个问题中提到的内容,但我没有很好的运气。 我目前的实施做了以下工作: // Gets all the vertices for Rectangle 1 and stores them in an array -> arrRect1 // point 1 x: arrRect1[0], point 1 y: arrRect1[1] and so

Whats the cost of calling a virtual function in a non

I have a pure abstract base and two derived classes: struct B { virtual void foo() = 0; }; struct D1 : B { void foo() override { cout << "D1::foo()" << endl; } }; struct D2 : B { void foo() override { cout << "D1::foo()" << endl; } }; Does calling foo in Point A cost the same as a call to a non-virtual member function? Or is it more expensive than if D1 and D2 wouldn

调用一个非虚函数的成本是多少?

我有一个纯粹的抽象基础和两个派生类: struct B { virtual void foo() = 0; }; struct D1 : B { void foo() override { cout << "D1::foo()" << endl; } }; struct D2 : B { void foo() override { cout << "D1::foo()" << endl; } }; 在A点调用foo的花费与对非虚拟成员函数的调用相同吗? 还是比D1和D2不会来自B更昂贵? int main() { D1 d1; D2 d2; std::vector<B*> v = { &d1, &

How to do something every millisecond or better on Windows

This question is not about timing something accurately on Windows (XP or better), but rather about doing something very rapidly via callback or interrupt . I need to be doing something regularly every 1 millisecond, or preferably even every 100 microseconds. What I need to do is drive some assynchronous hardware (ethernet) at this rate to output a steady stream of packets to the network, and m

如何在Windows上每毫秒或更多时间做某些事情

这个问题不是关于在Windows(XP或更高版本)上准确计时的事情,而是通过回调或中断非常快速地做出某些事情。 我需要每1毫秒定期做一次,或者最好每100毫秒。 我需要做的是以这个速率驱动一些分布式硬件(以太网),以便向网络输出稳定的数据包流,并使该数据流看起来像常规和同步一样。 但是,如果问题可以从(以太网)设备中分离出来,那么知道一般答案是很好的。 在你说“甚至不考虑使用Windows !!!!”之前,稍微谈一下。

Can a C++ class member function template be virtual?

I have heard that C++ class member function templates can't be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function? Templates are all about the compiler generating code at compile-time . Virtual functions are all about the run-time system figuring out which function to call at run-time . Once the run-time system figure

C ++类成员函数模板可以是虚拟的吗?

我听说C ++类成员函数模板不能是虚拟的。 这是真的? 如果它们可以是虚拟的,那么可以使用这种功能的场景的例子是什么? 模板都是关于编译器在编译时生成代码的。 虚拟功能都是关于运行时系统确定在运行时调用哪个函数的。 一旦运行时系统计算出它需要调用一个模板化的虚拟函数,编译完成并且编译器不能再生成适当的实例。 因此您不能拥有虚拟成员函数模板。 但是,有几个强大而有趣的技术来源于多态和模板的结合,特

Why do we not have a virtual constructor in C++?

为什么C ++没有虚拟构造函数? Hear it from the horse's mouth:). From Bjarne Stroustrup's C++ Style and Technique FAQ Why don't we have virtual constructors? A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object y

为什么我们在C ++中没有虚拟构造函数?

为什么C ++没有虚拟构造函数? 从马的嘴里听到:)。 来自Bjarne Stroustrup的C ++风格和技巧常见问题为什么我们没有虚拟构造函数? 虚拟呼叫是在部分信息下完成工作的机制。 特别是,“虚拟”允许我们调用一个只知道任何接口的函数,而不知道对象的确切类型。 要创建一个对象,你需要完整的信息。 特别是,您需要知道您想要创建的确切类型。 因此,“调用构造函数”不能是虚拟的。 常见问题解答条目继续给出代码,以便在不

Can I call a base class's virtual function if I'm overriding it?

Say I have classes Foo and Bar set up like this: class Foo { public: int x; virtual void printStuff() { std::cout << x << std::endl; } }; class Bar : public Foo { public: int y; void printStuff() { // I would like to call Foo.printStuff() here... std::cout << y << std::endl; } }; As annotated in the code, I'd

如果我重写它,我可以调用基类的虚函数吗?

假设我有Foo和Bar这样的班级设置: class Foo { public: int x; virtual void printStuff() { std::cout << x << std::endl; } }; class Bar : public Foo { public: int y; void printStuff() { // I would like to call Foo.printStuff() here... std::cout << y << std::endl; } }; 正如代码中所注释的那样,我希望能够调用我重写的基

C++ static virtual members?

Is it possible in C++ to have a member function that is both static and virtual ? Apparently, there isn't a straightforward way to do it ( static virtual member(); is a compile error), but is there at least a way to achieve the same effect? IE: struct Object { struct TypeInformation; static virtual const TypeInformation &GetTypeInformation() const; }; struct SomeObject : p

C ++静态虚拟成员?

在C ++中有可能具有static和virtual的成员函数吗? 显然,没有一个简单的方法来做到这一点( static virtual member();是一个编译错误),但至少有一种方法来实现相同的效果? IE: struct Object { struct TypeInformation; static virtual const TypeInformation &GetTypeInformation() const; }; struct SomeObject : public Object { static virtual const TypeInformation &GetTypeInformation

Correctness of misaligned access in C++

From what I have read, misaligned access means mostly two things: you may get a performance loss you will lose atomicity of loads and stores that aligned access has Supposing that performance is not an issue and what I want from software is correctness, how bad is misaligned access? My understanding is that the x86 CPU will handle such accesses correctly but may have to do additional work

C ++中未对齐访问的正确性

从我读到的,错位的访问意味着大部分两件事: 您可能会收到性能损失 您将失去对齐访问所具有的加载和存储的原子性 假设性能不是问题,我想从软件中得到的结果是正确的,访问失调有多糟? 我的理解是,x86 CPU将正确处理这些访问,但可能需要做额外的工作来获取数据。 什么导致我问这个问题是用-fsanitize=undefined编译我的代码。 我有很多关于错位商店/货物的错误。 我不确定这是否是一个问题,因为: 只有在数据

C++ reading leftover data at the end of a file

I am taking input from a file in binary mode using C++; I read the data into unsigned ints, process them, and write them to another file. The problem is that sometimes, at the end of the file, there might be a little bit of data left that isn't large enough to fit into an int; in this case, I want to pad the end of the file with 0s and record how much padding was needed, until the data is

C ++读取文件末尾的剩余数据

我正在使用C ++以二进制模式从文件输入; 我将数据读入未签名的整数,处理它们,并将它们写入另一个文件。 问题在于,有时候,在文件末尾,可能会留下一点数据量不足以容纳int的数据; 在这种情况下,我想用0填充文件的末尾,并记录需要多少填充,直到数据足够大以填充无符号整数。 以下是我从文件中读取的内容: std::ifstream fin; fin.open('filename.whatever', std::ios::in | std::ios::binary); if(fin) { unsigne