Sign in to follow this  
Waaayoff

Is this possible? if so, how to code it? (c++)

Recommended Posts

Waaayoff    952
Let's say i have a Class that handles saving/loading and encrypting/decrypting that i can use for all my projects by just including it in the project. If i have a simple struct such as: struct StudentInfo { int Age; string Name; int Mark; bool Pass; } Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this: void Func(StudentInfo &Object) And i also can't access the members like this: (Inside Func) Object.Age Object.Name Object.Mark Object.Pass What can i do?

Share this post


Link to post
Share on other sites
RDragon1    1205
Quote:
Original post by Waaayoff
Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this:

void Func(StudentInfo &Object)


Uh, why not? What does needing to use it in multiple projects have to do with passing the object to a function?

Quote:
Original post by Waaayoff
And i also can't access the members like this:


Uh, why not? Those are members of the struct, aren't they? So what's the problem?

Share this post


Link to post
Share on other sites
CodeCriminal    290
there is a way to do it but i dont think people recommend it (not quite sure why). Unfortunatly ive forgotten how to do it, i think it has something to do with casting the instance of the struct to char* when using the member functions read or write... errr.. example:

std::ofstream out("somefile.txt");

/* --- */

out.write(reinterpret_cast<char*>(&studentInfoInstance), sizeof(StudentInfo));



something like that..

Share this post


Link to post
Share on other sites
Waaayoff    952
Quote:
Original post by RDragon1
Quote:
Original post by Waaayoff
Now since i need to use this class for all my projects and not just this one, i can't pass the structure like this:

void Func(StudentInfo &Object)


Uh, why not? What does needing to use it in multiple projects have to do with passing the object to a function?

Quote:
Original post by Waaayoff
And i also can't access the members like this:


Uh, why not? Those are members of the struct, aren't they? So what's the problem?




Well, since the class is going to be the same for all projects, the func declaration will always be:

void Func(StudentInfo &Object)

But what if the struct name was PlayerInfo? We can't pass an Object of PlayerInfo to Func since it's expecting an Object of StudentInfo.

Share this post


Link to post
Share on other sites
haegarr    7372
The OP probably meant to have an API for that special file management that is unaware of the concrete structures; it should be able to load/save anything that is passed.

However, such a persistence system based on de-/serialization has its own caveats. E.g.: What is if a structure evolves over time although some older versions are already stored somewhere? What is with references/pointers to other structures? Endianess differences of the platform where a file was stored and the platform where it has to be loaded? And, similarly, the differences in data types for 32 vs. 64 bit machines? Not to forget the problem that e.g. C++ classes may have hidden features that are not found in C structures.

Simply casting a memory region to char[] for saving may work, but it will cause all kinds of trouble during loading. You may look at the principles of blender's SDNA or google's Protocol Buffer to get some insight how others do it (although AFAIK both these examples deal with C structures only, so that C++ would need some extra work, e.g. a kind of a class registry for mapping class names to object factories).

Share this post


Link to post
Share on other sites
Buklenios    106
You can actually achieve what you want by using template functions.

Declare a template function in your saving class like so:


template<class T> void Save(T &DataToSave)
{
int nNrOfBytes = sizeof(T);
char *pBytes = reinterpret_cast<char*>(&DataToSave);

// TODO:
// Do your decryption here (updating pBytes and nNrOfBytes if necessary)

// Save the data as proposed by CodeCriminal
// (out is an open file stream)
out.write(pBytes, nNrOfBytes);
}





Your application would call the function as so

StudentInfo studentInfoInstance;
PlayerInfo playerInfoInstance;
// Initialize your struct instances here

// Call the save template method
CSaveAndDecryptClass.Save<StudentInfo>(studentInfoInstance);
CSaveAndDecryptClass.Save<PlayerInfo>(playerInfoInstance);



Share this post


Link to post
Share on other sites
rip-off    10979
The examples posted above will only work for POD types, which std::string is not. Most useful C++ classes are not POD, so you will need a separate layer that will turn them into a byte stream which can them be fed to raw decrypt/write functions.

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by rip-off
The examples posted above will only work for POD types, which std::string is not.


Worse. Under MVC it will work for strings less than 16 characters due to inlined buffer, simply as a side-effect of how Visual Studio implements std::string.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this