List of initial Members



  • Hello, is it interesting to know if, with some variable mySpace, as well as a base class designer, it is possible to set a value from enum'a? The idea is to get rid of the parameter in the foo function.

    namespace mySpace{
       enum myEnum {val1=0, val2, val3, fin}; 
          const char *setVal(int par){
          static const char *valNames[fin]={"this val1","this val2","this val3"}; 
            if(par>=0 && par <fin)return valNames[fin];
            return "error"; 
         }
    }
    class Parent {
        public:
          virtual void foo(int par)=0;     
    };
    class Child : public Parent{
        public:
          void foo(int par){
             char name[64];
             sprintf(name, "%s", mySpace::setVal(par));
          }
    };
    


  • For example, create a field for this parameter and initiate it when setting up the facility (transfer to the designer):

    #define NAME_LENGTH 64
    

    namespace mySpace
    {
    enum myEnum {
    val1=0,
    val2,
    val3,
    fin
    };

    const char* setVal( int par )
    {
        static const char* valNames[ fin ] = {
            "this val1", "this val2", "this val3"
        }; 
    
        if ( par &gt;= 0 &amp;&amp; par &lt; fin )
            return valNames[ fin ];
    
        return "error"; 
    }
    

    }

    class Parent
    {
    public:
    virtual void foo()=0;
    private:
    int _par;
    };

    class Child : public Parent
    {
    public:
    Child( int parameter )
    {
    _par = parameter;
    }
    void foo()
    {
    char name[ NAME_LENGTH ];
    sprintf( name, "%s", mySpace::setVal( _par ) );
    }
    };




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2