wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> C++ problem
(Message started by: curt_cobain on Oct 1st, 2008, 1:37pm)

Title: C++ problem
Post by curt_cobain on Oct 1st, 2008, 1:37pm
Implement a c++ class such that it allows us
to add data members at runtime.

Title: Re: C++ problem
Post by towr on Oct 1st, 2008, 2:03pm
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.

Title: Re: C++ problem
Post by SMQ on Oct 1st, 2008, 7:23pm
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

Title: Re: C++ problem
Post by ic10503 on Oct 1st, 2008, 11:12pm

on 10/01/08 at 19:23:33, 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?

Title: Re: C++ problem
Post by towr on Oct 2nd, 2008, 12:35am

on 10/01/08 at 23:12:40, 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.



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board