defining int with variable name

Hi,
i am trying to make a program that can set a variable by a non-static name,

something like this:

string variable_with_name_of_int = test
int (contents of variable_with_name_of__int) = value;


and then there has to be an int with the name "test"
does anyone know how i could do that?

Thanks in advance, modkip.
There's no way to directly do that. In fact, I'm not sure why you'd ever want to.

I suppose you could create a std::map<std::string, int> and sort of do it that way. Again, not sure why, though. What's the use case here?
> i am trying to make a program that can set a variable by a non-static name
> ... and then there has to be an int with the name "test"

Why do you need this? There probably is a simpler solution to the problem.

If it has to be done, use a lookup table.

Something along these lines, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include <stdexcept>

int& get( const std::map< std::string, std::reference_wrapper<int> >& name_map, const std::string& name )
{
    const auto iter = name_map.find(name) ;
    if( iter == name_map.end() ) throw std::domain_error( "no such name" ) ;
    return iter->second.get() ;
}

int main()
{
    // http://en.cppreference.com/w/cpp/container/map
    // http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
    std::map< std::string, std::reference_wrapper<int> > name_map ;

    int first = 100 ;
    int second = 222 ;

    name_map.emplace( "first", first ) ;
    name_map.emplace( "second", second ) ;
    name_map.emplace( "alias_for_first", first ) ;

    std::cout << get( name_map, "first" ) << ' ' << get( name_map, "alias_for_first" ) << '\n' ;
    get( name_map, "second" ) += 1000 ;
    std::cout << get( name_map, "second" ) << ' ' << second << '\n' ;
    
    try { get( name_map, "not there" ) = 0 ; }
    catch( const std::domain_error& e ) { std::cerr << "error: " << e.what() << '\n' ; }
}

http://coliru.stacked-crooked.com/a/3ed0be5faa8d7ce5
i am trying to retrieve a string from a file,
if it contains "set" it should define a variable, a string if the next word is "string" a int if its "number" this is as far as i come, then it should define a int/string with as name the next word

it looks like this (this is NOT c++)
set(string, test = "test is populated")

my code successfully converts it to
set, string, test, "test is populated"

and now i want the code to do this
string test = "test is populated";

is there any way i could do this? (i hope it's clear this time)
That's commonly called a symbol table. Use a map as others have suggested.
> ... and then there has to be an int with the name "test"

This can be logical association of a name to a variable.
Instead of a variable int test = 23 ; we can have an entry in the symbol table where the key is the name of the variable - 'test' in this case - mapped to the value of the variable - integer 23 in this case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <map>
#include <string>
#include <iomanip>

// a variable, a string if the next word is "string" a int if its "number"
// we could use a union-like-class here, but we will keep it simple for now
struct variable // string or number
{
    enum var_type { STRING, INT };
    var_type type ; // the type of the variable

    std::string str ; // we'll use this member if type==STRING; ignore it otherwise
    int number ; // we'll use this member if type==INT; ignore it otherwise

    // for convenience, we will write a pair of constructors
    variable( int i = 0 ) : type(INT), number(i) {} ; // construct with a number, also the default constructor
    variable( const std::string& s ) : type(STRING), str(s) {} // construct with a string
};

// we need to keep track of the names and values of variables.
// a map where the key is the name of the variable, and the mapped value is the variable itself
// for convenience, we will use a type-alias
using symbol_table = std::map< std::string, variable > ;

// my code successfully converts it to set, string, test, "test is populated"
// and now i want the code to do this: string test = "test is populated";
void set_value( symbol_table& sym_tab, const std::string& varable_name, const std::string& str_value )
{
    // look up the variable_name, if present update it to STRING with value str_value
    // if the variable_name is not found, create it and set the value to STRING, str_value
    sym_tab[varable_name] = str_value ; // note: converting constructor variable( const std::string& )
}

// and another function to set INT values
void set_value( symbol_table& sym_tab, const std::string& varable_name, int int_value )
{
    sym_tab[varable_name] = int_value ; // note: converting constructor variable(int)
}

// helper functions to print out the contents of the symbol_table
// first, a function to print out a single variable
std::ostream& operator<< ( std::ostream& stm, const variable& var )
{
    if( var.type == variable::INT ) return stm << "type: INT value: " << var.number ;
    else return stm << "type: STRING value: " << std::quoted(var.str) ;
}

// print symbol table
std::ostream& operator<< ( std::ostream& stm, const symbol_table& sym_tab )
{
    for( const auto& pair : sym_tab ) // for each name-variable pair in sym_tab
    {
        stm << "name: " << std::quoted(pair.first) <<  ' ' // print out its name
            << pair.second << '\n' ; // and its value
    }
    return stm ;
}

int main()
{
    symbol_table sym_tab ;

    // set, string, test, "test is populated"
    set_value( sym_tab, "test", "test is populated" ) ;

    // set, number, count, "73"
    set_value( sym_tab, "count", std::stoi("73") ) ;

    set_value( sym_tab, "name", "modkip" ) ; // set, string, name, "modkip"
    set_value( sym_tab, "num_posts", std::stoi("2") ) ; // set, number, num_posts, "2"


    // print out the contents of sym_tab
    std::cout << sym_tab << '\n' ;

    // change the value of test to "this is the new value"
    set_value( sym_tab, "test", "this is the new value" ) ;

    // change the type and value of variable "count" to string, "one million"
    set_value( sym_tab, "count", "one million" ) ;

    // print out the contents of sym_tab
    std::cout << "------------------\n" << sym_tab << '\n' ;
}

http://coliru.stacked-crooked.com/a/7ea075712ffcd49e

I presume you are writing a parser of some kind; take it up from here.
Thanks JLBorges, this is exactly what i was searching for!
I have not yet tested it but im sure it will work this way
Thanks!
Topic archived. No new replies allowed.