Author |
Topic: C++ problem (Read 607 times) |
|
curt_cobain
Newbie
Posts: 33
|
Implement a c++ class such that it allows us to add data members at runtime.
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: C++ problem
« Reply #1 on: Oct 1st, 2008, 2:03pm » |
Quote Modify
|
That doesn't really make much sense. Can you elaborate on what exactly you mean by "adding data-members at runtime". Also the title for this topic really doesn't do anything to distinguish from the dozens of other "C++ problem"s.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: C++ problem
« Reply #2 on: Oct 1st, 2008, 7:23pm » |
Quote Modify
|
Well, since C++ is statically typed there's no way to make dynamic members type safe, so I'd probably keep it darn simple: class foo { public: void set(string name, void * value) { _members[name] = value; } void * get(string name) { return _members[name]; } protected: map<string, void *> _members; }; --SMQ
|
|
IP Logged |
--SMQ
|
|
|
ic10503
Junior Member
Gender:
Posts: 57
|
|
Re: C++ problem
« Reply #3 on: Oct 1st, 2008, 11:12pm » |
Quote Modify
|
on Oct 1st, 2008, 7:23pm, SMQ wrote:Well, since C++ is statically typed there's no way to make dynamic members type safe, so I'd probably keep it darn simple: class foo { public: void set(string name, void * value) { _members[name] = value; } void * get(string name) { return _members[name]; } protected: map<string, void *> _members; }; --SMQ |
| I could not understand your code. Can you please explain what u r trying to do ? What is _members?
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: C++ problem
« Reply #4 on: Oct 2nd, 2008, 12:35am » |
Quote Modify
|
on Oct 1st, 2008, 11:12pm, ic10503 wrote:I could not understand your code. Can you please explain what u r trying to do ? What is _members? |
| It's a map (let's say, associative array). You give it a string and it returns a pointer to the "member" added at runtime. Of course the map is really the member here, and not its key-value pairs.
|
|
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
|