great c++ question

A

Amar Kumar Dubedy

implement a c++ class such that it allows us
to add data members at runtime.
 
J

John Harrison

Amar said:
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
 
A

amarkrdubedy

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.
 
B

Bobba Dobba

Amar said:
implement a c++ class such that it allows us
to add data members at runtime.
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.
 
J

John Harrison

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.

Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.

john
 
J

John Harrison

John said:
Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.

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.
 
B

Bobba Dobba

John said:
Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.

john
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*
 
J

John Harrison

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*

Add data to a vector is adding data to an object. It is not the same
thing at all as adding a data member to a class. For instance if you add
a data member to a class, then ALL objects of that class get the new
data member.

class X
{
int d;
};

d is a data member, now how at runtime do I change the code so that X
becomes

class X
{
int d;
int d2;
};

The obvious problem is that X only exists in the source code of the
program. This is not true of all programming languages, but it is true
of C++.
 
G

Gianni Mariani

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.
 
B

Bobba Dobba

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.
 
J

John Harrison

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
 
J

John Harrison

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
 
B

Bobba Dobba

John 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
well, as you are the programmer yourself you know how to embed the podts in
classes. why is that a problem?
 
J

John Harrison

Bobba said:
well, as you are the programmer yourself you know how to embed the podts in
classes. why is that a problem?

'podts'? I'm sorry I don't understand.
 
B

Bobba Dobba

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
i just proved it was possible. don't get stuck with plain old data types.
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.
 
J

John Harrison

Bobba said:
plain old data type, like int, char, long.....

I still don't understand your point, and don't think you understood
mine. Oh well it's a pretty fruitless discussion.
 
J

John Harrison

Bobba said:
John said:
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.
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
i just proved it was possible. don't get stuck with plain old data types.
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.

I'll say it again, your code and Gianni's code adds data to objects, the
question was about adding data members to classes. Do you understand the
difference? Gianni at least does.

I'll say this again, any solution will only be a *simulation* because it
is impossible to modify a *class* in C++ at runtime. In C++ modifying
classes is what programmers do when they write code. Some programming
languages allow you to modify or create classes at runtime but not C++.

john
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,494
Members
48,180
Latest member
DelmarCarv

Latest Threads

Top