为什么我不能使用带开关的非整数类型[重复]

时间:2021-08-16 06:53:28

This question already has an answer here:

这个问题在这里已有答案:

Cause if I define the operator== then comparison will be possible:

因为如果我定义运算符==那么可以进行比较:

class My
{
    int x;
    // ...
public:
    My(int);

    bool operator==(const My & y);
        // ...
};

//...

My one = 1;
switch (one)
{
    case 1 : // anything

    default : // ...
}

But it's possible only for integer types. Why?

但它只适用于整数类型。为什么?

3 个解决方案

#1


3  

The BCPL and C languages implemented switch (switchon in BCPL) as a higher-level implementation of an assembly branch table.

BCPL和C语言实现了切换(BCPL中的switchon)作为程序集分支表的更高级实现。

A branch table is a very efficient implementation of an if/else chain that uses a single integer to index into an array of addresses (or address offsets). Program control jumps to the address at the specified index of the table.

分支表是if / else链的非常有效的实现,它使用单个整数索引到地址数组(或地址偏移量)。程序控制跳转到表的指定索引处的地址。

switch requires an integer type (or a type implicitly convertible to an integer) because array-indexing requires an integer type.

switch需要整数类型(或可隐式转换为整数的类型),因为数组索引需要整数类型。

C++ inherited the same language properties of switch without making significant changes.

C ++继承了switch的相同语言属性而没有进行重大更改。

It would be possible to redefine the language to implement switch using operator ==, but that same behavior can already be implemented as an if/else chain.

可以使用operator ==重新定义语言以实现切换,但是同样的行为已经可以实现为if / else链。

#2


2  

You can use classes in the switch statement.

您可以在switch语句中使用类。

According to the C++ Standard (6.4.2 The switch statement):

根据C ++标准(6.4.2开关语句):

2 The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (Clause 4) to an integral or enumeration type.

2条件应为整数类型,枚举类型或类类型。如果是类类型,则将条件在上下文中隐式转换(第4节)为整数或枚举类型。

Here is a demonstrative program

这是一个示范计划

#include <iostream>

class A
{
    int x;

public:
    A( int x ) : x( x ) {}

    operator int() const { return x; }
};

int main()
{
    A a( 2 );

    switch ( a )
    {
        case 1:
            std::cout << "past by" << std::endl;
            break;
        case 2:
            std::cout << "Bingo!" << std::endl;
            break;
    }            
}

The program output is

程序输出是

Bingo!

#3


1  

You can add an implicit conversion operator to your class to make this possible:

您可以向类中添加隐式转换运算符以实现此目的:

operator int() const { return x; }

#1


3  

The BCPL and C languages implemented switch (switchon in BCPL) as a higher-level implementation of an assembly branch table.

BCPL和C语言实现了切换(BCPL中的switchon)作为程序集分支表的更高级实现。

A branch table is a very efficient implementation of an if/else chain that uses a single integer to index into an array of addresses (or address offsets). Program control jumps to the address at the specified index of the table.

分支表是if / else链的非常有效的实现,它使用单个整数索引到地址数组(或地址偏移量)。程序控制跳转到表的指定索引处的地址。

switch requires an integer type (or a type implicitly convertible to an integer) because array-indexing requires an integer type.

switch需要整数类型(或可隐式转换为整数的类型),因为数组索引需要整数类型。

C++ inherited the same language properties of switch without making significant changes.

C ++继承了switch的相同语言属性而没有进行重大更改。

It would be possible to redefine the language to implement switch using operator ==, but that same behavior can already be implemented as an if/else chain.

可以使用operator ==重新定义语言以实现切换,但是同样的行为已经可以实现为if / else链。

#2


2  

You can use classes in the switch statement.

您可以在switch语句中使用类。

According to the C++ Standard (6.4.2 The switch statement):

根据C ++标准(6.4.2开关语句):

2 The condition shall be of integral type, enumeration type, or class type. If of class type, the condition is contextually implicitly converted (Clause 4) to an integral or enumeration type.

2条件应为整数类型,枚举类型或类类型。如果是类类型,则将条件在上下文中隐式转换(第4节)为整数或枚举类型。

Here is a demonstrative program

这是一个示范计划

#include <iostream>

class A
{
    int x;

public:
    A( int x ) : x( x ) {}

    operator int() const { return x; }
};

int main()
{
    A a( 2 );

    switch ( a )
    {
        case 1:
            std::cout << "past by" << std::endl;
            break;
        case 2:
            std::cout << "Bingo!" << std::endl;
            break;
    }            
}

The program output is

程序输出是

Bingo!

#3


1  

You can add an implicit conversion operator to your class to make this possible:

您可以向类中添加隐式转换运算符以实现此目的:

operator int() const { return x; }