Link to home
Start Free TrialLog in
Avatar of jaygeekay
jaygeekay

asked on

access c++ method using string literal

Hi,

I have a problem I am working on here. I wish to store in a file a list of attributes for an object. From within a C++ class i then want to read in this file and access the specified attributes for the object.

e.g.
the file might say - type, a Get method name and a Set method name.
int, GetID, SetID

somewhere there is a class like
class MyClass
{
private:
int m_ID;
public:
int GetID() {return m_ID;};
void SetID(int val) {m_ID = val;};
};

then in the code i wish to get this row from the file and execute the correspoding Get or Set method on the object.

string gettype = get the type from the file
string getmethod = get the Get method name from the file
string setmethod = get the Set method name from the file

then somehow say

class MyClass obj = new class MyClass();

*** this is the bit i haven't a clue about
int result = (gettype) obj.getmethod();
***

is this possible? or even ignore the typing.

any help gratefully appreciated.

Jim.
Avatar of efn
efn

No, it's not possible--unless you build it all yourself, or do a lot of very arcane, difficult, and implementation-specific magic with a debug build of the program.

As an example of building it all yourself, you could design the class with a function that takes a member name string and returns a value, along with a function that takes a member name string and a value and stores the value.

There are other languages that would make this much easier than it would be in C++.  Python is one.

SOLUTION
Avatar of jmccay
jmccay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaygeekay

ASKER

"What your looking for is a combination of reflection & persistence of some kind."

Yup, I want to be able to serialize an object but I want to be able to configure what within the object actually needs to be serialized. JSON was mentioned here as a possible solution, but I can't find any examples of how i would do this anywhere.

Are there any solutions out there for doing something like this?
Thanks for your replies, Jim.
  I am not sure what you mean by JSON.  Could you describe it further?  
   Let me see if I understand you correctly.  You want a mechanism to specify what will be serialized to the storage medium, and you want to decide what will not be serialized.  Setting asside possible unitialized object concerns, the object being serialized knows what needs to be serialized.  So, it needs to be in control.  I think you need to read http://www.microsoft.com/msj/archive/S385.aspx it probably doesn't give you all the answers you want, but it should give you some ideas.
   I am writing a library to allow you to do something similar to what you talked about, BUT it is nowhere ready for anyone else to look at.
jmccay
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well,
Thanks folks for your replies.
There was a small bit more work in the end than I had envisaged.
Some of it along the lines of what was suggested above.

For the configuration side - I associate an id with each attribute. I'm not sure yet whether this will be a simple int or a more descriptive string. I have a configuration file which specifies the ids to serialise.

At the moment I create a structure on construction of the object which associates each id with a memory pointer. So when serialising it is a simple task of moving the data in/out of the pointed to space. Note that the structure contains a union to allow for different types. I'm not sure if this is really necessary though. It might be easier to do as Dan suggested.

Then the serialisation - well, simple types are easy enough to serialise, but each class dependency also needs to be serialised. And also vectors/queues etc. At the moment my solution is very simple. Any class that wishes to be serialised must implement it's own serialise/deserialise method. Arrays/queues etc. will have to store the size and then serialise each entry.

For my first foray into serialising it is certainly an eye opener.

Anyways, as I said, thanks for your replies.
Jim.