你能用数字开始一个类名吗

你能用数字开始一个类名吗

本文介绍了你能用数字开始一个类名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 中,是否可以使用数字启动类名称?例如,

 模板< class T> class 2DArray {

public:
// 1D ARRAY CLASS
class 1DArray {
public:
1DArray(){Create(); }
1DArray(iterator arr):array1d_(arr){}
explicit 1DArray(size_type cols,const T& t = T()){Create(cols,t); }
1DArray(const 1DArray& arr){Create(arr.begin(),arr.end()); }
1DArray& operator =(const 2DArray&);
〜1DArray(){Uncreate(); }

T& operator [](size_type n){
return array1d_ [n];
}
const T& operator [](size_type n)const {
return array1d_ [n];
}
}


解决方案

中的标识符名称是:


  1. 不能以数字开头

  2. 可以由字母,数字,下划线,通用字符名称和实现定义的字符组成

  3. a 。

在它说:



C ++ 的这也适用于因为这样的显示。






脚注




  • 1。允许的通用字符



允许的通用字符包含在 / code>:


In C++, is it possible to start a class name with a digit? For example,

template <class T> class 2DArray {

public:
    // 1D ARRAY CLASS
    class 1DArray {
    public:
        1DArray() { Create(); }
        1DArray(iterator arr) : array1d_(arr) { }
        explicit 1DArray(size_type cols, const T& t = T()) { Create(cols, t); }
        1DArray(const 1DArray& arr) { Create(arr.begin(), arr.end()); }
        1DArray& operator=(const 2DArray&);
        ~1DArray() { Uncreate(); }

        T& operator[](size_type n) {
            return array1d_[n];
        }
        const T& operator[](size_type n) const {
            return array1d_[n];
        }
}
解决方案

Rules for identifier names in C++ are:

  1. It can not start with a number
  2. Can be composed of letters, numbers, underscore, universal character names and implementation defined characters
  3. Can not be a keyword.

The sections in the C++ draft standard that cover this are 2.11 Identifiers which includes the following grammar:

identifier:
  identifier-nondigit            <- Can only start with a non-digit
  identifier identifier-nondigit <- Next two rules allows for subsequent
  identifier digit               <-  characters to be those outlined in 2 above
identifier-nondigit:
  nondigit                       <- a-z, A-Z and _
  universal-character-name
  other implementation-defined characters
[...]

and 2.12 Keywords explains all the identifier reserved for use as keywords.

Finally, the following names are also reserved:

  1. Names that contain a double underscore __, or start with either an underscore followed by an uppercase letter (like _Apple) in any scope,
  2. Names that start with an underscore in the global namespace (like _apple in the global namespace) are reserved.

The section that covers this in the draft standard is 17.6.4.3.2. We can find a rationale for why these are reserved from Rationale for International Standard—Programming Languages—C which says:

In C++ this also applies to name mangling as this example shows.


Footnotes

  • 1. Allowed universal characters

The universal characters that are allowed is covered in Annex E.1:

这篇关于你能用数字开始一个类名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 12:21