9

I'm writing a library in C++. I have two classes in my library, A and B. I want to hide the A() constructor from any code that references my library. I also want class B to be able to call the A() constructor.

I come from a C# background and remember little of my C++. In C#, I would simply declare the A() constructor as internal. I've read that the closest way to do this in C++ is a combination of friend declarations and forward-declarations. How do I do this? Here are my three files below:

A.h:

#pragma once
class A
{
    private:
        A();
};

B.h

#pragma once
class A;
class B
{
    public:
        A createA();
};

B.cpp:

#include "A.h"
#include "B.h"

A B::createA()
{
    A result; //cannot access private member declare in class 'A'
    return result;
}

I've tried adding this to A.h:

public: friend A createA();

I've instead tried adding this to A.h with a corresponding forward declaration:

public: friend A B::createA();

I've instead tried adding and extern class B; to A.h and making B a class like this:

public: friend class B;

I'm at a loss.

I think this might be easier if I have the B::createA() function return a pointer to an A object rather than an A object directly, but that won't do in my case. I am emulating a closed API and the API call returns an A object rather than a pointer.

4

4 回答 4

6

您可能只需要从第三次尝试中删除“extern”即可将其转换为正确的前向声明。尝试:

啊:

#pragma once
class B;
class A
{
    friend class B;
private:
    A();
};
于 2013-09-09T18:37:50.093 回答
1

除非绝对必要,否则您应该自己A构造(或拥有创建的工厂A)。如果你真的想B这样做:

class B; // foward declared

class A
{
private:
    A() {}
    friend class B;
};

class B
{
public:
    A CreateA()
    {
        A a;
        return a;
    }
};

int main()
{
    B b;
    A a = b.CreateA();
    return 0;
}

注意:您必须在将其声明为inB之前转发声明。friendA

如果您只想将功能作为朋友:

class A;

class B
{
public:
    A CreateA();
};

class A
{
private:
    A() {}
    friend class A B::CreateA();
};

A B::CreateA()
{
    A a;
    return a;
}

int main()
{
    B b;
    A a = b.CreateA();
    return 0;
}
于 2013-09-09T18:38:44.493 回答
1

您不需要external关键字。让它简单:

// In A.h

class B;  // Forward declaration

class A
{
    friend class B; // Make all the class B friend
    A();
};

// In B.h

class B
{
public:
    A createA() {}
};

活生生的例子

于 2013-09-09T18:39:46.213 回答
0

你可以让 B 成为 A 的朋友:

class A
{
private:
   A();
   friend class B;
};
于 2013-09-09T18:37:23.920 回答