代码之家  ›  专栏  ›  技术社区  ›  missingfaktor Kevin Wright

以下两个语句在语义上是否相同?

  •  1
  • missingfaktor Kevin Wright  · 技术社区  · 15 年前

    以下两个语句在语义上是否相同?

    α1 person p("Rahul", 20);

    α2 person const &p = person("Rahul", 20);

    编辑:

    对不起,我想问一下以下两个词在语义上是否相同:

    #1个 person const p("Rahul", 20);

    α2 person const&p=人(“rahul”,20);

    2 回复  |  直到 15 年前
        1
  •  2
  •   Johannes Schaub - litb    15 年前

    它们不是。然而,差异只受第二种情况需要在C++ 03中可访问复制构造函数的事实影响(即使复制构造函数调用实际上未完成)。

    // works with #1 fails with #2
    struct f1 { f1(string, int); private: f1(f1 const&); };
    
        2
  •  5
  •   anon    15 年前

    不,他们不是。在每种情况下,p的行为方式是不同的。例如,在后一种情况下,您不能说:

    p.rename( "fred" );
    

    假设此人有一个rename()方法。

    当然,如果您的第一个实例是:

    const person p("Rahul", 20);
    

    这两者本来会更相似。我希望您不打算对所有“变量”使用引用“:-”