Ganeric Class Define
-
class CObject
{
public:
bool SetInt(string strPropName,int nValue);
private:
map m_properties;private:
std::string m_strClassName;
};for example,If CObject has a "Hello" property,I can set it's value like this:
CObject* pObject = new CObject("switch");
pObject->SetInt("Hello",123);but It is not very good enough,if I code like this:
pObject->SetInt("Helloooooooo",123);
here,I write the wrong property name ,but the code wouldn't generate any error at compile time. ******************************************************************* 1 there is a lot class defined by string 2 there may be a lot of objects defined by any class 3 there is a lot of properties for each object My question is how could I avoid the above mistake at compile time? Thank you all
-
class CObject
{
public:
bool SetInt(string strPropName,int nValue);
private:
map m_properties;private:
std::string m_strClassName;
};for example,If CObject has a "Hello" property,I can set it's value like this:
CObject* pObject = new CObject("switch");
pObject->SetInt("Hello",123);but It is not very good enough,if I code like this:
pObject->SetInt("Helloooooooo",123);
here,I write the wrong property name ,but the code wouldn't generate any error at compile time. ******************************************************************* 1 there is a lot class defined by string 2 there may be a lot of objects defined by any class 3 there is a lot of properties for each object My question is how could I avoid the above mistake at compile time? Thank you all
One pretty obvious way is make a set of strings of valid property strings
std::set s;
s.insert("Hello");Then all you need to do is use the standard set find call to know if its there
std:string TestCmd = "Hellooooooooo";
if (s.find(TestCmd) == s.end()){
// Your property name isnt in the property set list
}Personally I wouldn't use a string for the property it's not going to be fast to enumerate but that is an exact description of what you are asking. I would do the same as windows use something like a CRC to hash the strings to integer. Much faster to search for an integer ID than a string. However if you want to google away the question is "Fastest way to check if a string exists in a set of strings"
In vino veritas
-
One pretty obvious way is make a set of strings of valid property strings
std::set s;
s.insert("Hello");Then all you need to do is use the standard set find call to know if its there
std:string TestCmd = "Hellooooooooo";
if (s.find(TestCmd) == s.end()){
// Your property name isnt in the property set list
}Personally I wouldn't use a string for the property it's not going to be fast to enumerate but that is an exact description of what you are asking. I would do the same as windows use something like a CRC to hash the strings to integer. Much faster to search for an integer ID than a string. However if you want to google away the question is "Fastest way to check if a string exists in a set of strings"
In vino veritas
-
Thank you I could check whether a property is in the set or not at run time. but I want to check it at compile time. I just write "Helloooooooo" instead of "Hello" by mistake. I want to find out this mistake when I compile it.
If you are on a C++11 compiler you are defining a compile time helper function. If you aren't you are dead in the water. Search "function that accepts only compile time known expressions" If you are on Visual Studio, I have never played with it but I think you want this. <type_traits>[^]
In vino veritas
-
class CObject
{
public:
bool SetInt(string strPropName,int nValue);
private:
map m_properties;private:
std::string m_strClassName;
};for example,If CObject has a "Hello" property,I can set it's value like this:
CObject* pObject = new CObject("switch");
pObject->SetInt("Hello",123);but It is not very good enough,if I code like this:
pObject->SetInt("Helloooooooo",123);
here,I write the wrong property name ,but the code wouldn't generate any error at compile time. ******************************************************************* 1 there is a lot class defined by string 2 there may be a lot of objects defined by any class 3 there is a lot of properties for each object My question is how could I avoid the above mistake at compile time? Thank you all
The only way to provoke a compiler error would be to pass the knowledge of the full list of class properties to the compiler, at compile time. That would defeat the purpose of a generic class definition, and you could just as well define simple member variables instead. This raises the question: where do your requirements come from, i. e the two requirements to define a generic class, and to force compile time errors when accessing an incorrectly labeled property? One of them has to go.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)