Special Case: Creating a variable in a function?

Discussion in 'C++' started by Justcallmedrago@yahoo.com, Apr 17, 2007.

  1. Guest

    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.
    , Apr 17, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > 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?

    --
    Ian Collins.
    Ian Collins, Apr 17, 2007
    #2
    1. Advertising

  3. Gaijinco Guest

    Your question is very hard to understand. Can you try to be a little
    clearer in your problem? Maybe some code.
    Gaijinco, Apr 17, 2007
    #3
  4. Ian Collins Guest

    Gaijinco wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Apr 17, 2007
    #4
  5. wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 17, 2007
    #5
  6. Gaijinco <> writes:

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

    --
    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Sherm Pendley, Apr 17, 2007
    #6
  7. Guest

    On Apr 16, 8:08 pm, Ian Collins <> wrote:
    > wrote:
    > > How would you declare and assign a variable inside a function THAT HAS
    > > THE NAME OF A PARAMETER YOU PASSED

    >
    > Please don't shout!


    Sorry :D

    > > 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?



    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.
    , Apr 17, 2007
    #7
  8. Guest

    On Apr 16, 9:23 pm, wrote:

    >
    > > You can't. What problem are you trying to solve?

    >
    > 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.).
    , Apr 17, 2007
    #8
  9. Guest

    On Apr 16, 9:40 pm, wrote:
    > On Apr 16, 9:23 pm, wrote:
    >
    >
    >
    > > > You can't. What problem are you trying to solve?

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


    yep.
    , Apr 17, 2007
    #9
  10. Salt_Peter Guest

    On Apr 16, 9:23 pm, wrote:
    > On Apr 16, 8:08 pm, Ian Collins <> wrote:
    >
    > > wrote:
    > > > How would you declare and assign a variable inside a function THAT HAS
    > > > THE NAME OF A PARAMETER YOU PASSED

    >
    > > Please don't shout!

    >
    > Sorry :D
    >
    > > > 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?

    >
    > 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
    */
    Salt_Peter, Apr 17, 2007
    #10
  11. Guest

    On Apr 17, 5:58 am, 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.
    , Apr 17, 2007
    #11
  12. On Apr 17, 9:46 am, 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?).
    tragomaskhalos, Apr 17, 2007
    #12
  13. Chinchilla Guest

    On Apr 16, 8:58 pm, 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.
    Chinchilla, Apr 18, 2007
    #13
  14. patelvijayp Guest

    On Apr 17, 4:42 pm, tragomaskhalos <>
    wrote:
    > On Apr 17, 9:46 am, 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.
    patelvijayp, Apr 18, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joker
    Replies:
    3
    Views:
    370
  2. Replies:
    0
    Views:
    285
  3. Robert Latest

    Equality of floating-point numbers (special case)

    Robert Latest, Apr 19, 2006, in forum: C Programming
    Replies:
    14
    Views:
    471
    P.J. Plauger
    Apr 20, 2006
  4. Replies:
    2
    Views:
    285
  5. Shane Emmons
    Replies:
    2
    Views:
    113
    Adam Prescott
    Apr 14, 2011
Loading...

Share This Page