473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

dynamic variable name

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?

Jul 23 '05 #1
9 3058
fi******@yahoo.com.tw wrote:
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
Jul 23 '05 #2
<fi******@yahoo.com.tw> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #3
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.

Jul 23 '05 #4
fi******@yahoo.com.tw 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? 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...
Jul 23 '05 #5
Victor Bazarov wrote:
fi******@yahoo.com.tw wrote:
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.


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


Jul 23 '05 #6
Victor Bazarov wrote:
fi******@yahoo.com.tw 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")
Jul 23 '05 #7
Markus Dehmann wrote:
Victor Bazarov wrote:

fi******@yahoo.com.tw 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
Jul 23 '05 #8

<fi******@yahoo.com.tw> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Jul 23 '05 #9
fi******@yahoo.com.tw wrote:
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.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken Halley | last post by:
How does one refer to a variable name using a variable within a variable name??? For example, I want to perform a SQL query and use the results of the query to determine which variable to assign a...
2
by: Curantil | last post by:
When I look at all the examples every selection like select="child::item" has a constant value to test against ("3" in this case). Can't this be done with a variable? eg: select="child::item" ...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
2
by: Martin Hart - Memory Soft, S.L. | last post by:
Hi all: I still very new to the .NET world and don't know if what I am asking is due to an over-imaginative imagination or the fact that I have read too many fiction books!! Let me show you a...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
5
by: Angelos | last post by:
Hello, I need to dynamically specify the name of a variable. I just read that $varA = "Cat"; echo $$varA; OUTPUT: Cat What I try to establish is somehow add a bit of text on my dynamic...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
by: devx777 | last post by:
Hello, I am trying to find some information or an example on how to build a dynamic query in DB2 that would allow me to join a table which its name is stored as a field value on another table....
2
by: viki1967 | last post by:
Hi there. I have a problem in to recover the values of a dynamic form. The form is this: strSQL = "SELECT * " strSQL = strSQL & " FROM " strSQL = strSQL & " TBL "
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.