好友功能可访问性问题
Friend Function Accessibility Issue
我正在尝试为另一个 class 创建好友函数,但我当前的布局导致访问问题和 header-include 问题。
在我的项目中,我有两个文件:Class A 和 Class B。为了简洁起见,所有内容都内联在 header 文件中,因为它仍然展示了我的问题。
#ifndef CLASSA
#define CLASSA
#include "ClassB.h"
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
friend void testFriend(A in);
};
#endif
#pragma once
#ifndef CLASSB
#define CLASSB
#include <cstdio>
#include "ClassA.h"
class B {
public:
void testFriend(A in) {
printf("%d", in.x);
}
};
#endif
但是,对于这种设置,Visual Studio 认为 class A 的私有成员元素是不可访问的,尽管它是一个成员函数。此外,它们相互包含,最终会导致错误。不过,当这两个 classes 在同一个 header 文件中时,此设置工作正常。我怎样才能实现这样的设置,其中一个 class 有一个成员函数需要与另一个 class 成为朋友,同时让两个 class 分开 header 个文件。
friend void testFriend(A in);
与 B::testFriend
.
无关
您可以将整个 class 加为好友:
class A {
private:
int x;
public:
A(int x) : x(x) {}
friend class B;
};
class B {
public:
void testFriend(A a) { std::cout << a.x; }
};
宣言
friend void testFriend(A in);
使名为 testFriend
的非成员函数成为 class 的 friend
。它不会使 B::testFriend
成为 class A 的 friend
。
您可以通过将 B
设为 A
的 friend
来解决问题。这将只需要一个前向声明。
#ifndef CLASSA
#define CLASSA
// No need for this.
// #include "ClassB.h"
class B;
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
// This does not work.
// friend void testFriend(A in);
// Make B a friend of the class.
friend B;
};
#endif
现在,
class B {
public:
void testFriend(A in) {
printf("%d", in.x);
}
};
应该可以正常工作。
void testFriend(A in);
指的是全局范围内的一个函数。你需要的是void B::testFriend(A in);
。但是,这需要 B
已经声明和实施。
但是要在 B
中实现 testFriend
,必须声明 A
以便您可以使用 A
作为参数。另外,必须实现 A
才能使用 A.in
。如果您的 类 有单独的 headers 和实现,这应该不是问题。
但是,如果您决定在一个文件中执行此操作,则需要按以下顺序声明和实施:
class A;
class B {
public:
void testFriend(A in);
};
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
friend void B::testFriend(A in);
};
void B::testFriend(A in) {
printf("%d", in.x);
}
我正在尝试为另一个 class 创建好友函数,但我当前的布局导致访问问题和 header-include 问题。
在我的项目中,我有两个文件:Class A 和 Class B。为了简洁起见,所有内容都内联在 header 文件中,因为它仍然展示了我的问题。
#ifndef CLASSA
#define CLASSA
#include "ClassB.h"
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
friend void testFriend(A in);
};
#endif
#pragma once
#ifndef CLASSB
#define CLASSB
#include <cstdio>
#include "ClassA.h"
class B {
public:
void testFriend(A in) {
printf("%d", in.x);
}
};
#endif
但是,对于这种设置,Visual Studio 认为 class A 的私有成员元素是不可访问的,尽管它是一个成员函数。此外,它们相互包含,最终会导致错误。不过,当这两个 classes 在同一个 header 文件中时,此设置工作正常。我怎样才能实现这样的设置,其中一个 class 有一个成员函数需要与另一个 class 成为朋友,同时让两个 class 分开 header 个文件。
friend void testFriend(A in);
与 B::testFriend
.
您可以将整个 class 加为好友:
class A {
private:
int x;
public:
A(int x) : x(x) {}
friend class B;
};
class B {
public:
void testFriend(A a) { std::cout << a.x; }
};
宣言
friend void testFriend(A in);
使名为 testFriend
的非成员函数成为 class 的 friend
。它不会使 B::testFriend
成为 class A 的 friend
。
您可以通过将 B
设为 A
的 friend
来解决问题。这将只需要一个前向声明。
#ifndef CLASSA
#define CLASSA
// No need for this.
// #include "ClassB.h"
class B;
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
// This does not work.
// friend void testFriend(A in);
// Make B a friend of the class.
friend B;
};
#endif
现在,
class B {
public:
void testFriend(A in) {
printf("%d", in.x);
}
};
应该可以正常工作。
void testFriend(A in);
指的是全局范围内的一个函数。你需要的是void B::testFriend(A in);
。但是,这需要 B
已经声明和实施。
但是要在 B
中实现 testFriend
,必须声明 A
以便您可以使用 A
作为参数。另外,必须实现 A
才能使用 A.in
。如果您的 类 有单独的 headers 和实现,这应该不是问题。
但是,如果您决定在一个文件中执行此操作,则需要按以下顺序声明和实施:
class A;
class B {
public:
void testFriend(A in);
};
class A {
private:
int x;
public:
A(int x) {
this->x = x;
}
friend void B::testFriend(A in);
};
void B::testFriend(A in) {
printf("%d", in.x);
}