类问题中的C ++变量数组 [英] C++ Variable array in a Class Problem

查看:168
本文介绍了类问题中的C ++变量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



函数initModulation将创建一个大小为M的int数组和一个大小为M的复杂数组(Complex是另一个类,由一个实部和虚部组成)。



函数modulate需要能够访问这两个数组。在调用init调制函数之后,这两个数组超出范围。为了避免这种情况,我只是使这两个数据成员的Modulator类,但是我不能这样做,因为数组大小取决于M。

  class Modulator 
{
int M;
double phase;
std :: string mapping;

public:
void initModulation(int M,double phase,std :: string mapping);
double * modulate(int * input,int inputlength,int complexFlag);
};

任何想法?




Minh

解决方案


  1. 情况;


  2. 数组是类数据的逻辑部分,因为它们用于调制,这就是类的实例。


  3. 您打算创建未知编译时大小的数组,对吧?无论你是否将他们存储为类的成员如何?无论如何,你有这方面的M的信息,并相应地使用它。


  4. 但是你不应该分配你自己的数组。使用 std :: vector 。毕竟,你足够聪明地使用 std :: string 来存储文本数据,因此...


  5. 为什么你会使用一个 int 一个参数,应该是某种标志? C ++有一个真正的布尔类型,称为 bool


  6. 对于调制的输入和输出,再次使用向量。


  7. p>具有其名称与类名称对齐的成员函数的类是可疑的。许多语言支持可调用对象的想法,C ++在其中。在C ++中,我们拼写此功能 operator()


因此:

  class Modulator 
{
std :: vector< int& int_data;
std :: vector< Complex> complex_data;
double phase;
std :: string mapping;

public:
调制器(int M,double phase,std :: string mapping):
int_data(M),complex_data(M)映射){}

std :: vector< double> operator()(const std :: vector< int>& input,bool is_complex);
};

欢迎使用现代C ++。 :)


I was wondering if there was a way to include a data member which is an array of unfixed size.

The function initModulation will create an int array of size M and a Complex array of size M. (Complex is another class and is made up of a real component and imaginary component).

The function modulate will need to be able to access these two arrays. These two arrays go out of scope after the init Modulation function is called. To avoid this, I would just make these two data members of the Modulator class, however I can't do that because the array size depends on M.

class Modulator
{
    int M;
    double phase;
    std::string mapping;

public:
    void initModulation(int M, double phase, std::string mapping);
    double* modulate(int *input,int inputlength,int complexFlag);
};

Any ideas around this?

Thanks, Minh

解决方案

  1. Don't write initialization functions without very special circumstances; construct with the constructor.

  2. The arrays are logical part of the class data, since they are used for modulation, and that's what instances of the class do. So you should have them as members.

  3. You were planning to create arrays of unknown-at-compile-time size, right? How does it matter whether you're storing them as members of the class or not? Either way, you have the information about M at this point, and use it accordingly.

  4. But you shouldn't be allocating your own arrays, anyway. Use std::vector. After all, you're smart enough to use std::string for text data, so...

  5. Why would you use an int for a parameter that's supposedly some sort of "flag"? C++ has a real boolean type, called bool. Use it.

  6. For the input and output of modulation, again, use vectors.

  7. Having a class with a member function whose name aligns with the class name is suspicious. Many languages support the idea of a "callable" object and C++ is among them. In C++, we spell this functionality "operator()".

Thus:

class Modulator
{
    std::vector<int> int_data;
    std::vector<Complex> complex_data;
    double phase;
    std::string mapping;

    public:
    Modulator(int M, double phase, std::string mapping): 
    int_data(M), complex_data(M), phase(phase), mapping(mapping) {}

    std::vector<double> operator()(const std::vector<int>& input, bool is_complex);
};

Welcome to modern C++. :)

这篇关于类问题中的C ++变量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆