A
Amar Kumar Dubedy
implement a c++ class such that it allows us
to add data members at runtime.
to add data members at runtime.
Amar said:implement a c++ class such that it allows us
to add data members at runtime.
implement a c++ class such that it allows us
to add data members at runtime.
Here's a great answer, impossible.
I think Coplien has some code that *simulates* this in 'Advanced C++
Programming Styles and Idioms'.
john
I thought that it was impossible too. But its a question asked in
Yahoo Interview. So i thought i wud put it up.
sure it's possible, still you need to access them, and know their types. allAmar said:implement a c++ class such that it allows us
to add data members at runtime.
Bobba said:sure it's possible, still you need to access them, and know their types. all
feasible.
a void pointer kan be used, or if you know the type you can make an abstract
base class and inherit classes from that. save in whatever for later
access. Still, as I see it there needs to be some kind of "known" interface
that you can use for accessing.
John said:Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.
depends on where you save them, stack, list, vector. a data member can alsoJohn said:Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.
john
Bobba said:depends on where you save them, stack, list, vector. a data member can also
be embedded in a abstract class, which you derive a specialised class from.
so, my idea is that the cotnainer used for storing the dynamic data members
would be "compiled in". but the contents would not. well, try it*S*
Amar said:implement a c++ class such that it allows us
to add data members at runtime.
John said:Data members have a type, and a name, they are accessed with a
particular syntax. All of these are features of source code, not of a
running program. The question doesn't amke sense unless it is a trick
question.
Of course of the question had been, 'write a class so that you can add
arbitrary data at runtime' it would be much easier. But the question
said data members, not data.
Gianni said:Amar said:implement a c++ class such that it allows us
to add data members at runtime.
This is usually implemented as a map like so:
#include <string>
#include <map>
#include <at_any.h> // or boost any
struct Extensible
{
std::map< std::string, at::Any<> > m_members;
};
Extensible a;
int main()
{
a.m_members[ "new_member" ] = at::ToAny( 5 );
}
If you want to enforce that every Extensible object has the same members
it gets a little more complex but nothing too hard.
well, as you are the programmer yourself you know how to embed the podts inJohn said:Bobba Dobba wrote:
Well your code has the problem I mentioned in another post. You are
adding data to objects, not data members to classes.
john
Bobba said:well, as you are the programmer yourself you know how to embed the podts in
classes. why is that a problem?
i just proved it was possible. don't get stuck with plain old data types.John said:Gianni said:Amar said:implement a c++ class such that it allows us
to add data members at runtime.
This is usually implemented as a map like so:
#include <string>
#include <map>
#include <at_any.h> // or boost any
struct Extensible
{
std::map< std::string, at::Any<> > m_members;
};
Extensible a;
int main()
{
a.m_members[ "new_member" ] = at::ToAny( 5 );
}
If you want to enforce that every Extensible object has the same members
it gets a little more complex but nothing too hard.
Well this last sentence is the point.
And it still remains the case that Extensible has only one data member
'm_members', so this approach is only ever going to be a simulation. But
the original question didn't say anything about simulation.
I still think the correct answer is 'impossible in C++'.
john
plain old data type, like int, char, long.....John said:'podts'? I'm sorry I don't understand.
Bobba said:plain old data type, like int, char, long.....
Bobba said:i just proved it was possible. don't get stuck with plain old data types.John said:Well this last sentence is the point.Gianni said:Amar Kumar Dubedy wrote:
implement a c++ class such that it allows us
to add data members at runtime.
This is usually implemented as a map like so:
#include <string>
#include <map>
#include <at_any.h> // or boost any
struct Extensible
{
std::map< std::string, at::Any<> > m_members;
};
Extensible a;
int main()
{
a.m_members[ "new_member" ] = at::ToAny( 5 );
}
If you want to enforce that every Extensible object has the same members
it gets a little more complex but nothing too hard.
And it still remains the case that Extensible has only one data member
'm_members', so this approach is only ever going to be a simulation. But
the original question didn't say anything about simulation.
I still think the correct answer is 'impossible in C++'.
john
C++ is OO, where you create types as needed, which is really the whole idea
with C++. Keep in mind when creating classes you create new types. Which
can be used in many senses like the plain old data types.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.