简体   繁体   English

在编译时具有未知类型的对象的类

[英]Class with an object of type unknown at the compilation time

i am sure this problem was touched here many times but i really can't find an answer to the type of problem i have. 我相信这个问题在这里多次触及,但我真的找不到我遇到的问题类型的答案。 I have a class that needs to hold an array inside. 我有一个需要在里面装一个数组的类。 But the type of that array is dependable (to be exact, it depends on what is the bit rate of the opened wave file, for example 8 bit - char, 16 bit - short etc.). 但是该数组的类型是可靠的(确切地说,它取决于打开的波形文件的比特率,例如8位 - 字符,16位 - 短等)。 I need it to be defined in one of the class method. 我需要在其中一个类方法中定义它。

My idea was to use the auto keyword to declare the pointer: 我的想法是使用auto关键字来声明指针:

class WaveReader {

//

auto *data;

};

and then, inside the method: 然后,在方法内:

void some_func(int datasize)
{
    //
    case 8:
      data = new char[datasize];
      break;
    case 16:
      data = new short[datasize];
      break;
    //
    etc.
}

but that was a stupid idea. 但这是一个愚蠢的想法。 I know the easiest way would be to declare arrays for each type but i would like to know if there's a smart way, maybe with some template use? 我知道最简单的方法是为每种类型声明数组,但我想知道是否有一种聪明的方法,也许使用一些模板? Thanks a lot for any help. 非常感谢您的帮助。

The auto keyword is not designed for this, declare data as: auto关键字不是为此而设计的,将数据声明为:

void* data;

Then, you can just use an int or enum to keep track of the type of data. 然后,您可以使用int或enum来跟踪数据类型。 Such as, 如,

typedef enum 
{
    CHAR,
    SHORT
} DataTypeEnum;
...
DataTypeEnum dataType;
...

and modify your code as follows: 并修改您的代码如下:

void some_func(int datasize)
{
    //
    case 8:
      data = static_cast<void*>(new char[datasize]);
      dataType = CHAR;
      break;
    case 16:
      data = static_cast<void*>(new short[datasize]);
      dataType = SHORT;
      break;
    //
    etc.
}
...
if( dataType == CHAR )
{
    ...
}
else if ( dataType == SHORT )
{
    ...
}

You probably want to think about "what does this mean". 你可能想要考虑“这是什么意思”。 Yes, there are situations when this type (solved as by jsidhu, for example) of thing is exactly what you want and need. 是的,有些情况下,这种类型(例如由jsidhu解决)的东西正是你想要和需要的。 But quite often it indicates that you are trying to do soemthing "not quite the right way". 但通常它表明你试图做“不太正确的方式”。

One alternative is to use virtual functions: 一种替代方法是使用虚函数:

class Base
{
   public: 
    virtual void do_stuff_with_data() = 0;
}

class CharData
{
  private:
    char * data;

  public:
    CharData(size_t size) { data = new char[size]; }

    void do_stuff_with_data() { ... }; 
};

class ShortData
{
  private:
    short* data;

  public:
    ShortData(size_t size) { data = new short[size]; }
    void do_stuff_with_data() { ... }; 
};

void some_func(int datasize)
{
    Base *pBase;
    case 8:
      pBase = new CharData(datasize);
      break;
    case 16:
      pBase = new ShortData(datasize);
      break;
    //
    etc.

    pBase->do_stuff_with_data(); 
}

Multiple datatypes. 多种数据类型。 You can make separate overloaded functions with the same name. 您可以使用相同的名称创建单独的重载函数。 Depending on the type of data passed, it will use the right function. 根据传递的数据类型,它将使用正确的功能。 The function can then call the right class from with in. 然后,该函数可以使用in调用正确的类。

I would use a union to to the job: 我会用工会来完成工作:

union {
    int8_t *i8;
    int16_t *i16;
    /* ... */
} data;

// snip

switch (bitsPerSample) {
case 8:
     dataType = CHAR;
     break;
case 16:
     dataType = SHORT;
     break;
default:
     // throw
}

data.i8 = new int8_t[datasize * bitsPerSample / CHAR_BIT];

do_8bit(data.i8);
// or
do_16bit(data.i16);

delete[] data.i8;

Since you only store POD, it doesn't matter, if you allocate N*2 char s or N short s, and you can use the correct pointer type without casting. 由于您只存储POD,因此如果您分配N * 2个char或N个short ,则无关紧要,您可以使用正确的指针类型而不进行强制转换。

Also: Integer types from <inttypes.h> because there is no garantuee on the absolute sizes of "basic" integer types. 另外: <inttypes.h>整数类型,因为“基本”整数类型的绝对大小没有garantuee。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM