0

So in javascript we can access the member of any object by just using []. For example:

let obj = { foo : 'bar'};
let accessedField = obj["foo"];
obj["foo"] = "something"

How can I access a member of a namespace without knowing the name of the member beforehand like if I get it from somewhere else as input, a string/char* for example. Not necessarily looking for the operator[] implementation.

namespace Foo {
   int bar = 2;
}

Foo["bar"] = 3;

The reason is so I can dynamically access items and I don't want too use std::maps.

**EDIT: Just to clarify not necessarily the [] operator but any way to dynamically access a member of a namespace without knowing it beforehand.

  • 2
    Basically, you cant... – Sean Jan 26 '22 at 15:35
  • 7
    Advice -- You shouldn't try to make C++ look like another language. You'll wind up with code that looks weird to a C++ programmer. – PaulMcKenzie Jan 26 '22 at 15:35
  • 1
    Don't do direct translations between languages (spoken, written, or programming), such translations will almost never turn out good. Instead take the requirements, analysis, design and algorithms and reimplement in the destination language, using the features of the destination language and its libraries. – Some programmer dude Jan 26 '22 at 15:38
  • Sure, it's easy. You'll just need to use `std::any operator[](string const& key);` and map the key to member variable. Performance will be dreadful, and you'll need to unpack the returned `std::any`, so using it will be very awkward. – Eljay Jan 26 '22 at 15:40
  • @OP -- You don't want to be that person who, when you show your code to a C++ programmer, get the response "what the heck are you trying to do? Why didn't you simply do ?" Because that's the response you will get if you attempt to turn C++ into something that does not use proper C++ idioms and methodologies. The same thing can be said for a C++ programmer who tries to turn JavaScript into C++. They will get the same response from a JavaScript programmer. – PaulMcKenzie Jan 26 '22 at 15:48
  • 1
    "The reason is so I can dynamically access items and I don't want too use std::maps." But in Javascript you're using something like a std::map "under the hood" anyway. (Actually it's probably more like a std::unordered_map depending on your JS implementation, but tomato tomato.) – Daniel McLaury Jan 26 '22 at 15:49
  • _"The reason is so I can dynamically access items"_ A good question, then, might be "How can I dynamically access objects?" More effective than "How can I achieve that [Javascript syntax] in C++?" – Drew Dormann Jan 26 '22 at 15:57
  • @PaulMcKenzie just to clarify I don't want to implement javascript syntax in C++ so it doesn't have to be the [] operator necessarily. What I'm asking is accessing a member without knowing what it is beforehand and without having to write a 100 line long switch case statement to find it given an input. – Darren Rahnemoon Jan 26 '22 at 16:01
  • @DrewDormann I'm not looking for the same syntax. Just accessing member fields without knowing what it is before hand and without having to write a switch case statement that is 100 lines long. – Darren Rahnemoon Jan 26 '22 at 16:04
  • @DarrenRahnemoon you may be asking about a concept known as "introspection". Similar to [C++ Introspection techniques](https://stackoverflow.com/questions/16549493/c-introspection-techniques-similar-to-python). – Drew Dormann Jan 26 '22 at 16:07
  • 2
    @DarrenRahnemoon -- *What I'm asking is accessing a member without knowing what it is beforehand* -- Even though you say you don't want JavaScript syntax, you're still trying to implement what JavaScript does in C++. You should pretend that JavaScript never existed, and look at the problem with a fresh C++ mindset. Also, maybe the issue is that you have an [XY](https://xyproblem.info/) problem. If you don't know the items beforehand, then something that is totally different than your solution, more to a choosing of the right design patterns, would be more appropriate. – PaulMcKenzie Jan 26 '22 at 16:16
  • It is probably possible in C# more similar to JS as it has generics, C++ does not except some https://en.cppreference.com/w/cpp/language/typeid or dirty macro tricks. Anyway if you need this, you can use something similar like std::map, etc. But always you need to store name or type info somewhere/somehow and C++ will not help you much if at all... – Jan Jan 26 '22 at 17:35

0 Answers0