小编Kev*_*ang的帖子

枚举类型值作为C++中数组的长度

众所周知,必须确定C++中的数组长度.然后我们可以使用:

const int MAX_Length=100;
Run Code Online (Sandbox Code Playgroud)

要么:

#define MAX_LENGTH 100
Run Code Online (Sandbox Code Playgroud)

在编译之前确定数组的长度.但是,当我读到lippman的书c ++入门时,在第5版的第3.5.1节中,它说:数组的长度必须是一个常量表达式.然后问题来了:

typedef enum Length{LEN1=100, LEN2, LEN3, LEN4}LEN; 
LEN MAX_Length=LEN2;  //101
int iArray[LEN2];     //attention
Run Code Online (Sandbox Code Playgroud)

代码在mingw32-g ++中成功编译.但在VS2008中失败了,错误是:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'iArray' : unknown size
Run Code Online (Sandbox Code Playgroud)

我认为枚举值是常量,因此它应该用作数组的长度.对?

我很困惑,你能帮助我吗?谢谢.

c++ arrays enums visual-studio constant-expression

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

在C ++中隐藏,重载或覆盖

然后我又有一个问题。像这样的东西:

#include <iostream>
using namespace std;

class Base
{
public:
    void foo()
    {
        cout<<"Base."<<endl;
    }
};

class Derive:public Base
{
public:
    void foo()
    {
        cout<<"Derive."<<endl;
    }
};

int main()
{
    Derive d;
    Base *pb=&d;    //attention here
    pb->foo();      //ateention here
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出为“ Base”。然后功能规则不起作用,对此我感到困惑,您能帮我吗?谢谢。

c++ inheritance member-hiding

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

矢量&lt;int &amp;&gt; 未编译

问题来自下面的代码:

vector<int &> one;  //compile failed
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么代码无法编译。

c++ reference vector

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