【问题标题】:A few questions about declaration semantics [closed]关于声明语义的几个问题[关闭]
【发布时间】:2013-02-25 23:40:22
【问题描述】:

以下哪一个更正确,使用更广泛?实际问题是最后一个问题,我认为行为发生了变化。

int *test; //for this, it is probably both?
int* test;


int& test;
int &test;

实际问题:

const int test;
int const test;


const int* test;
int* const test; //<-- I guess this also have a different meaning if I consider the last one?


const int& test;
int const& test; //also, considering last one, any difference?


const int*& test;
int* const& test; //<-- this is the correct one for stating "I won't modify the passed object, but I may modify the one pointed"? I have had problems with the other one sometimes, does the other one has some other meaning?
const int* const& test; //<-- another meaning?

如果你知道这个主题中的任何视觉“歧义”,我会很高兴。

【问题讨论】:

  • 如果您愿意阅读,里面一个有效的问题。如果您不知道答案,请停止提议关闭。或者如果你不在乎,不在乎。
  • @JesseGood 是的,我一直在阅读。没有与强硬的参考组合。我认为这一切都是由指针引起的。不过我会等待答案。
  • 引用问题与指针问题相同,请记住 C++ 非常重视类型,即int&amp; i

标签: c++ semantics


【解决方案1】:

除了这些之外,您的所有示例都为每一行提供了相同的语义:

//test is a pointer to a const int.
//test may be modified, but the int
//that it points to may not (through this
//access path).
const int* test;

//test is a const pointer to int.
//test may not be modified, but the
//int that it points to may.
int* const test;



//test is a reference to a pointer to a const int.
//The referenced pointer may be modified, but
//the int that that pointer points to may not be.
const int*& test;
//test is a reference to a const pointer to 
//int. The referenced pointer may not be modified
//but the int may be.
int* const& test; //<-- this is the correct one for stating
                  //    "I won't modify the passed object,
                  //     but I may modify the one pointed"?
                  //       Yes
                  //    I have had problems with the other one sometimes,
                  //    does the other one has some other meaning?
                  //       Yes
//test is a reference to a const pointer to const int.
//The referenced pointer may not be modified, nor may
//the int that it points to.
const int* const& test; //<-- another meaning?

【讨论】:

    【解决方案2】:

    &amp;* 周围放置空格(或者实际上,如果您在一侧或两侧有一个或多个空格)绝对没有区别。 const 的位置确实有所不同;

    const int *test;
    

    表示test 所指的内容没有改变。所以:

    int b = 42;
    *test = 42;
    test = &b;
    

    *test = 42; 是非法的,但将测试分配给新地址是有效的。

    int * const test;
    

    表示test 不能改变它的值,但它所指向的可以:

    int b = 42;
    *test = 42;
    test = &b;
    

    现在test = &amp;b; 无效。

    const int& test;
    int const& test; //also, considering last one, any difference?
    

    两者都一样。 const 和 int 是&amp; 的同一侧。

    这个:

    const int*& test;
    

    意味着我们有一个int * 的引用,它的值不能改变。完全有效,我们可以使用以下内容:

    test = &b;
    

    这两个:

    int* const& test
    const int* const& test;
    

    分别是对 int *const int * 的引用,我们不能更改指针本身 - 所以通过引用传递它没有意义。

    【讨论】:

      【解决方案3】:

      首先,空格在技术上并不重要,除了它分隔符号的程度。

      也就是说,您不会通过分析现有声明来尝试理解事物。

      你应该从构造声明开始。

      在这些结构中,将 const 放在它适用的任何内容之后。

      不幸的是,C++ 中保留了 C 的一般概念,即声明就像用法一样。因此,假设表达式*p 应该产生int,那么p 的声明将是int *p。现在假设表达式*p 应该产生int const,那么声明将是int const *p

      在 C++ 中,重点是类型,因此 C++ 程序员可能会将其编写为

      int const* p;
      

      将类型事物与声明的名称分开。

      但请记住,空间并不重要技术上

      通过这种从预期用途的外观构造声明的方式,您可以轻松地使用 C++ 编译器测试您的声明是否有效,它是否可以编译。

      【讨论】:

      • 这是否意味着 p 是一个指向 const int 的指针,指向的对象不能被修改?它与“int* const p”不同吧?
      【解决方案4】:

      int *xint &amp;x 被认为更好。为什么? int* x, y 看起来像是两个指向 int 的指针,但这是一个指针和一个 int。 int *x, *y 语法稍微好一点 - 你可以更好地看到这是两个指针。

      我知道我没有回答你问题的第二部分;)

      【讨论】:

      • 在 C 中,是的,当然。在 C++ 中,您首选的约定不能普遍适用,因此会提供各种看起来不同的声明。因此,在 C++ 中,一次只声明一件事。
      • 自 C 以来有变化吗?
      猜你喜欢
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 2011-08-24
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多