以下程序是使用VC++ 2012编译的.
#include <algorithm>
struct A
{
A()
: a()
{}
bool operator <(const A& other) const
{
return a <= other.a;
}
int a;
};
int main()
{
A coll[8];
std::sort(&coll[0], &coll[8]); // Crash!!!
}
Run Code Online (Sandbox Code Playgroud)
如果我return a <= other.a;改为return a < other.a;那么程序按预期运行,没有例外.
为什么?
_currentHandle()在下面是什么意思?
template<class SpiHandleT>
class SpiHandleIterator : public ISpiHandleIterator<SpiHandleT>
{
public:
SpiHandleIterator() : _currentHandle()
{
}
...
protected:
SpiHandleT _currentHandle;
};
Run Code Online (Sandbox Code Playgroud) 对两个类中的一个对象执行 var_dump 会得到相同的结果
Class Node{
public $parent = null;
public $right = null;
public $left = null;
function __construct($data){
$this->data = $data;
}
}
Class Node{
public $parent;
public $right;
public $left;
function __construct($data){
$this->data = $data;
}
}
Run Code Online (Sandbox Code Playgroud)
例如
$a = new Node(2);
var_dump($a);
Run Code Online (Sandbox Code Playgroud)
对于上述任一类返回以下内容
object(Node)#1 (4) {
["parent"]=>
NULL
["right"]=>
NULL
["left"]=>
NULL
["data"]=>
int(2)
}
Run Code Online (Sandbox Code Playgroud)
对于变量来说,情况似乎并非如此。
$b;
var_dump($b);
Run Code Online (Sandbox Code Playgroud)
如果您打算将该属性的值设置为 ,null是否需要明确编写该值,因为 php 似乎会自动为您执行此操作?
另外 - 根据这个答案/sf/answers/422316331/,如果您尝试获取未初始化变量的值,C++ 会给出未定义的行为。如果类中的属性未初始化为值null,C++ 是否会像 php 那样自动设置该属性的值?