Special Case: Creating a variable in a function?

J

Justcallmedrago

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

Ian Collins

How would you declare and assign a variable inside a function THAT HAS
THE NAME OF A PARAMETER YOU PASSED
Please don't shout!
example:
when you call createvariable("myvariable")

it will declare the variable "myvariable"
and then maybe assign it something.
myvariable = "this is a real variable"
You can't. What problem are you trying to solve?
 
G

Gaijinco

Your question is very hard to understand. Can you try to be a little
clearer in your problem? Maybe some code.
 
I

Ian Collins

Gaijinco said:
Your question is very hard to understand. Can you try to be a little
clearer in your problem? Maybe some code.
Which question, please retain context on Usenet posts.
 
V

Victor Bazarov

How would you declare and assign a variable inside a function THAT HAS
THE NAME OF A PARAMETER YOU PASSED

Please do not use ALL CAPS. It looks like you are SHOUTING.
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.

There are no variable names when the program is running. They
exist only for the programmer's convenience while he/she is
writing the program. Once the program has been processed by
the compiler, the code and all its individually recognizable
elements have been converted to machine instructions and
addresses.

If you need to tag objects in your program with some kind of
symbolic constructs (like names, IDs, etc.), you're free to do
so using whatever constructs you feel suited for that. For
example, you can add a data member to your class and give it
the type 'std::string' and make it store what you'd interpret
as a name of the instance of the class. Perhaps you could
elaborate on what you're trying to accomplish...

V
 
S

Sherm Pendley

Gaijinco said:
Your question is very hard to understand.

Followups that don't quote context are very hard to understand too. What
question are you talking about?

sherm--
 
J

Justcallmedrago

Please don't shout!

Sorry :D
You can't. What problem are you trying to solve?


I'm trying to create a game engine and one of the functions i'm trying
to create involve loading a mesh and then having the materials for it
end up in an array.

void create_mesh(LPCWSTR path, char array arraynew) // just... kind of
ignore that second parameter.
{

// complicated stuff here to make arraynew work
LPD3DXBUFFER bufNew;

D3DXLoadMeshFromX (path, // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into
system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufNew, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in
this model
&meshSpaceship); // put the mesh here

// retrieve the pointer to the buffer containing the material
information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufNew-
GetBufferPointer();

// create a new material buffer for each material in the mesh
material = new D3DMATERIAL9[numMaterials];

for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
arraynew = tempMaterials.MatD3D; // get the material
info
arraynew.Ambient = material.Diffuse; // make ambient
the same as diffuse
}

return;

so far the path parameter works. you can put any character string that
is a filename into it. ex: "mymeshfile.x"
i want to do the same sort of thing for creating an array.


*I want to be able to call the function as many times i want to
without having to also put a declaration for each array at the top.
*basically i want a stand-alone function.
 
D

dave_mikesell

I'm trying to create a game engine

This may be off-topic, but is this for educational purposes? There
are some good, stable, open-source engines out there already
(Irrlicht, Ogre3d, Blitz3D, CrystalSpace, etc.).
 
J

Justcallmedrago

This may be off-topic, but is this for educational purposes? There
are some good, stable, open-source engines out there already
(Irrlicht, Ogre3d, Blitz3D, CrystalSpace, etc.).

yep.
 
S

Salt_Peter

Please don't shout!

Sorry :D
You can't. What problem are you trying to solve?

I'm trying to create a game engine and one of the functions i'm trying
to create involve loading a mesh and then having the materials for it
end up in an array.

void create_mesh(LPCWSTR path, char array arraynew) // just... kind of
ignore that second parameter.
{

// complicated stuff here to make arraynew work
LPD3DXBUFFER bufNew;

D3DXLoadMeshFromX (path, // load this file
D3DXMESH_SYSTEMMEM, // load the mesh into
system memory
d3ddev, // the Direct3D Device
NULL, // we aren't using adjacency
&bufNew, // put the materials here
NULL, // we aren't using effect instances
&numMaterials, // the number of materials in
this model
&meshSpaceship); // put the mesh here

// retrieve the pointer to the buffer containing the material
information
D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufNew-
GetBufferPointer();

// create a new material buffer for each material in the mesh
material = new D3DMATERIAL9[numMaterials];

for(DWORD i = 0; i < numMaterials; i++) // for each material...
{
arraynew = tempMaterials.MatD3D; // get the material
info
arraynew.Ambient = material.Diffuse; // make ambient
the same as diffuse
}

return;

so far the path parameter works. you can put any character string that
is a filename into it. ex: "mymeshfile.x"
i want to do the same sort of thing for creating an array.

*I want to be able to call the function as many times i want to
without having to also put a declaration for each array at the top.
*basically i want a stand-alone function.


Then you might consider explaining a fairly simple question without
throwing in 30 lines of irrelevent code.
Your question is quite difficult to answer because we don't know if
the array to be used is static.

Can you declare a local variable or container and use it outside the
function?
no
Why? cause it ceases to exist once the function terminates (unless its
static)

Can you allocate a variable or container on the heap and return a
pointer to it?
yes, but you are responsible for its deallocation and it severely
complicates code

Can you declare an array and pass it by reference for a function to
access and modify (not modifying just a copy)?
yes

#include <iostream>

template< typename N, const size_t Size >
void initialize(N (& r)[Size]) // pass array by reference
{
for( size_t t = 0; t < Size; ++t)
{
r[t] = t;
}
}

template< typename N, const size_t Size >
void display(const N (& r)[Size])
{
std::cout << "array size = " << Size;
for( size_t t = 0; t < Size; ++t)
{
std::cout << "\nr[" << t;
std::cout << "] = " << r[t];
}
std::cout << std::endl;
}

int main()
{
int array[8];
initialize(array);
// do work with array
display(array);
}

/*
array size = 8
r[0] = 0
r[1] = 1
r[2] = 2
r[3] = 3
r[4] = 4
r[5] = 5
r[6] = 6
r[7] = 7
*/

___
There has to be an easier way to do this?
yes there is - stop using primitive containers

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template< typename T >
void initialize(std::vector< T >& r_v)
{
// std::generate is a better option
for(size_t t = 0; t < r_v.size(); ++t)
{
r_v[t] = t;
}
}

template< typename T >
std::eek:stream&
operator<<(std::eek:stream& os, const std::vector< T >& r_v)
{
std::copy( r_v.begin(),
r_v.end(),
std::eek:stream_iterator< T >(os, "\n") );
return os;
}

int main()
{
std::vector< int > vn(8);
initialize(vn);
std::cout << vn;
}

/*
0
1
2
3
4
5
6
7
*/
 
P

patelvijayp

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

tragomaskhalos

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

Chinchilla

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

patelvijayp

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.
 

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,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top