据我所知,override
在C++ 11中引入关键字只不过是一个检查,以确保正在实现override
的virtual
函数是基类中的函数.
是吗?
这应该是一个微不足道的问题,但我无法在stackoverflow上明确找到它.
如果用户不提供,将隐式定义以下内容.
但是我已经读过某个地方(我现在似乎无法找到),有些情况下编译器不会隐式实现它们.
这些条件是什么?
class Foo
{
public:
virtual int foo() final = 0;
};
Run Code Online (Sandbox Code Playgroud)
编译好.
这不Foo
只是浪费空间,而是制造事故吗?或者我错过了什么?
可能重复:
如何初始化具有默认值的类的函数参数
#include <string>
void foo1(const std::string& s = std::string());
void foo2(std::string& s = std::string());
void foo3(const std::string s = std::string());
void foo4(std::string s = std::string());
Run Code Online (Sandbox Code Playgroud)
error at foo2(): default argument for ‘std::string& s’ has type ‘std::string {aka std::basic_string<char>}’
我理解编译器的观点,但我不知道这也不适用于foo1()
它.
class Foo
{
public:
const int x;
};
class Bar
{
private:
const int x;
};
Run Code Online (Sandbox Code Playgroud)
输出:
test.cpp:10:13: warning: non-static const member ‘const int Bar::x’ in class without a constructor [-Wuninitialized]
Run Code Online (Sandbox Code Playgroud)
为什么会Bar
产生警告但不产生警告Foo
(显然是因为访问限定符,但逻辑是什么?).
我知道这些问题似乎含糊不清,但我想不出任何其他方式来表达它,但是,是否有可能做到这样的事情:
#include<iostream>
class wsx;
class wsx
{
public:
wsx();
}
wsx::wsx()
{
std::cout<<"WSX";
}
Run Code Online (Sandbox Code Playgroud)
?
实施1:
foo(const Bar x);
Run Code Online (Sandbox Code Playgroud)
实施2:
foo(const Bar & x);
Run Code Online (Sandbox Code Playgroud)
如果在函数内不更改对象,为什么要复制它(实现1).
这将由编译器自动优化吗?
简介:即使对象const
在函数声明中声明,仍然可以通过其他别名编辑对象&
.
如果您是编写库的人并且知道您的功能没有这样做,或者该对象足够大以证明每次操作的解除引用成本,
foo(const Bar & x);
那么就是要走的路.
第2部分:
这将由编译器自动优化吗?
既然我们确定它们并不总是等价的,并且等价的条件是非平凡的,那么编译器通常很难确保它们,所以几乎肯定 没有
可能重复:
C++中的const声明之间的差异
#include <iostream>
class Bar{};
void foo(const Bar x){} //l5
void foo(Bar x){} //l6
void foo(Bar const x){} //l7
////pointer functions
void foo(const Bar* x){} //l11
void foo(Bar* x){} //l12
void foo(Bar* const x){} //l13
Run Code Online (Sandbox Code Playgroud)
编译器输出:(长话短说l5
,l6
,l7
冲突;但只l12
,l13
冲突)
untitled.cpp:6:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:7:6: error: redefinition of ‘void foo(Bar)’
untitled.cpp:5:6: error: ‘void foo(Bar)’ previously defined here
untitled.cpp:13:6: error: redefinition of ‘void foo(Bar*)’
untitled.cpp:12:6: …
Run Code Online (Sandbox Code Playgroud) 给定data
为
data = [ [0, 1], [2,3] ]
Run Code Online (Sandbox Code Playgroud)
我想索引列表列表中列表中的所有第一个元素。即我需要索引0
和2
。
我试过了
print data[:][0]
Run Code Online (Sandbox Code Playgroud)
但它输出完整的第一个列表。即
[0,1]
Run Code Online (Sandbox Code Playgroud)
甚至
print data[0][:]
Run Code Online (Sandbox Code Playgroud)
产生相同的结果。
我的问题具体是如何完成我所提到的。更一般地说,python 如何处理双重/嵌套列表?
for(unsigned int i = 0; i < x.size(); i++)
assert(x[i] > 0);
Run Code Online (Sandbox Code Playgroud)
不调试(NDEBUG
标志)时,结果是空for
循环.是否有一种干净的方法来处理这个问题(不执行空for
循环); 最好没有预处理器指令,因为它首先会破坏目的assert
.
c++ ×9
const ×3
c++11 ×2
optimization ×2
reference ×2
alias ×1
assert ×1
class ×1
declaration ×1
definition ×1
final ×1
implicits ×1
inheritance ×1
list ×1
loops ×1
member ×1
overriding ×1
pointers ×1
pure-virtual ×1
python ×1
python-2.7 ×1
warnings ×1