dynamic variable name

F

firegun9

Hello all,

After looking for the solution for a while in the group, I know this is
not possible in C++. So I just say my problme here.

The input file is:
dev1 1 1
dev2 0 0 0 6

There is a base class "Base" and two derivated classes "dev1" and
"dev2".
The best way of my code is like:

Read_line_from_file(char* arg1, char* arg2, char* arg3);
Base* p_base = new arg1(arg2, arg3);

If I could have a dynamic variable, the second statement above can new
any type of future derivative class without changing the codes here.
For example, if I have a new class dev3, I just add a line of "dev3 5 5
5" into the text file, then the code will new a "dev3" object.
Isn't it so cool?
However, I can't do that.
Any better idea about this?
 
V

Victor Bazarov

After looking for the solution for a while in the group, I know this is
not possible in C++. So I just say my problme here.

The input file is:
dev1 1 1
dev2 0 0 0 6

There is a base class "Base" and two derivated classes "dev1" and
"dev2".
The best way of my code is like:

Read_line_from_file(char* arg1, char* arg2, char* arg3);
Base* p_base = new arg1(arg2, arg3);

If I could have a dynamic variable, the second statement above can new
any type of future derivative class without changing the codes here.
For example, if I have a new class dev3, I just add a line of "dev3 5 5
5" into the text file, then the code will new a "dev3" object.
Isn't it so cool?

No. If I just add a line of "screwup 13 666" into the text file, how will
your code know what to do?
However, I can't do that.
Any better idea about this?

About what? So, you need to read a string and several numbers that should
in your program lead to creation of an object of some type and pass the
numbers to the constructor of that object. Why does the string have to be
the same as the name of the type in your program?

You need to read some beginner book on reading and parsing input files.
Are you a subscriber of a library? Head there and look for a decent book
on general programming. It really have nothing to do with C++, and is
usually done the same way in all respectable languages.

One thing you should remember, once your program has been compiled, there
are *no* class names or variable names. They've disappeared. To the user
your program looks like a black box. Only the usage rules apply. Now,
you are the one who makes up those rules. So, go make up good rules and
then create a program that enforces them.

V
 
C

Cy Edmunds

Hello all,

After looking for the solution for a while in the group, I know this is
not possible in C++. So I just say my problme here.

The input file is:
dev1 1 1
dev2 0 0 0 6

There is a base class "Base" and two derivated classes "dev1" and
"dev2".
The best way of my code is like:

Read_line_from_file(char* arg1, char* arg2, char* arg3);
Base* p_base = new arg1(arg2, arg3);

If I could have a dynamic variable, the second statement above can new
any type of future derivative class without changing the codes here.
For example, if I have a new class dev3, I just add a line of "dev3 5 5
5" into the text file, then the code will new a "dev3" object.
Isn't it so cool?
However, I can't do that.
Any better idea about this?

Untested code:

class Base
{
public:
// create a new object of the actual type using the argument
// as a parameter
virtual Base *clone(const std::string &) const = 0;
...
};

typedef boost::shared_ptr<Base> PBase;

class BaseFactory
{
std::map<std::string, PBase> m_map;
public:
BaseFactory()
{
m_map["dev1"] = PBase(new dev1);
m_map["dev2"] = PBase(new dev2);
}

Base *
operator() (const std::string &name, const std::string &parameters)
{
const PBase &old_one = m_map[name];
if (old_one.get() == 0)
throw some_exception; // name not found
return old_one->clone(parameters);
}
};

BaseFactory fact;
PBase new_one(fact("dev1", "1 1"));

For this to work, Base must have a default constructor and a virtual "clone"
function which takes a string as a parameter and returns a new object of its
own type intialized with the contents of the string. A BaseFactory object
keeps a complete set of default constructed objects derived from Base and
uses them to clone new ones as required. The smart pointer deletes the
objects when there are no outstanding references to them so you generally
don't have to worry about memory leaks.
 
F

firegun9

Thanks your code, map is a good idea.
Basically, all the types of derived classes have to be stored in the
map.
The disadvantage is I have to update the map if new derived class is
created.
 
V

Victor Bazarov

Thanks your code, map is a good idea.
Basically, all the types of derived classes have to be stored in the
map.
The disadvantage is I have to update the map if new derived class is
created.

Why do you say that it's a disadvantage? Is it a disadvantage that we
have to use our brains to survive? Would you say it's a disadvantage
to have to use our fingers to type?

There are ways where the map could be generated from some external
source (like from calling a function when a dynamic library is loaded
by your application). However, in your *very simple* case, if some
new derived class is developed, your program has to be updated for
that anyway, so you just update your map at the same time. Why are
you making such a big deal out of it? It's not a disadvantage, it's
just life as it manifests itself in programming tasks...
 
M

Markus Dehmann

Victor said:
No. If I just add a line of "screwup 13 666" into the text file, how will
your code know what to do?


About what? So, you need to read a string and several numbers that should
in your program lead to creation of an object of some type and pass the
numbers to the constructor of that object. Why does the string have to be
the same as the name of the type in your program?

You need to read some beginner book on reading and parsing input files.
Are you a subscriber of a library? Head there and look for a decent book
on general programming. It really have nothing to do with C++, and is
usually done the same way in all respectable languages.

He should *not* get a book on general programming because he is not wrong in
terms of general programming. He tries to do something that happens to be
not possible in C++. What he proposes is something very normal and common
in languages with reflection support.

In Java, it is good style to have settings or properties files that
determine which specific implementation of a certain interface should be
loaded. The class name to load is written into the settings file, the file
is read at runtime, and the appropriate class is loaded by the ClassLoader,
or a runtime exception is thrown (if the class does not exist).

Markus
 
M

Markus Dehmann

Victor said:
Thanks your code, map is a good idea.
Basically, all the types of derived classes have to be stored in the
map.
The disadvantage is I have to update the map if new derived class is
created.

Why do you say that it's a disadvantage?
[...]
if some
new derived class is developed, your program has to be updated for
that anyway, so you just update your map at the same time. Why are
you making such a big deal out of it? It's not a disadvantage

"Simplicity - the art of maximizing the amount of work not done - is
essential" (Martin, "Agile Software Development")
 
V

Victor Bazarov

Markus said:
Victor Bazarov wrote:

Thanks your code, map is a good idea.
Basically, all the types of derived classes have to be stored in the
map.
The disadvantage is I have to update the map if new derived class is
created.

Why do you say that it's a disadvantage?
[...]
if some
new derived class is developed, your program has to be updated for
that anyway, so you just update your map at the same time. Why are
you making such a big deal out of it? It's not a disadvantage


"Simplicity - the art of maximizing the amount of work not done - is
essential" (Martin, "Agile Software Development")

Simplicity? Real simplicity or perceived simplicity?

The optimal amount of work to achieve the same result is the same no
matter when you have to apply it. If all possible cases are provided
for before the program begins its lifetime, instead of during the
maintenance periods, the amount of work is not less, it's the same.

Maintenance is a way to delay spending time taking care of all possible
situations that arise in the future. I am not saying that by doing
everything upfront you can avoid maintenance altogether. But you
definitely can shift the work to earlier or later times depending on
your expectations and ability to predict future events. Example: if
there will be no derived classes added to the system later, there is no
need to design and implement (debug, deploy) a more generic system to
accommodate adding of derived classes.

This is all a topic for comp.software-eng, of course.

V
 
H

Howard

Thanks your code, map is a good idea.
Basically, all the types of derived classes have to be stored in the
map.
The disadvantage is I have to update the map if new derived class is
created.

Yes, that's true, but wouldn't you *want* to update the code that uses any
new object? How often do you add a new object or function to a program and
not change the code so that it can make *use* of that new object or
function? I mean, I assume your objects have members that you need to
access, right? Are those memebrs always identical in name and usage? Do
their functions always have the same parameters and return the same type of
result? Doesn't the calling code need to change *somewhere* to deal with
the fact it's using new objects? See what I'm getting at?

But, if those objects *are* always going to interact with the calling code
in exactly the same way, then perhaps what you need is not inheritance, but
rather a state machine in your object, where you simply tell it which of
several "states" it is in, and let it act internally according to those
settings. In other words, you only create objects of one class type, and
let the parameters tell it how it will behave when later calls to its
memebrs are made. (This can work well if you're using a dynamic external
object, such as a COM object. Then, all you do is recompile your DLL or
whatever, and your calling program doesn't change anything at all.)

Another idea might be an intermediate object, one which is constant to the
calling code, but which contains that map you were discussing. Then, when
new derived objects are added, you only change the intermediate object, and
not the "main" program. Not sure if that buys you anything, but it's a
thought.

-Howard
 
N

NON-Microsoft-User

Hello all,

After looking for the solution for a while in the group, I know this is
not possiblemalicious So I just say my problme here.

The input file is:
dev1 1 1
dev2 0 0 0 6

There is a base class "Base" and two derivated classes "dev1" and
"dev2".
The best way of my code is like:

Read_line_from_file(char* arg1, char* arg2, char* arg3);
Base* p_base = new arg1(arg2, arg3);

If I could have a dynamic variable, the second statement above can new
any type of future derivative class without changing the codes here.
For example, if I have a new class dev3, I just add a line of "dev3 5 5
5" into the text file, then the code will new a "dev3" object.
Isn't it so cool?
However, I can't do that.
Any better idea about this?


C++ is not an interpreted language. What you are asking can be done with
more primitive scripting languages like visual basic or php.

In C++ you must know what is to be executed.

MS developed a technology (actually just a wrapped dll with a few hardcoded
functions that tell the app what properties and methods are available)
called ActiveX for which Microsoft is very sorry today. People exploit that
and load malicious code into MS Outlook, MS Internet Explorer, or in any of
server class Microsoft Operating Systems including the not-so-much-better
Windows 2003 server.

But still you must have a fixed variable name of some general ActiveX Type.

Oh, one more thing!
C++ does not provide means to load code. It can only load data. But you can
simulate what a *loader* does but that has nothing to do with C++. It more
OS.
 

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
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top