在C中使用C + +命名空间 [英] using C++ with namespace in C
问题描述
在Linux上使用Eclpse我已经定义了一个名为ABC(ABC.hpp)的C ++类:
#ifndef ABC_HPP_
#define ABC_HPP_
#include< stdio.h>
//命名空间my {< ---- COMMENTED OUT
class ABC {
private:
int m_number;
public:
ABC(int p_number);
void doSomething();
virtual〜ABC();
};
//} / *命名空间my * /< ---- COMMENTED OUT
#endif / * ABC_HPP_ * /
,其实现是(ABC.cpp):
#includeABC.hpp
// using namespace my; < ---- COMMENTTED OUT
ABC :: ABC(int p_number){
this-> m_number = p_number;
}
ABC ::〜ABC(){
this-> m_number = -1;
}
void ABC :: doSomething(){
printf(do something(%d)\\\
,this-> m_number);
}
为了在C程序中使用这个类,我创建了一个包含这些方法的图层(ABCwrapper.h):
typedef void CABC;
#ifdef __cplusplus
externC{
#endif
CABC * create_abc
void call_abc_methods(const CABC * p_abc);
void destroy_abc(CABC * p_abc);
#ifdef __cplusplus
} // externC
#endif
和
#includeABC.hpp
#includeABCWrapper.h
externC{
CABC * create_abc(){
ABC * abc = new ABC
return(ABC *)abc;
}
void call_abc_methods(const CABC * p_abc){
ABC * abc =(ABC *)p_abc;
abc-> doSomething();
}
void destroy_abc(CABC * p_abc){
ABC * abc =(ABC *)p_abc;
delete abc;
}
}
ABC类实例。但是在名称空间中定义ABC类,我们说我的呢?如果我删除所有命名空间行的注释符号,IDE抱怨说类型ABC'无法解析。
如果我想扩展我的C +库我必须使用命名空间为我的类,但然后我不知道如何使用在包装。
SK
ABCWrapper.cpp
/ code>行,添加:
或
using namespace my;
john的建议(替换 ABC
in
ABCWrapper.cpp
)也可以工作。
Using Eclpse on Linux I have defined a C++ class, named ABC (ABC.hpp):
#ifndef ABC_HPP_
#define ABC_HPP_
#include <stdio.h>
// namespace my { <---- COMMENTED OUT
class ABC {
private:
int m_number;
public:
ABC(int p_number);
void doSomething();
virtual ~ABC();
};
// } /* namespace my */ <---- COMMENTED OUT
#endif /* ABC_HPP_ */
and its implementation is (ABC.cpp):
#include "ABC.hpp"
// using namespace my; <---- COMMENTED OUT
ABC::ABC(int p_number) {
this->m_number = p_number;
}
ABC::~ABC() {
this->m_number = -1;
}
void ABC::doSomething() {
printf("doing something (%d)\n", this->m_number);
}
To use this class in C programs, I have created a layer containing these methods (ABCwrapper.h):
typedef void CABC;
#ifdef __cplusplus
extern "C" {
#endif
CABC* create_abc();
void call_abc_methods(const CABC *p_abc);
void destroy_abc(CABC *p_abc);
#ifdef __cplusplus
} // extern "C"
#endif
and
#include "ABC.hpp"
#include "ABCWrapper.h"
extern "C" {
CABC* create_abc() {
ABC* abc = new ABC();
return (ABC*)abc;
}
void call_abc_methods(const CABC *p_abc) {
ABC* abc = (ABC*)p_abc;
abc->doSomething();
}
void destroy_abc(CABC *p_abc) {
ABC* abc = (ABC*)p_abc;
delete abc;
}
}
That's fine and I can use the ABC class instance. But what about the define the ABC class in a name space, let's say "my"? If I remove the comment signs from all name space lines the IDE complains saying that the "Type 'ABC' could not be resolved".
If I want to extend my C++ library I have to use name space for my classes but then I don't know how to use in wrappers. Please, help me to solve this mystery.
Thank you.
SK
In ABCWrapper.cpp
, above the extern "C" {
line, add:
using my::ABC;
or
using namespace my;
john's suggestion (replace all instances of ABC
with my::ABC
in ABCWrapper.cpp
) also works.
这篇关于在C中使用C + +命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!