Archived

This topic is now archived and is closed to further replies.

winper

Using a var for an objects name.

Recommended Posts

Is there a way to use a variable for an objects name. The code below is an example of how I been trying to do it. Any and all help is appreciated. Main.CPP #include <iostream.h> #include "item.h" void printNum(char); int main() { printNum(''iOne''); return 0; } void printNum(char itemName) { cout << itemName.getNum(); } item.h class Item { private: int itemNum; public: void changeNum(int); int getNum(); }; void Item::changeNum(int x) { itemNum = x; } int Item::getNum() { return itemNum; } Item iOne;

Share this post


Link to post
Share on other sites
No. Objects only have names at compile time, variables only have values at run time. So what you are asking for is meaningless.

An alternative that may be appropriate for what you want to do is to use some container like a map to associate strings with object pointers.

[edited by - Plasmadog on November 16, 2003 5:50:23 PM]

Share this post


Link to post
Share on other sites
Unless you explicitely create a mapping from names to variables, there is no way (in C++) to directly do what you are trying to achieve.
As the AP above mentioned, a variable''s "name" is its address, that is to say, a pointer to its memory location.
However, the C++ standard library includes an std::map container class that makes the task feasible.


#include <iostream>
#include <string>
#include <map>

#include "item.h"

int main()
{
std::map<std::string, Item*> items;
items["iOne"] = Item();

std::cout << items["iOne"].getNum() << std::endl;
}


Additionally, please note that there is no <iostream.h> header in standard C++. There is, however, an <iostream> header, which places its symbols in the std namespace.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Also please note that the STL sucks balls.


Care to elaborate ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
quote:
Original post by Anonymous Poster
Also please note that the STL sucks balls.


Care to elaborate ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]



what i think he ment to say was it''s way to complicated for him to figure out and therefore it sucks balls. *rolls eyes*

Share this post


Link to post
Share on other sites
Not automatic but you can do something like this

(excuse my stl, still getting to grips with it, but you get the idea)


class Item
{
static std::map AssosArray;
public:
static Item *GetByName(const char *pName) { return AssosArray[pName]; }
Item(const char *pName) {
AssosArray[pName] = this;
}
}





you then declare your global class variable like this :


Item iOne("iOne");



and your printNum func


void printNum(const char *itemName)
{
cout << Item::GetByName(ItemName)->getNum();
}


I have left out error checking (making sure that the name has been registered) in the interests of clarity

Share this post


Link to post
Share on other sites