[英]declare a C function as friend inside CPP class
I need to use private variable of a class inside a C function. 我需要在C函数中使用类的私有变量。 I was doing something like this
我正在做这样的事情
class Helper
{
private:
std::string name;
public:
std::getName(){return name;}
friend extern "C" void initializeHelper();
};
but this code segment gives error unqualified-id before string constant extern "C" {
但此代码段
unqualified-id before string constant extern "C" {
给出错误unqualified-id before string constant extern "C" {
I am not able to identify what I am doing wrong here. 我无法确定我在这里做错了什么。
Just forward-declare this function before your class: 只需在课前向前声明此函数:
extern "C" void foo();
Then you can use it in friend-declaration: 然后你可以在friend-declaration中使用它:
class A {
public:
A() {}
private:
friend void foo();
int a;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.