By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

great c++ question

P: n/a
implement a c++ class such that it allows us
to add data members at runtime.

Jun 21 '07 #1
Share this Question
Share on Google+
29 Replies


P: n/a
Amar Kumar Dubedy wrote:
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
Jun 21 '07 #2

P: n/a
On Jun 21, 6:48 am, Amar Kumar Dubedy <adub...@yahoo.co.inwrote:
implement a c++ class such that it allows us
to add data members at runtime.
Who in the world would want that?

Jun 21 '07 #3

P: n/a
On Jun 21, 10:50 am, John Harrison <john_androni...@hotmail.com>
wrote:
Amar Kumar Dubedy wrote:
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.

Jun 21 '07 #4

P: n/a
am**********@yahoo.co.in wrote:
On Jun 21, 10:50 am, John Harrison <john_androni...@hotmail.com>
wrote:
>Amar Kumar Dubedy wrote:
>>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.
Sounds like a trick question.
Jun 21 '07 #5

P: n/a
Amar Kumar Dubedy wrote:
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.
Jun 21 '07 #6

P: n/a
Bobba Dobba wrote:
Amar Kumar Dubedy wrote:
>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.
Show us some code. I don't see how anything you've said above relates to
adding data members at runtime.

john
Jun 21 '07 #7

P: n/a
John Harrison wrote:
Bobba Dobba wrote:
>Amar Kumar Dubedy wrote:
>>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.

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*
Jun 21 '07 #8

P: n/a
John Harrison wrote:
Bobba Dobba wrote:
>Amar Kumar Dubedy wrote:
>>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.

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.
Jun 21 '07 #9

P: n/a
Bobba Dobba wrote:
John Harrison wrote:
>Bobba Dobba wrote:
>>Amar Kumar Dubedy wrote:

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

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++.
Jun 21 '07 #10

P: n/a
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.
Jun 21 '07 #11

P: n/a
John Harrison wrote:
John Harrison wrote:
>Bobba Dobba wrote:
>>Amar Kumar Dubedy wrote:

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.

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.
Jun 21 '07 #12

P: n/a
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
Jun 21 '07 #13

P: n/a
Gianni Mariani wrote:
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
Jun 21 '07 #14

P: n/a
John Harrison wrote:
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?
Jun 21 '07 #15

P: n/a
Bobba Dobba wrote:
John Harrison wrote:
>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?
'podts'? I'm sorry I don't understand.
Jun 21 '07 #16

P: n/a
John Harrison wrote:
Gianni Mariani wrote:
>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.
Jun 21 '07 #17

P: n/a
John Harrison wrote:
Bobba Dobba wrote:
>John Harrison wrote:
>>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?

'podts'? I'm sorry I don't understand.
plain old data type, like int, char, long.....
Jun 21 '07 #18

P: n/a
Bobba Dobba wrote:
John Harrison wrote:
>Bobba Dobba wrote:
>>John Harrison wrote:

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?
'podts'? I'm sorry I don't understand.
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.

Jun 21 '07 #19

P: n/a
Bobba Dobba wrote:
John Harrison wrote:
>Gianni Mariani wrote:
>>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

Jun 21 '07 #20

P: n/a
"Amar Kumar Dubedy" <ad*****@yahoo.co.inwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
implement a c++ class such that it allows us
to add data members at runtime.
You can't do that with Standard C++. However, I think you might be able to
use a modified C++ compiler that has some reflection techniques wrt storing
a full-blown metadata representation of the class in the compiled binary.
The metadata could store the data-members along with their types and names.
Jun 21 '07 #21

P: n/a
"John Harrison" <jo*************@hotmail.comwrote in message
news:yj***************@newsfe1-win.ntli.net...
[...]
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.
[...]

Humm, perhaps that was the whole point behind the "trick" question?

;^)

Jun 21 '07 #22

P: n/a
>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.
You seem to have turned the question about adding data members to
classes, into a question about how to handle arbitary data types in C++.
But that is a different question.

Here's a simpler question, implement a class so that is allows you to
add integer data members at runtime. This is NOT a solution

class X
{
vector<intm_members;
};

because that allows you to add integers to an object, not integer data
members to a class. See?

john
Jun 21 '07 #23

P: n/a
You can't do that with Standard C++. However, I think you might be
able to
use a modified C++ compiler that has some reflection techniques wrt
storing a full-blown metadata representation of the class in the
compiled binary. The metadata could store the data-members along
with their types and names.

Why would you? You can use boost::any vectors for that. No need for a
special tool.
Jun 21 '07 #24

P: n/a
On Jun 21, 8:37 am, John Harrison <john_androni...@hotmail.comwrote:
Gianni Mariani wrote:
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.
One of your points, anyway:-).
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++'.
I think that the problem is understanding at what level the
question was asked. I don't think that there's any doubt that
you cannot change the topology of a C++ class at runtime, at the
C++ level. And the question *did* ask about a "C++ class". But
I'd hesitate to respond "impossible" myself, if asked the
question during an interview, because in practice, I suspect
that what the person asking really means is "implement a class
[conceptual type] in C++ such that...". And that can be done:
how, and how difficult it is, depends on what the questionner
really means---I suspect that in most cases, a solution like
Gianni's is more or less what they are really looking for. Even
though it "fails" on two grounds: you are adding elements to
individual objects, not to the class (but that can be handled by
some sort of a static "set" with the names of the elements), and
that the elements aren't associated with a type---no problem if
they are only present in each separate object (because
boost::any, and I suppose Gianni's at::Any, manage type), but
you'd need some sort of shared typemap as well if you wanted to
manage type at the "class" level.

I think it's an often overlooked point that we often use the
same, or very similar, vocabulary for the concept, and the way
we implement it in the language. Thus, for example, when I
"inherit" in C++, I may be doing so to implement the concept of
inheritance in OO design, but I may be doing so for some
entirely different reason; there's not necessarily a one to one
mapping. In this case, given the way the question is
formulated, I suspect that---despite the presicion "C++
class"---what is really meant is a conceptual class, or a user
defined type, if you prefer. I suspect this because it is
really very rare for people to make the distinction properly,
and a question of the form "implement X in C++", or even
"implement a C++ X", usually means "implement the concept X in
the programming language C++". Maybe it shouldn't, but in my
experience, it usually does.

And of course, I don't want to get turned down for a job just
because the questionner doesn't formulate the questions as
precisely as I would like.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 21 '07 #25

P: n/a
On Jun 21, 10:20 am, "Gernot Frisch" <M...@Privacy.netwrote:
You can't do that with Standard C++. However, I think you
might be able to use a modified C++ compiler that has some
reflection techniques wrt storing a full-blown metadata
representation of the class in the compiled binary. The
metadata could store the data-members along with their types
and names.
Why would you? You can use boost::any vectors for that. No need for a
special tool.
As John has pointed out, a map of boost::any just adds values to
an object---a single instance of a class. Presumably, by adding
something like a static:

static std::map< std::string, std::type_info const* >
ourMembers ;

and adding whatever checks are appropriate to the getters and
setters of the class, you could get the same effect as adding
members dynamically to a class. The same effect, but no where
near the same syntax. Where as with a compiler modification,
you could write something like:

std::string name ;
std::cin >name ;
std::cout << container.name ...

Of course, even with the compiler modifications, you couldn't
get the static typechecking which otherwise characterizes C++
class members.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 21 '07 #26

P: n/a
On Jun 21, 12:48 am, Amar Kumar Dubedy <adub...@yahoo.co.inwrote:
implement a c++ class such that it allows us
to add data members at runtime.
class CrazyClass
{
public:
void AddDataMember(string Name, void* Value)
{
_DataMembers[Name] = Value;
}

void* GetDataMember(string Name) const
{
map<string, void*>::const_iterator iter =
_DataMembers.find(Name);
if (iter == _DataMembers.end())
return NULL;
return iter->second;
}

private:
map<string, void*_DataMembers;
};

Do I get the job?

Jun 21 '07 #27

P: n/a
"James Kanze" <ja*********@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
On Jun 21, 8:37 am, John Harrison <john_androni...@hotmail.comwrote:
[...]
I think that the problem is understanding at what level the
question was asked. I don't think that there's any doubt that
you cannot change the topology of a C++ class at runtime, at the
C++ level. And the question *did* ask about a "C++ class". But
I'd hesitate to respond "impossible" myself, if asked the
question during an interview, because in practice, I suspect
that what the person asking really means is "implement a class
[conceptual type] in C++ such that...".
[...]
If you talked about both sides of the issue, like you did, well:

"Welcome to our Company Sir: Your Hired!"

:^)

Jun 21 '07 #28

P: n/a
On Jun 21, 2:04 am, John Harrison <john_androni...@hotmail.comwrote:
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.

You seem to have turned the question about adding data members to
classes, into a question about how to handle arbitary data types in C++.
But that is a different question.

Here's a simpler question, implement a class so that is allows you to
add integer data members at runtime. This is NOT a solution

class X
{
vector<intm_members;

};

because that allows you to add integers to an object, not integer data
members to a class. See?

john
I'm pretty sure everyone here understands the difference between
adding items to a class and to an object. When faced with a question
like that in a job interview, however, do you simply say "oh yea it
isn't possible", or do you try to think of simulated approaches to
present to them and explain that technically this isn't what they
asked for, but it's the closest possible thing. I actually thought of
any even better solution a second ago.
class C
{
public:
template<typename T>
void AddMember(string Name, T Value)
{
static map<string, TLookup;
}
};

etc. Nobody cares about the purist technical difference between
adding members to a class at compile time and adding members to a
class at runtime. What's important in an interview situation is being
able to improvise and think of out-of-the-box solutions while still
indicating that you know when something is or isn't exactly what
they're asking about.

Jun 21 '07 #29

P: n/a
Bobba Dobba wrote:
John Harrison wrote:
>John Harrison wrote:
>>Bobba Dobba wrote:
Amar Kumar Dubedy wrote:

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

#include <iostream>
#include <vector>
class AbstrBase
{
public:
virtual void run()=0;
};
class Int:public AbstrBase
{
int m_i;
public:
void setI(int i)
{
m_i = i;
};
const int& getI()const
{
return m_i;
};
void run()
{
std::cout << "Value is of type integer" << std::endl;
std::cout << "Value is=" << m_i << std::endl;
};

};
class Str:public AbstrBase
{
std::string m_s;
public:
void setS(const std::string s)
{
m_s = s;
};
const std::string& getS()const
{
return m_s;
};
void run()
{
std::cout << "Value is of type string" << std::endl;
std::cout << "Value is=" << m_s << std::endl;
};
};
class Container:public std::vector<AbstrBase*>
{
};
int main(void)
{
Container c;
Int * a = new Int;
a->setI(1);
Str * b = new Str;
b->setS(std::string("hello world!"));
c.push_back(a);
c.push_back(b);
std::vector<AbstrBase*>::iterator it = c.begin();
while(it != c.end() )
{
(*it)->run();
it++;
}
return 0;
}
OK, but all you have shown here is an example of polymorphism and
dynamic binding. Both of these are well-known and basic OO concepts
that can be implemented in C++. If the question really was about this,
than it's not a very interesting or "great c++ question," as the OP stated.

However, the question explicitly asked about adding data _members_ at
runtime. That is something entirely different from what you are doing here.
Jun 21 '07 #30

This discussion thread is closed

Replies have been disabled for this discussion.