开发者

Ways to use variable as object name in c/c++

开发者 https://www.devze.com 2023-03-25 02:16 出处:网络
Just out of curiosity: is there a way to use variable as object name in c++? something along the lines:

Just out of curiosity: is there a way to use variable as object name in c++?

something along the lines:

char a[] = "testme\0";
*a *vr = new *a();

If you were to write a c/c++ compiler how would you go about to implement such a th开发者_StackOverflowing?

I know they implemented this feature in zend engine but to lazy to look it up.

Maybe some of you guys can enlight me :)


In case what you are looking for is something like this

<?php
$className = "ClassName";
$instance = new $className();
?>

That's simply not possible in C++. This fails for many reasons, one of them that C++ at runtime doesn't know much about names of classes anymore (only in debug mode) If somebody wanted to write a compiler that would allow something like this, it would be necessary to keep a lot of information that a C++ compiler only needs during compilation and linking. Changing this would create a new language.

If you want to dynamically create classes depending on information only available at runtime, in C++ you would most likely use some of the Creational Design Patterns.

Edit:

PHP is one language, C++ is a very different one. 16M may not be that much nowadays, for a C++ programmer where some programs are in the k range, it's a whole world. Nobody wants to ship a complete compiler with his C++ app to be able to get all the dynamic features (that btw PHP too implements only in a limited way as far as I know, if you want really dynamic runtime code creation, have a look at Ruby or Python). C++ has (as all languages) a certain philosophy and creating objects by name in a string doesn't fit very well with it. This feature alone is quite useless anyway and would by no means justify the overhead necessary to implement it. This could most likely be done without adding runtime compilation, but even the extra kilobytes necessary to store the names alone make no sense in the C++ world. And C++ is strictly typed and this functionality would have to make sure, that type checking doesn't break.


In C and C++, identifier names do not have the same meaning they do in PHP.

PHP is a dynamic language, and (at least conceptually) runs in an interpreted context. Identifier names are present at run time, they can be inspected through PHP's reflection features, you can use strings to refer to functions, variables, globals, and object properties by name, etc. PHP identifiers are actual semantic entities.

In C++, identifiers are lost at run time (again, conceptually speaking). You use them in your source code to identify variables, functions, classes, etc., but the compiler translates them into memory addresses or literal values, or even optimizes them away completely. Identifier names are not generally present in the compiled binary (unless you instructed the compiler to include debug symbols), and there is no way to inspect them at run-time. Even with RTTI, the best you can get is an arbitrary number to identify a type; you can compare them for equality, but you cannot get the name back.

Consequently, if you want to translate strings into identifier names at run-time in C++, you have to perform the mapping manually. std::map can be a great help for this - you hand it a string, and it gives you a value. This doesn't work directly for class names; for these, you need to implement some sort of factory method. A nice solution is to have one wrapper function for each type, and then a std::map that maps class names to the corresponding wrappers. Something like:

map<string, FoobarFactoryMethod> factory_map;

Foobar* FooFactory() { return new Foo(); }
Foobar* BarFactory() { return new Bar(); }
Foobar* BazFactory() { return new Baz(); }

void fill_map() {
    factory_map["Foo"] = FooFactory;
    factory_map["Bar"] = BarFactory;
    factory_map["Baz"] = BazFactory;
}

// and then later:
Foobar* f = factory_map[classname]();


Why do you even want to have this feature? You are most likely misusing OOP. Whenever my needs ran into hard language barriers like this I ended up doing one of the following:

  • Rethink your solution to the problem so it fits OOP better
  • Create a DSL for your problem (domain specific language)
  • Create a code generator for this part of your problem
  • Pick a language that fits your problem better

  • A combination of the above


I would think that what you want to do would be best accomplished using interfaces and a factory pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消