-8

Both Convert string to variable name or variable type and How to use a string as a variable name in C++? answers say you can not use a string as a variable after compile time. However, they mention that higher level languages such as python can. As I understand it, Python is written in C from this https://softwareengineering.stackexchange.com/questions/20988/why-is-python-written-in-c-and-not-in-c .

So if python is written in C, and it can use string as variable, then why cant people do this in C++.

I guess the question is not clear. From the analogies in comments,

If Python is made from C, I would assume there is some function written that allows Python to be dynamic. So why cant that functionality be written without all of the other parts of Python.

Community
  • 1
  • 1
user-2147482637
  • 2,115
  • 8
  • 35
  • 56
  • please learn difference between static-typed language and dynamic-typed language. – Bryan Chen Aug 01 '14 at 03:40
  • @BryanChen if python is written in C, then why cant the functionality of the string being dynamically typed be implemented – user-2147482637 Aug 01 '14 at 03:41
  • You can store a table of variable addresses with their string versions, and then look up the string in that table to find the variable – M.M Aug 01 '14 at 03:41
  • 2
    @user1938107 Just because language A is written in language B doesn't mean that language B is a superset of language A. I can conceivably write a C++ compiler using old, non-OO Visual Basic. Even a better example -- some C++ compilers are written in C. Does C have virtual functions or templates? – PaulMcKenzie Aug 01 '14 at 03:45
  • 3
    "So if python is written in C, and it can use string as variable, then why cant people do this in C++." -- A house is made of bricks. Bricks can be stacked. So why can't houses be stacked? – Jim Balter Aug 01 '14 at 03:52
  • People consist of cells. Cells are small. So why aren't people small? – Jim Balter Aug 01 '14 at 03:55
  • 1
    Fortran was used to navigate the early spaceships. So why can't Fortran orbit the earth? – PaulMcKenzie Aug 01 '14 at 03:55
  • 3
    __Every__ programming languages are executed as machine code. So every language programming must be same. please, have some common sense. – Bryan Chen Aug 01 '14 at 03:57
  • @BryanChen - Good general point. Any computer language can be written in low-level assembly language. – PaulMcKenzie Aug 01 '14 at 04:00
  • "is actually the question" -- No, the question is, why doesn't a programming language have some attribute of the programming language it was written in. The answer is that there is no necessary relationship between the attributes of a programming language and the attributes of a programming language implemented in it. – Jim Balter Aug 01 '14 at 04:01
  • @JimBalter No, it is not "why doesnt a programming language have some attribute of the programming language it was written in", the question is if a programming language came from a different one, why do people say that it is impossible to implement the same function with the original language – user-2147482637 Aug 01 '14 at 04:05
  • To the edit you made to your question, you can implement the functionality of strings as variable names, you can get the equivalent, you just can't extend C or C++s syntax to do it. – Keith Nicholas Aug 01 '14 at 04:06
  • "No, it is not" -- Yes it is. I've programmed for nearly 50 years, you've programmed for what, a day? – Jim Balter Aug 01 '14 at 04:06
  • @JimBalter "No, it is not" is referring to you claiming what my question is, which as it is my question, I know what I want to ask. If its not clear, I edit it. – user-2147482637 Aug 01 '14 at 04:08
  • 1
    "why do people say that it is impossible to implement the same function with the original language" -- No one has said that. You are confused, and are acting as if you know better than people far more experienced and knowledgeable that you are. – Jim Balter Aug 01 '14 at 04:08
  • Your question is the one you asked, not the one you wanted to ask ... we aren't mind readers. – Jim Balter Aug 01 '14 at 04:09
  • "implement the same function with the original language" -- being able to use strings as variable names is a **feature** of a language, not a *function* implemented in the language. Kuba Ober's answer below shows how you can get the same *functionality*, but it doesn't use strings as variable names. – Jim Balter Aug 01 '14 at 04:14
  • @JimBalter Yes, sorry. I had meant functionality – user-2147482637 Aug 01 '14 at 04:17
  • "So why cant that functionality be written without all of the other parts of Python." -- It can and no one ever said it can't. Your question is entirely based on your own misunderstandings and really ought to be deleted. " I had meant functionality" -- why, then, is there a compile-time tag? – Jim Balter Aug 01 '14 at 04:19
  • @JimBalter yes i can delete if you think so – user-2147482637 Aug 01 '14 at 04:29
  • 2
    The question you apparently want an answer to is "Can you map a string to a value in C++?" The answer is certainly yes, and some of the answers below show how. – Jim Balter Aug 01 '14 at 04:33
  • This question really should have been marked as a duplicate of one of the ones the OP linked to, since the responses to those questions cover all the issues here. – Jim Balter Aug 01 '14 at 04:56

4 Answers4

3

When a C++ program is compiled, the variables names aren't put into the resulting executable. Once the compiler has everything figured out, they are literally discarded. All that is left behind are the very low level pieces of assembly language instructions and memory addresses. So the language is fundamentally engineered in a way that doesn't allow for what you want.

Some other languages, like Python, preserve the name information. They don't compile in the same way as C++. There's usually a lot more interpreting at runtime happening. At some fundamental level, the variables in a Python program probably end up in some kind of underlying data structure - perhaps a map of names to values. That way the language can support on-the-fly lookups and such of variable names.

So at best in C++, you can engineer your own data structures or framework in a similar manner - to hold data with an associated name and then be able to query by name to retrieve the relevant data. (Or of course, you could search for someone else who has already implemented such a thing.)

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17
3

The CPython implementation - the implementation of Python written in C - uses the C language to implement a certain kind of variable lookup that makes Python behave like Python and not like, say, C or BASIC.

So, you can certainly use C or C++ to implement string-named variable lookup. Just don't expect the compiler to recognize it, because it's not part of the C/C++ language. The end product - the compiled executable - will support the concept, though.

For example, in C++, using Boost:

std::map<std::string, boost::variant<std::string, int>> _;

_["name"] = "user";
_["index"] = 1938107;

std::cout << _["name"] << _["index"] << std::endl;

Output:

user1938107

You can also make it interoperate with regular variables known to the compiler, if but there are no off-the-shelf classes to help you there.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

A screwdriver can't drill a hole. But you can use a screwdriver to build a drill.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

In another way, I think you CAN create some TYPE of variables from string or something using some trick, like in the Factory Design Pattern, but, that's not a language feature, you will need to register the type's name and the type's creator function first.

std::map<string, creating_function> lookup_table;
lookup_table["class_1"] = &class_1_creator;  // register type and creator

obj_instance& Create(string type_name, parameters_passed_to_the_creator) // create a variable
{
    return lookup_table[type_name](parameters_passed_to_the_creator);
}
dguan
  • 1,023
  • 1
  • 9
  • 21