注册 登录
编程论坛 C++教室

问个比较2的问题,vector作为class成员

melody_rain 发布于 2012-10-23 10:29, 366 次点击
class xx
{
  private:
    std::vector< unsigned char > _pixels(_WIDTH*_WIDTH*4);
}

为嘛不可以这样用设定_pixels的大小?
谢谢
7 回复
#2
寒风中的细雨2012-10-23 11:56
class xx
{
private:
    int i = 10;
}
见过这样的?
#3
rjsp2012-10-23 12:18
回复 2楼 寒风中的细雨
C++11 中可以 ^_^
#4
melody_rain2012-10-23 14:16
回复 2楼 寒风中的细雨
std::vector< unsigned char > _pixels(_WIDTH*_WIDTH*4);难道不仅仅是申明一个_WIDTH*_WIDTH*4这么大的vector嘛? 看来问题确实略2... : )
#5
rjsp2012-10-23 14:44
回复 4楼 melody_rain
C++03标准中只有“static const integral data members”可以直接声明初值,其它的都必须在构造函数初始化列表中初始化

程序代码:

#include <vector>

class xx
{
public:
    xx() : val_(4), pixels_(4)
    {
    }

private:
    int val_;
    std::vector<unsigned char> pixels_;
public:
    static const int scint = 123;
    static const float scflt;
};
const float xx::scflt = 123.456f;

int main()
{
    int a = xx::scint;
    float b = xx::scflt;

    return 0;
}

#6
rjsp2012-10-23 14:49
如果在 C++11 中,我给两个代替品
程序代码:
class xx
{
private:
    //std::vector<unsigned char> pixels_( 4 );
    std::vector<unsigned char> a_{ 0, 0, 0, 0 };
    std::vector<unsigned char> b_ = std::vector<unsigned char>(4);
};

#7
寒风中的细雨2012-10-23 15:31
回复 4楼 melody_rain
程序代码:
#include <vector>
#include <iostream>
using namespace std;

class test
{
public:
    test(){cout << "test::test()" << endl; }
    test(const test&t){cout << "test::test(const test&t)" << endl; }
};

int main()
{
    vector<test> ivec(50);

    cout << ivec.size() << endl;

    return 0;
}
#8
melody_rain2012-10-23 17:27
我理解了 谢谢两位斑竹~
1