C函数声明为CPP类内部的朋友 [英] declare a C function as friend inside CPP class

查看:129
本文介绍了C函数声明为CPP类内部的朋友的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个类的私有变量的C函数中。
我在做这样的事情。

I need to use private variable of a class inside a C function. I was doing something like this

class Helper
{
private:
    std::string name;
public:
    std::getName(){return name;}
friend extern "C" void initializeHelper();
};

但这种code段给人错误的字符串常量前不合格-ID
  为externC{

我不能够确定什么,我做错了。

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:

class A {
public:
  A() {}
private:
  friend void foo();
  int a;
};

这篇关于C函数声明为CPP类内部的朋友的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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