linux- windows交叉c ++应用程序 [英] linux- windows cross c++ application

查看:168
本文介绍了linux- windows交叉c ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个必须在Linux和Windows上运行的应用程序。
我有一个对象称为obj,我想在代码中使用它,它在Linux和Windows上有不同的行为。所以我继承aaa并调用WindowsObj for Windows对象和LinuxObj for Linux对象。

I'm developing an application which has to run on Linux and Windows. I have an object called obj which I want to use in the code and it has different behavior on Linux and Windows. so I inherit aaa and called WindowsObj for Windows object and LinuxObj for Linux object.

我的问题是:如何在代码中使用这个对象?我需要写什么,它将为Linux和Windows运行?

My question is: How to use this object in the code? what do I have to write that it will run both for Linux and Windows?

对于swiching类型,我使用typedef像:

For swiching types I use typedef like:

typedef uint32_t       DWORD;

但是我必须为对象使用什么?
我想写这个代码:

but what do I have to use for objects? I want to write this code:

tr1::shared_ptr<WindowsObj> windowsobj (new WindowsObj(parameter));
tr1::shared_ptr<LinuxObj> linuxobj (new LinuxObj(parameter));

有什么想法吗?

推荐答案

同样的事情:)

class _object
{
};

class WindowsObject : public _object
{
};

class LinuxObject  public _object
{
};

#if defined(WIN32)
typedef WindowsObject Object;
#else
typedef LinuxObject Object;
#endif

Object myObject;

编辑:当然,WindowsObject和LinuxObject公开的接口必须是相同的。在这个例子中, _object 将是一个定义接口的抽象基类,然后LinuxObject和WindowsObject将实现这个接口,在其实现中隐藏平台特定的东西

Naturally, the interface that WindowsObject and LinuxObject expose must be the same. In this example, _object would be an abstract base-class that defined the interface, and LinuxObject and WindowsObject would then implement this interface, hiding away the platform-specific stuff in their implementation files.

示例

_object.h

_object.h

class _object
{
public:
    virtual void doSomething() = 0;
}; // eo class _object

WindowsObject.h

WindowsObject.h

#include "_object.h"

class WindowsObject : public _object
{ 
public:
    virtual void doSomething();
}; // eo class WindowsObject

WindowsObject.cpp

WindowsObject.cpp

#if defined(WIN32)
#include <windows.h>

void WindowsObject::doSomething()
{
    // do something totally reliant on windows here
}; // eo doSomething
#endif

然后你也会这样做 LinuxObject.h LinuxObject.cpp ,后者具有完全不同的预处理器指令。例如 #if defined(UNIX)或某些此类风格。注意 WIN32 围绕实现保护。然后你会有一些核心头文件,你可以使用:

Then you would do the same for LinuxObject.h and LinuxObject.cpp, the latter having completely different preprocessor instructions. e.g, #if defined(UNIX) or some such flavor. Note the WIN32 guards around the implementation. Then you'd have some core header file you'd use:

#if defined(WIN32)
#include "WindowsObject.h"
typedef WindowsObject Object;
#else
#include "LinuxObject.h"
typedef LinuxObject Object;
#endif

现在,在您的程序中

Object a;
a.doSomething();

值得注意的是,如果只是复杂对象中不同的奇数行代码在初始化,初始化调用,销毁)你可能会更好地与一个单一的平台无关的对象,并在实现中放置守卫。当有巨大的差异时,这个解决方案更有意义。

It's worth noting that, if it's just the odd line of code that differs in your complex object (like a init call at initialisation, destruction) you might be better off with a single platform-agnostic Object and put guards in the implementation. This solution makes more sense when there are huge differences.

这篇关于linux- windows交叉c ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆