What are security risks with friend functions? Will it compromise encapsulation and data-hiding in C++?
I am not able to get the proper answer in-spite a lots of research. Can someone give a concrete answer with example?
Here is an example ,the function FUNC will destroy the protection of the data in multiple thread enviroment.
# include <windows.h>
# include <iostream>
using namespace std;
void func();
class DataAdapter
{
friend void func();
private:
static volatile LONG _index;
public:
static void incr()
{
InterlockedIncrement(&_index);
}
};
void func()
{
DataAdapter::_index += 1;
}
DWORD WINAPI threadproc(void *pdata)
{
pdata = pdata;
DataAdapter::incr();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread = CreateThread(NULL , 0 , threadproc , 0 , 0 , 0);
WaitForSingleObject(hThread , 5000);
return 0;
}