A
alikim
Guest
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'.
How do I make it work?
#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();
}
Continue reading...
How do I make it work?
#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();
}
Continue reading...