Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Special Case: Creating a variable in a function?

Reply
Thread Tools

Special Case: Creating a variable in a function?

 
 
patelvijayp@gmail.com
Guest
Posts: n/a
 
      04-17-2007
On Apr 17, 5:58 am, (E-Mail Removed) wrote:
> How would you declare and assign a variable inside a function THAT HAS
> THE NAME OF A PARAMETER YOU PASSED
>
> example:
> when you call createvariable("myvariable")
>
> it will declare the variable "myvariable"
> and then maybe assign it something.
> myvariable = "this is a real variable"
>
> also so you can use it later, outside of the function.


Hello,

You are trying achieve one more level of generalization on Language.
Its like, you want your own variable generation at runtime!

1) If your variable type is fixed, i.e. you need just String type..--
You can create std::map<string, string> varMap
varMap["myvariable1"] = "1st variable";
varMap["myvariable2"] = "2nd variable";

at any time, you cat get your variable.

2) If variable type is also not fixed, you must need add extra setup.

NOTE: Code written below is just explanation & Not compiled or
working!

union varValue{
string strValue;
int intValue;
}

enum varType {STRING, INT} //Again enum, i was advised yesterday that,
enum is part of fully OO.

struct variable {
varType type;
varValue value;
}

std::map<string, variable> variableTable;

variable var1, var2;
var1.type = STRING;
var1.value = "my string variable value";
variableTable["myVar1"] = var1;

var2.type = INT
var2.value = 10;
variableTable["myVar2"] = var2;

Now you can have anytime, your variable's type & its value known to
you at runtime.

But still, getting value from varibaleTable is complex job.
Either you need to remember that myVar2 is int & you would always
write,
int t1 = variableTable["myVar2"].value
string s1 = variableTable["myVar1"].value

Or you need to write function that can manage typecasting or throw
exception! Here i think the solution fail.
still it could be like this,
int i1 = getValue(variableTable["myVar1"], INT); //myVar1 is string!!

& getValue would throw exception when it checks that
variableTable["myVar1"].type != INT


Finally, above could be considered as non-standard programming
practice.


Vijay.

 
Reply With Quote
 
 
 
 
tragomaskhalos
Guest
Posts: n/a
 
      04-17-2007
On Apr 17, 9:46 am, (E-Mail Removed) wrote:
>
> union varValue{
> string strValue;
> int intValue;
> }
>


No, you can't do this: (think: when a instance of varValue goes out of
scope, what destructor if any should be called?).

 
Reply With Quote
 
 
 
 
Chinchilla
Guest
Posts: n/a
 
      04-18-2007
On Apr 16, 8:58 pm, (E-Mail Removed) wrote:
> How would you declare and assign a variable inside a function THAT HAS
> THE NAME OF A PARAMETER YOU PASSED
>
> example:
> when you call createvariable("myvariable")
>
> it will declare the variable "myvariable"
> and then maybe assign it something.
> myvariable = "this is a real variable"
>
> also so you can use it later, outside of the function.


I would recommend a hash table. You make it sound like you want self
modifying code on runtime. 'Hash tables' is more of a topic for
another group - it's not a C++ phenomenon.

 
Reply With Quote
 
patelvijayp
Guest
Posts: n/a
 
      04-18-2007
On Apr 17, 4:42 pm, tragomaskhalos <(E-Mail Removed)>
wrote:
> On Apr 17, 9:46 am, (E-Mail Removed) wrote:
>
>
>
> > union varValue{
> > string strValue;
> > int intValue;
> > }

>
> No, you can't do this: (think: when a instance of varValue goes out of
> scope, what destructor if any should be called?).


Yes, thank you for pointing out union & Class object. So it must use
pointer now.
I tried to write whole code, here is working copy. Still it need extra
code to follow standard practice, i.e. auto_ptr, providing getValue
function.

#include <iostream>
#include <string>
#include <map>
using namespace std;

union varValue{
string* strValue;
int* intValue;
};

enum varType {STRING, INT}; //Again enum, i was advised yesterday
that, enum is part of fully OO.

struct variable {
varType type;
varValue value;
};

int main() {
map<string, variable> variableTable;

variable var1, var2;
var1.type = STRING;
string tmpStr = string("my string variable value");
var1.value.strValue = &tmpStr;
variableTable["myVar1"] = var1;

var2.type = INT;
int temp = 10;
var2.value.intValue = &temp;
variableTable["myVar2"] = var2;

cout << *variableTable["myVar2"].value.intValue << endl;
cout << *variableTable["myVar1"].value.strValue << endl;
}

Vijay.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a variable name as the value of another variable. rdstevenson@hotmail.co.uk ASP General 6 10-13-2007 02:51 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Special Report: How special are you? Death from Above MCSE 2 03-19-2007 07:22 PM
Special editions and Deluxe special edition dvd question. Rclrk43 DVD Video 8 12-29-2004 07:32 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments