friend declaration block an external function access to the private section of a class

Posted by MiP on Stack Overflow See other posts from Stack Overflow or by MiP
Published on 2012-10-01T17:55:02Z Indexed on 2012/10/02 21:37 UTC
Read the original article Hit count: 202

Filed under:
|
|

I'm trying to force function caller from a specific class. For example this code bellow demonstrate my problem. I want to make 'use' function would be called only from class A. I'm using a global namespace all over the project.

a.h

#include "b.h"
namespace GLOBAL{

class A{
public:
    void doSomething(B);
}
}

a.cpp

#include "a.h"
using namespace GLOBAL;

void A::doSomething(B b){

    b.use();
}

b.h

namespace GLOBAL{

class B{
public:
    friend void GLOBAL::A::doSomething(B);
private:
    void use();
}

Compiler says:

‘GLOBAL::A’ has not been declared

‘void GLOBAL::B::use()’ is private

Can anyone help here ?

Thanks a lot,

Mike.

© Stack Overflow or respective owner

Related posts about c++

Related posts about namespaces