小编Ani*_*kar的帖子

C++最终是否意味着所有方面都是最终的?

C++ 11添加了最终版.

最后!

我明白final做两件事:

  • 使一个类不可继承.
  • 使(虚拟)函数在类中不可覆盖(在派生类中).

这两者似乎彼此独立.但举个例子如下:

class Foo
{
    public:
    virtual void bar()
    {
        //do something unimportant.
    }
};
class Baz final : public Foo
{
    public:
    void bar() /*final*/ override
    {
        //do something more important than Foo's bar.
    }
};
Run Code Online (Sandbox Code Playgroud)

从上面,我相信Bazfinal的,我应该不是需要指定它的virtual成员函数bar也是final.由于Baz不能继承,覆盖的问题bar超出了范围.不过我的编译VC++ 2015,对此非常安静.目前我还没有对其他任何人进行测试.

如果有人能够对这个话题有所了解,我会很高兴.标准的引用(如果有的话)将非常感激.还请说明我不知道的任何角落案件,这可能导致我的逻辑信念失败.

所以,我的问题是:是否 final class 隐含地暗示其 virtual 功能 final 也是如此?应该是?请澄清.


我之所以这么说是因为final功能符合去虚拟化的要求,这是一个很好的优化.任何帮助表示赞赏.

c++ virtual final c++11

13
推荐指数
2
解决办法
2499
查看次数

为什么不能直接定义匿名struct / class-es的模板别名?

我可以创建以下内容:

using Foo = struct { /*Implementation*/ };

template<class>
using Bar = Foo;
Run Code Online (Sandbox Code Playgroud)

但是,以下内容是不允许的:

template<class>
using Bar = struct { /*Implementation*/ };
Run Code Online (Sandbox Code Playgroud)

来自Clang的错误比GCC更有用,并指出:

错误:无法在类型别名模板中定义“(在file:line:column处的匿名结构)”


为何不允许使用第二个代码示例?

注意:

  • 请说明第二个代码示例(如果允许)可能导致语言问题的所有示例。

  • 该标准的任何引用也有帮助。

c++ templates using-directives language-lawyer c++11

6
推荐指数
1
解决办法
191
查看次数

从void函数返回

假设一个类存在如下:

class Foo
{
    void do_after_something()
    {
    //some code here
    return;
    }

    void do_something()
    {
    //some code here
    return do_after_something(); //returning another (void) function
    }
};
Run Code Online (Sandbox Code Playgroud)

JAVA显然反对上面这样的东西,Borland C++编译器发出警告,MS VC++不抱怨.

我的问题是:从void函数返回逻辑上(理论上)是否正确?

return do_after_something();
Run Code Online (Sandbox Code Playgroud)

而不是:

do_after_something();
return;
Run Code Online (Sandbox Code Playgroud)

或者是所有实现(编译器/语言)依赖?

c++ java language-features return

5
推荐指数
1
解决办法
454
查看次数