Basically, based on whether a user inputs a 0 or 1 as a command line argument, I want my data class to either have a vector or an array (static) as its data member. I don't believe you can add if statements to .h files, but I'd like to do the following:
基本上,根据用户是否输入0或1作为命令行参数,我希望我的数据类有一个向量或一个数组(静态)作为其数据成员。我不相信你可以在.h文件中添加if语句,但我想做以下事情:
/*A.h*/
//various functions
private:
if(argv[1] == 0)
vector b;
else if(argv[1] == 1)
array b[10];
Is there a simple way to do this? Thanks!
有一个简单的方法吗?谢谢!
3 个解决方案
#1
C-type arrays and std::vector have very different interfaces, so even if you were able to do this, it would be tough to write meaningful code, as vector has push_back(), empty(), etc., and arrays do not.
C类型数组和std :: vector有非常不同的接口,所以即使你能够做到这一点,编写有意义的代码也很困难,因为vector有push_back(),empty()等,而数组也有不。
What you are looking for is one object type with a consistent interface, that can have multiple implementations under-the-hood (it can be implemented using a C-style array, or a C++ standard vector). This is called polymorphism and a tutorial of how to achieve this with C++ can be found here: http://www.cplusplus.com/doc/tutorial/polymorphism/
您正在寻找的是具有一致接口的一种对象类型,可以在引擎盖下具有多个实现(可以使用C样式数组或C ++标准向量来实现)。这称为多态,有关如何使用C ++实现此目的的教程可以在这里找到:http://www.cplusplus.com/doc/tutorial/polymorphism/
#2
One way to achieve that would be to use a template helper function. However note that this may lead to much code duplication if the code is complicated. But as I guess it's for something like performance measurement of different implementation methods (why else would the program user care how it is implemented), I guess that the code isn't too complicated anyway.
实现这一目标的一种方法是使用模板辅助函数。但请注意,如果代码很复杂,这可能会导致代码重复。但是我认为这是针对不同实现方法的性能测量(为什么程序用户会如何实现它),我想这个代码并不是太复杂。
So for example:
例如:
template<typename type_to_use> int template_main(int argc, char* argv[])
{
type_to_use b;
// ...
}
int main(int argc, char* argv[])
{
if (argv[1]==0)
template_main<vector>(argc, argv);
else
template_main<int[10]>(argc, argv);
}
#3
Is there a simple way to do this?
有一个简单的方法吗?
Yes: you create two different implementations with a common interface, defined by a base class. Then, you use a class factory to instantiate one of them, based on runtime parameters.
是:您使用由基类定义的公共接口创建两个不同的实现。然后,使用类工厂根据运行时参数实例化其中一个工厂。
class data {
public:
virtual ~data() = 0;
// define public interface here
};
class vector_data: public data {
std::vector<int> values;
public:
// implement public interface here in terms of values
};
class array_data: public data {
int values[20]; // or, std::array<int, 10> values;
public:
// implement public interface here in terms of values
};
std::unique_ptr<data> make_data(bool vector_data)
{
if(vector_data)
return { new vector_data() };
return { new array_data(); }
}
int main(int argc, char* argv[])
{
std::vector<std::string> args{ argv, argv + argc };
auto data = make_data( args[1] == "vector" };
}
data will be instantiated to vector data only when you call:
只有在您调用时,数据才会被实例化为矢量数据:
> application.exe vector
#1
C-type arrays and std::vector have very different interfaces, so even if you were able to do this, it would be tough to write meaningful code, as vector has push_back(), empty(), etc., and arrays do not.
C类型数组和std :: vector有非常不同的接口,所以即使你能够做到这一点,编写有意义的代码也很困难,因为vector有push_back(),empty()等,而数组也有不。
What you are looking for is one object type with a consistent interface, that can have multiple implementations under-the-hood (it can be implemented using a C-style array, or a C++ standard vector). This is called polymorphism and a tutorial of how to achieve this with C++ can be found here: http://www.cplusplus.com/doc/tutorial/polymorphism/
您正在寻找的是具有一致接口的一种对象类型,可以在引擎盖下具有多个实现(可以使用C样式数组或C ++标准向量来实现)。这称为多态,有关如何使用C ++实现此目的的教程可以在这里找到:http://www.cplusplus.com/doc/tutorial/polymorphism/
#2
One way to achieve that would be to use a template helper function. However note that this may lead to much code duplication if the code is complicated. But as I guess it's for something like performance measurement of different implementation methods (why else would the program user care how it is implemented), I guess that the code isn't too complicated anyway.
实现这一目标的一种方法是使用模板辅助函数。但请注意,如果代码很复杂,这可能会导致代码重复。但是我认为这是针对不同实现方法的性能测量(为什么程序用户会如何实现它),我想这个代码并不是太复杂。
So for example:
例如:
template<typename type_to_use> int template_main(int argc, char* argv[])
{
type_to_use b;
// ...
}
int main(int argc, char* argv[])
{
if (argv[1]==0)
template_main<vector>(argc, argv);
else
template_main<int[10]>(argc, argv);
}
#3
Is there a simple way to do this?
有一个简单的方法吗?
Yes: you create two different implementations with a common interface, defined by a base class. Then, you use a class factory to instantiate one of them, based on runtime parameters.
是:您使用由基类定义的公共接口创建两个不同的实现。然后,使用类工厂根据运行时参数实例化其中一个工厂。
class data {
public:
virtual ~data() = 0;
// define public interface here
};
class vector_data: public data {
std::vector<int> values;
public:
// implement public interface here in terms of values
};
class array_data: public data {
int values[20]; // or, std::array<int, 10> values;
public:
// implement public interface here in terms of values
};
std::unique_ptr<data> make_data(bool vector_data)
{
if(vector_data)
return { new vector_data() };
return { new array_data(); }
}
int main(int argc, char* argv[])
{
std::vector<std::string> args{ argv, argv + argc };
auto data = make_data( args[1] == "vector" };
}
data will be instantiated to vector data only when you call:
只有在您调用时,数据才会被实例化为矢量数据:
> application.exe vector