[英]How to access a variable which is declare in namespace into another cpp file
[英]How to access a structure variable which is inside a cpp class,from c?
示例例如:
messageStruct.hpp
class MessageStructure_t{
public:
struct MsgData_t {
float a;
int i;
}__attribute__((packed))msgdata_m;
};//classs end
我的項目Application.c中有一個文件。 我需要在這里訪問結構變量。 兩者是不同的,一個.hpp和另一個.c
我怎樣才能做到這一點 ?
希望您的關注。
您可以在單獨的頭文件msg_data.h中定義該結構,然后將其包含在兩個項目中。 如果需要,您可能必須將CaseS的MessageStructure_t指針鍵入MsgData_t。
因此MsgData.h:
struct MsgData_t {
float a;
int i;
}__attribute__((packed));
messageStruct.hpp:
#include "MsgData.h"
class MessageStructure_t {
public:
MsgData_t msgdata_m;
}
Appliaction.c:
#include "MsgData.h"
//...
當您想從C訪問C ++類及其對象時,周圍有一些眾所周知的模式。 谷歌為他們。
一個簡單的方法是將其包裝在OO C中:
typedef void* my_handle_t;
handle_t create(void); // returns address of new'ed object
void destroy(handle_t); // deletes object
MsgData_t* get_data(handle_t); // returns address of data in object
MsgData_t
的問題是如何使MsgData_t
可以訪問MsgData_t
。我看到三種可能性:
#ifndef __cplusplus
),以使C解析器可訪問C ++頭( #ifndef __cplusplus
,但避免了#2的代碼重復) 我認為最好的方法是創建一個外部“ C”函數來訪問該結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.