简体   繁体   English

静态类成员和开关

[英]static class members and switches

I got a little problem, I get an error: 我有一个小问题,出现错误:
"C2361: initialization of *identifier* is skipped by 'default' label"

I use an internal class member to set the chosen approach used in my methods. 我使用内部类成员来设置方法中使用的所选方法。
The methods use this internal member ( static int ) with a switch to determine which approach was set. 方法使用此内部成员( static int )和开关来确定设置了哪种方法。
The switch needs an intial value when compiling, so I decided to use a static const int . 该开关在编译时需要一个初始值,因此我决定使用一个static const int However, VS is still unhappy, and I can't change the static const int as well. 但是,VS仍然不满意,我也无法更改static const int I am pretty sure this ain't a big deal, but it's quite frustrating. 我敢肯定这没什么大不了的,但是这很令人沮丧。

example: 例:

class test{
    public:
    static int Val;
    static void setVal(int val);
    static int doStuff(void);
};

test::setVal(int val){
    Val=val;
}

test::doStuff(){
    switch(Val){
    case 1:
    // do stuff
    case 2:
    // do other stuff
    default:
    // do default
    }
}

Many thanks for any tips or solutions 非常感谢您提供的任何提示或解决方案

There are numerous problems with he code you have posted. 您发布的代码有很多问题。 But assuming there are no other issues the following code will produce what you are experiencing: 但是,假设没有其他问题,以下代码将产生您所遇到的问题:

test.h: test.h:

class test{
    public:
    static int Val;
    static void setVal(int val);
    static int doStuff(void);
};

test.cpp: TEST.CPP:

#include "test.h"

int test::Val = 0;

void
test::setVal(int val){
    Val=val;
}

int
test::doStuff(){
    switch(Val){
    case 1:
    // dostuff
        int apples = 1; /* problem line */
        break;
    default:
    // do default
        break;
    }
    return 0;
}

int main(int argc, char **argv)
{
    return 0;
}

Will produce the error: 会产生错误:

error C2361: initialization of 'apples' is skipped by 'default' label

The reason for this is because you are trying to initialise a variable within the case statement. 这样做的原因是因为您试图在case语句中初始化变量。 Visual Studio is complaining that there is no guarantee that the variable will be initialised because the case statement may be skipped over. Visual Studio抱怨不能保证变量将被初始化,因为case语句可能会被跳过。

The question linked here gives the reason for this behavior. 此处链接的问题给出了这种现象的原因。

But to put it briefly there is a chance that your code will jump to a case statement after the initialisation of your variable and visual studio does not allow such a case since it may lead to undefined behavior. 但是简单地说,在变量初始化之后,您的代码可能会跳转到case语句,而Visual Studio不允许出现这种情况,因为这可能会导致不确定的行为。

To quote the standard ( ISO C++ '03 - 6.7/3 ): 引用标准(ISO C ++ '03-6.7 / 3):

A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5) 从具有自动存储持续时间的局部变量不在范围内的点跳转到其处于范围内的点的程序是错误的,除非该变量具有POD类型(3.9)并且在没有初始化程序的情况下声明(8.5)

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

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