wu :: forums
« wu :: forums - C++ problem »

Welcome, Guest. Please Login or Register.
Jan 7th, 2025, 11:15pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: towr, SMQ, ThudnBlunder, Icarus, william wu, Grimbal, Eigenray)
   C++ problem
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: C++ problem  (Read 607 times)
curt_cobain
Newbie
*





   


Posts: 33
C++ problem  
« on: Oct 1st, 2008, 1:37pm »
Quote Quote Modify Modify

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: male
Posts: 13730
Re: C++ problem  
« Reply #1 on: Oct 1st, 2008, 2:03pm »
Quote Quote Modify 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: male
Posts: 2084
Re: C++ problem  
« Reply #2 on: Oct 1st, 2008, 7:23pm »
Quote Quote Modify 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: male
Posts: 57
Re: C++ problem  
« Reply #3 on: Oct 1st, 2008, 11:12pm »
Quote Quote Modify 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: male
Posts: 13730
Re: C++ problem  
« Reply #4 on: Oct 2nd, 2008, 12:35am »
Quote Quote Modify 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
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

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