Declare C function as friend inside CPP class

I need to use a private class variable 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();
};

      

but this code segment gives error unqualified-id before string constant extern "C" {

I cannot determine what I am doing wrong here.

+2


source to share


1 answer


Just forward this function before the class:

extern "C" void foo();

      



Then you can use it in your friend's declaration:

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

      

+6


source







All Articles