小编Tod*_*hev的帖子

[C++]在构造函数中为数组指定一个大小

我老老实实地坚持如何在构造函数的调用期间分配数组的大小.另外,我希望数组是'const'.在构造函数中这可能吗?或者我必须做一些更棘手的事情?以下是代码的一部分:

class CustomBitmap
{

public:
    CustomBitmap(int width,int height);
    ~CustomBitmap(void);
private:
    const int m_width;
    const int m_height;
    char const m_components[]; 

};
Run Code Online (Sandbox Code Playgroud)

////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////////

#include "CustomBitmap.h"


CustomBitmap::CustomBitmap(int width,int height) : m_width(width),  m_height(height)
// How do I implement the array? none of the syntax works, I tried m_components([width * height *4]) and all sorts of things along that line.
{}  
CustomBitmap::~CustomBitmap(void) {}
Run Code Online (Sandbox Code Playgroud)

c++ arrays constructor

1
推荐指数
1
解决办法
61
查看次数

标签 统计

arrays ×1

c++ ×1

constructor ×1