问题描述
我想保持func变量私有,所以我试图与它成为朋友,下面的代码不能在VS2017中编译,错误C2248:'wrapper :: _ Instance :: func':无法访问私有在'wrapper :: _ Instance'类中声明的成员。
I want to keep func variable private, so I'm trying to befriend it and the code below doesn't compile in VS2017 with an error C2248: 'wrapper::_Instance::func': cannot access private member declared in class 'wrapper::_Instance'.
如何使其有效?
#include "pch.h"
#include <iostream>
class wrapper
{
private:
// ...
public:
template <class T>
void FetchStructList(T);
// instance
class _Instance {
int (*func)(const char*, int*, double*);
public:
_Instance() {
// func = &externalFunction;
};
template <class T>
// friend void wrapper::FetchStructList<T>(T); doesn't compile either
friend void wrapper::FetchStructList(T);
} Instance;
void FetchInstanceExtentions() { FetchStructList(Instance); };
};
template <class T>
void wrapper::FetchStructList(T s)
{
int size = 0;
int res = 0;
if constexpr (std::is_same<T, _Instance>::value)
res = s.func(nullptr, &size, nullptr);
// ...
else {
// ...
return;
}
//...
}
int main()
{
wrapper w;
w.FetchInstanceExtentions();
}
推荐答案
单击此按钮并选择报告问题。
Click this and select Report a Problem.
在Visual中Studio Feedback窗口,确保您无法找到具有相同内容的反馈。如果您不能将此报告为错误。确保您提供代码作为问题的示例,并确保您提到这应编译
并且GCC / Clang接受此作为有效代码。
In the Visual Studio Feedback window that make sure that you can't find feedback with the same content. If you can't then report this as a bug. Make sure you provide the code as an example of the problem and make sure that you mention that this should compile and GCC/Clang accept this as valid code.
这篇关于如何成为模板类函数的朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!