Set variable type for C ++ class variable during instance creation

I have a C ++ class, for example:

class some_class {

    protected:
        decide_later some_variable;

    public:
        void some_random_function();

};

void some_class::some_random_function() {

    decide_later another_variable;

}

The problem is that I don’t know what type of some_variable variable will be until I instantiate the class. I want to do the following:

some_class class_instance(std::string);

And that would establish that rule_later would use std :: string (either int, or double, or something that he was told to use). In addition, it would be great if later I could use solve_later as a variable type in other functions that are members of the class. For example, in some_random_function () function.

I tried using boost :: any to do this, but it seems to work only for numeric types. Also, I think it would be more efficient if I could really set the type of the variable.

Is it possible?

+3
2

. :

template <typename T> class some_class {

    protected:
        T some_variable;

    public:
        void some_random_function() {
            T another_variable;
        }

};

:

some_class<std::string> class_instance();
+7

, ++ .

(.. ), , , (, boost:: any equal).

, , , - . ++ , . - .

+2

All Articles