c ++声明顺序(const/type/function) [英] c++ declaration order (const/type/function)

查看:74
本文介绍了c ++声明顺序(const/type/function)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在可以进行c ++声明的固定顺序"?我正在查看类声明中的声明.

I was wondering if there is a "fixed order" in which c++ declarations can be made? I'm looking at declarations within a class declaration.

在pascal中,我将按照using-const-type-function序列的思路进行思考:

In pascal I would be thinking along the lines of using-const-type-function sequence:

  1. 使用 objpas;//整数
  2. const c_limit = 5;
  3. 类型 t_int_array =整数数组[0..c_limit];
  4. 功能 my_func(a_arg0:t_int_array):整数;
  1. using objpas; //integer
  2. const c_limit = 5 ;
  3. type t_int_array = array [0..c_limit] of integer ;
  4. function my_func( a_arg0 : t_int_array ) : integer;

我的类声明与某些用户相反,因为我使用了private-protected-public顺序.但是在这些部分中,声明的顺序也可以固定吗?

My class declarations are contrary to some users because I use private-protected-public order. But within these sections can the order of declarations be fixed also?

我正在考虑的事情是:

type declarations (types, enum, class etc)
friend class
friend functions
static member value
static functions
virtual functions
member function
member value 
operators
bitfields
constructors
destructors
using clause
(..plus any I may have missed?)

此操作的背景是,我正在使用一个GUI,使孩子们可以将项目(例如,虚拟功能)拖放到列表(例如,虚拟功能列表)中-因此,这些列表的顺序很重要对我来说.

The background to this is that I'm working on a GUI that allows kids to drag and drop items (e.g. virtual function) into lists (e.g. the virtual-function list) - so its the order of these lists that is important to me.

根据c ++,目前没有固定顺序.但是,如果可以证明固定订单有效,那么它将帮助引导"用户查看可用的列表",并以最小/最大查看的方式将商品放入其中.

At the moment there is no fixed order, as per c++. But if a fixed order can be proven to work, then it would help "steer" users into seeing what kind of "lists" are available and drop items into them with min/max viewing.

我最不确定的一个方面是函数和成员值的声明顺序,因为我不确定成员是否可以指向类函数.我在Pascal方面比C ++强大,请问对此有何想法?

One area that I'm most unsure about is the order in which functions and member values are declared, because I'm not sure if a member can point to a class function..that kind of thing. I'm stronger in pascal than c++ and would appreciate any thoughts on this please?

此外,我知道有些用户对商品的显示顺序有偏好,例如因为他们更喜欢先查看最多的公共项目,但是在我的情况下,这并不是一个因素,因为GUI以使我只能专注于声明的必需顺序"的方式最小化了部分.

Also, I know some users have a preference for the order in which items are displayed, e.g. because they prefer to view the most public items first, but in my case this isn't a factor because the GUI minimizes sections in such a way that I'm able to focus only on the "required order" of the declarations.

推荐答案

这可能是解决方案吗?

我已经考虑了以任何顺序声明常量和类型的需要,并且必须先完成它们.

I have taken into consideration the need to declare constants and types in any order, and that they have to be done prior to anything else.

此外,代码体不能放置在类的函数声明中.

Also, code body cannot be placed inside a function declaration in class.

在那之后,下面概述的部分是否可以按照我给定的顺序进行固定,或者实际上以任何其他/更好的顺序进行固定?

After that, can the sections outlined below be fixed in the order I have given, or indeed in any other/better order?

class t_maybe_this_will_work
{
    //1.
    // This MUST be first, and allow declarations in ANY order
    // constexpr,typedefs,enums,nested classes

    //2.
    // What is best/only order for these..?
    // I have listed them so that functions come before values in case
    // functions are referenced?

    // friend                  classes
    // friend                  functions
    // using                   clauses

    // static                  functions
    // virtual                 functions
    // virtual                 operator
    // non-static,non-virtual  function
    // non-static,non-virtual  operator

    // static                  value
    // non-static              value
    // bitfield                value

    // virtual                 destructors
    // non-virtual             destructors
    //                         constructors

    /*     ***** RULE *****

    The body of functions are NOT defined within the class.

    int not_allowed_body_here (void)
    {
       // defining some code here...
    } ;

    */

} ;

这篇关于c ++声明顺序(const/type/function)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆