great c++ question

C

Chris Thomasson

Amar Kumar Dubedy said:
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.
 
J

John Harrison

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<int> m_members;
};

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

john
 
C

Chris Thomasson

[...]
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?

;^)
 
G

Gernot Frisch

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

James Kanze

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.

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

James Kanze

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

Zachary Turner

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?
 
Z

Zachary Turner

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<int> m_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, T> Lookup;
}
};

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

Chris Thomasson

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!"

:^)
 
S

shadowman

Bobba said:
>
> #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.
 

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,179
Latest member
รับปั๊มไลค์|LikePro

Latest Threads

Top