Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Ganeric Class Define

Ganeric Class Define

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bestbear
    wrote on last edited by
    #1

    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

    L S 2 Replies Last reply
    0
    • B bestbear

      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

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • L leon de boer

        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

        B Offline
        B Offline
        bestbear
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • B bestbear

          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.

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • B bestbear

            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

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            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)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups