Hello,
I'm trying to write a DLL in which I want to put classes I use alot. I don't want any non-public methods or data members to be visible to my client applications.
For learning/testing purposes, I have written a test class which contains one public method 'char* HelloWorld()' and one private data member 'char *data'. Upon class construction 'data' is set to "Hello World!". All the 'HelloWorld()' function does is returning the data pointer so it can be printed by my client application.
When I link the lib/DLL to my client application, the data hiding seems to be working. I simply create an instance of the class, and call the 'HelloWorld()'-method.
The only thing I see is the public method - the private data member is hidden. I have reached this by using an class interface / struct.
All compiles well. When I run my client app in debug-mode and let it print the result from 'HelloWorld()', it prints it well, but then it gives the following message:
Quote:
Debug Error!
Run-Time Check Failure #2: Stack around the variable 't' was corrupted.
When I run it in release-mode no error message is given (obviously) but the text contains all kind of weird characters. Seems that the data of the class is pointing to somewhere it shouldn't.
Here is the code:
SVCORE.h (used by library code and client code)
#ifdef SVCORE_EXPORTS
#define SVCORE_API __declspec(dllexport)
#else
#define SVCORE_API __declspec(dllimport)
#endif
ITestClass.h (used by library code and client code)
struct SVCORE_API ITestClass
{
virtual char* HelloWorld(void) = 0;
};
SVTestClass.h (only used by client code)
#include <svcore/itestclass.h>
class SVCORE_API SVTestClass : public ITestClass
{
public:
char* HelloWorld(void);
};
SVTestClass.h (only used by library code)
//==============================================================
#include <svcore/itestclass.h>
class SVCORE_API SVTestClass : public ITestClass
{
public:
SVTestClass(void);
~SVTestClass(void);
public:
char* HelloWorld(void);
private:
char* data;
};
SVTestClass.cpp (only used by library code)
#include "svtestclass.h"
SVTestClass::SVTestClass(void)
{
this->data = "Hello World!";
};
SVTestClass::~SVTestClass(void)
{
this->data = NULL;
}
char* SVTestClass::HelloWorld(void)
{
return data;
}
And finally my client app
#include <stdio.h>
#include <svcore/svtestclass.h>
void main()
{
SVTestClass t;
printf("%s\n", t.HelloWorld());
}
Thus.. I have one general interface declaring which methods should be visible to the client app. In the library class I declare a data field, while the client isn't aware of it.
What am I doing wrong? I DO need that data member in the class, but I DON'T want the client to be able to see or access it.
Best wishes, and thanks for your help in advance,
Necrarch