473,837 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3092
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.goo glegroups.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_p tr<Base> PBase;

class BaseFactory
{
std::map<std::s tring, 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(paramete rs);
}
};

BaseFactory fact;
PBase new_one(fact("d ev1", "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******@yaho o.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.goo glegroups.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 possiblemalicio us 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
10024
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 value to. Let's say I've dimmed 12 variables based on the months called "featured_x_product" where x is the month. I'd like to refer to the variable with something like: featured_" & R("month") & "_product where R("month") is the from the...
2
1958
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" Because I'm trying to create a structure like this... usecase ->item(s) (kind of ordered list) ->alt (with @number parameter)
1
17687
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 Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
2
673
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 very basic scenario: using System;
7
3395
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 either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
2
2947
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 sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
5
14439
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 variable name.
2
3641
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
14310
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. I have done this in the past in SQL server, but DB2 is not as easy... Anyone out there that can help me? Your help will be much appreciated.
2
1437
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 "
0
9835
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10562
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10617
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10265
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5667
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4036
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3122
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.