English 中文(简体)
瞬时变数的一组C++级变量
原标题:Set variable type for a c++ class variable during instantiation

I have a c++ class such as the following:

class some_class {

    protected:
        decide_later some_variable;

    public:
        void some_random_function();

};

void some_class::some_random_function() {

    decide_later another_variable;

}

问题在于,我不知道,在我提出这几类情况之前,哪些类型的变数是可变的。 我想做的是:

some_class class_instance(std::string);

这将决定如何使用 st子:扼杀(或 double,或翻一番,或听从使用)。 此外,如果我可以决定“later”为后来担任该类成员的其他职务时的变型。 例如,在这项职能中,有一部分是“功能”。

我尝试利用动力:任何事情都这样做,但似乎只是为了数字类型。 此外,我认为,如果我能够实际确定实际变量类型,就会更加有效。

这是可能的吗?

最佳回答

请参看项目>。 1. 宣布你为:

template <typename T> class some_class {

    protected:
        T some_variable;

    public:
        void some_random_function() {
            T another_variable;
        }

};

并随函附上:

some_class<std::string> class_instance();
问题回答

如果你在创建时静地知道这种类型,那么你就应当使用C++模板,并将该类型作为模板参数。

如果你仅能动态地知道(即根据用户投入),那么你可以分门别类,即时即刻进行模板,或者可以找到一个完全动态的解决办法(如:任何或同等的)。

然而,我认为,如果你能够用模板来做到这一点,那么你在方案设计上就会出现一些错误。 C++静态打字的想法是,在汇编时间时知道这些类型。 通常在以目标为导向的设计中,你会使用多形态,而不是软弱的打字。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签