How to initialize variable dynamically in class declaration, and being not constant

Viewed 158

How to initialize variable(s) dynamically i.e. being not as constant, in class declaration which will be invoked/used without need of object instantiation such as a constructor for member function call immediately gets returned in a function ?
There must, side by side of that variable(s) in that class, be a constant member.

enum Value :int { NO,LOW,MED,HIGH };

template<Value V> class valuation {

public:
    valuation(const ints) : pos(s) {};
    valuation(int a, int b, int c) : j(a), k(b), l(c) {};
    value() {
        if (!V)
        return (j+k+l) * pos;
    }

private:    
    const int pos;
    int j,k,l;
};


int a_function(int s) {
    return valuation<NO>(s).value();  // no need object at all
}


int main() {
    // somehow in run-time j, k, l has been given 1,2,3, or other valid values
    int v = a_function(7)   // e.g.
}

Thanks in advance

1 Answers

I think you want to declare your value method -- or at least a version of it -- as a static member. This will allow you to call the method without instantiating an object from your class.

Related