相关疑难解决方法(0)

如果 php 自动为您初始化类内的属性,是否需要将其初始化为 null?

对两个类中的一个对象执行 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 那样自动设置该属性的值?

php

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

条件检查:if(x==0) vs. if(!x)

if(x==0)vs.的区别是if(!x)什么?或者它们总是等价的?对于不同的 C++ 内置类型x

  • bool
  • int
  • char
  • pointer
  • iostream
  • ...

c++

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

nullptr不是特殊关键字和std :: nullptr_t的对象吗?

可能重复:
nullptr究竟是什么?

我首先认为这是一个关键字.我目前的gcc没有nullptr以不同的色调突出显示.为了验证这一点,我写了以下内容:

void *&p = nullptr;
Run Code Online (Sandbox Code Playgroud)

所以我从错误中得到了一些线索:

错误:从'std :: nullptr_t'类型的右值开始,无效初始化'void*&'类型的非const引用

如果nullptr是一个对象那么它真的是一个简单的指针0吗?换句话说,假设我写道:

#define NULL nullptr
Run Code Online (Sandbox Code Playgroud)

以上语句是否不会改变我的代码中的任何内容?此外,了解std::nullptr_t类型的其他用例会很有趣.

c++ pointers nullptr c++11

4
推荐指数
2
解决办法
3499
查看次数

4
推荐指数
2
解决办法
126
查看次数

C++:清空if语句

这更像是一个入门级问题,但我想知道是否有一个空if语句是一个好习惯.

考虑以下代码:

void RabbitList::purge()
{
    if(head == NULL)
    {
        //cout << "Can't purge an empty colony!" << endl;
    }
    else
    {
        //Kill half the colony
        for(int amountToKill = (getColonySize()) / 2; amountToKill != 0;)
        {
            RabbitNode * curr = head;
            RabbitNode * trail = NULL;

            bool fiftyFiftyChance = randomGeneration(2);

            //If the random check succeeded but we're still on the head node
            if(fiftyFiftyChance == 1 && curr == head)
            {
                head = curr->next;
                delete curr;
                --size;
                --amountToKill;
            }
            //If the random …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement

3
推荐指数
2
解决办法
1万
查看次数

指针是否可能指向0x000000的地址

这个毫无意义的问题是关于指针的,有人可以指出我正确的方向

变量的地址是否可以合法地分配值0x000000,如果是这样,这是否意味着例如:

#define NULL 0x00000000
int example;
int * pointerToObject = &example;

if(pointerToObject != NULL)
Run Code Online (Sandbox Code Playgroud)

会返回虚假?

c++ pointers

3
推荐指数
1
解决办法
2318
查看次数

警告:将 NULL 传递给 'std::thread::thread 的非指针参数

我想运行的功能:

struct foo;
void bar(const foo* p = 0);
Run Code Online (Sandbox Code Playgroud)

我如何调用函数:

auto thread = std::thread(&bar, NULL);
Run Code Online (Sandbox Code Playgroud)

警告:

foob​​ar.h:223:9: 警告:将 NULL 传递给 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (*)(const foo*), _Args 的非指针参数 2 = {int}]' [-Wconversion-null]

我在这里缺少什么?

当我用非NULL参数调用函数时,警告消失了。

c++ templates nullptr perfect-forwarding c++11

3
推荐指数
1
解决办法
9626
查看次数

在C++中,有没有办法从函数中返回任何函数,该函数的返回类型为string&?

我正在尝试实现以下子例程.

string& sortAndMerge(string &u, int start, int end){
    if(end>=0){

        int mid=(start+end)/2;

        if(start<end){
            sortAndMerge(u,start,mid);
            sortAndMerge(u,mid+1,end);
            return merge(u,start,mid,end);
        }
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

该函数被称为sortAndMerge(s,0,s.length() - 1),这里s是一个字符串.由于第3个参数最终可能是负数,我需要子程序来忽略这些情况.

我收到以下错误: -

return-statement with no value, in function returning ‘std::string& {aka std::basic_string<char>&}’ [-fpermissive].
Run Code Online (Sandbox Code Playgroud)

我有两个问题.
1)有没有办法可以从具有除void之外的返回类型的函数返回任何内容.
2)什么时候可以使用return关键字什么也不用NULL.

c++ function

3
推荐指数
2
解决办法
9849
查看次数

我可以在C++方法中返回NULL指针吗?

在我的代码中,我使用了一种方法getNumberOfP_ForAdd().此方法打开一个文件并返回XMLDocumentWrapper*.

XMLDocumentWrapper* Tab::getNumberOfP_ForAdd()
{
QString defaultName = GuiUtil::getLastPath();
QString fileName = QFileDialog::getOpenFileName(this, "Open " +  displayName + " File",
           defaultName, displayName     + " Files (*." + fileSuffix + ")", 0, 0);

if (fileName.isNull() || fileName.isEmpty()) {
    qDebug() << "Load" << displayName << "aborted.";
    return NULL;
}

GuiUtil::setLastPath(fileName);

// Open file
XMLDocumentWrapper* inputDoc = XMLDocumentWrapper::readFromFile(fileName);
if (inputDoc == NULL) {
    qDebug() << "Load" << displayName << "aborted.";
    return NULL ;
}

return inputDoc;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试读取文件时,我首先检查了两件事:Wheather

(fileName.isNull() || fileName.isEmpty())
Run Code Online (Sandbox Code Playgroud)

和 …

c++ null qt return

2
推荐指数
3
解决办法
4120
查看次数

为什么 std::cout 只有在函数返回时才能打印 nullptr 的值?

根据这个问题Unable to print the value of nullptr on screen

我无法打印 nullptr 的值,因为它的类型为 nullptr_t,并且 std::cout 没有此类重载。

但是,如果你看看这个:

int* f()
{
    return nullptr;
}

int main()
{
    std::cout << f();
}
Run Code Online (Sandbox Code Playgroud)

输出是:

00000000

在这个问题Why does std::cout output gone完全消失后 NULL is sent to it他们讨论了一个不同的问题。

我只是想理解为什么std::cout 不能打印 nullptr,但当 nullptr由函数返回时它实际上可以。

c++ cout nullptr

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

XOpenDisplay(0) 和 XOpenDisplay(NULL) 有什么区别?

Display XOpenDisplay(0) 和 XOpenDisplay(NULL) 有什么区别?

#include <X11/Xlib.h>

struct MwmHints
{
    unsigned long flags;
    unsigned long functions;
    unsigned long decorations;
    long input_mode;
    unsigned long status;
};
enum
{
    MWM_HINTS_FUNCTIONS = (1L << 0),
    MWM_HINTS_DECORATIONS =  (1L << 1),

    MWM_FUNC_ALL = (1L << 0),
    MWM_FUNC_RESIZE = (1L << 1),
    MWM_FUNC_MOVE = (1L << 2),
    MWM_FUNC_MINIMIZE = (1L << 3),
    MWM_FUNC_MAXIMIZE = (1L << 4),
    MWM_FUNC_CLOSE = (1L << 5)
};

extern "C"
{
    void borderless(Window window)
    {
        Display *display = XOpenDisplay(0);
        Atom …
Run Code Online (Sandbox Code Playgroud)

c++ linux x11 xlib

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

我能保证nullptr的地址始终为0吗?

我知道NULL总是如此0,但是为什么下面的代码会打印该消息?

#include <iostream>

using namespace std;

int main() {
    int* ptr = nullptr;
    if (ptr == 0) {
        cout << "Does it always work?";
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ nullptr c++11

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

什么是'坏Ptr'在Visual C++中的意思?

对于C++,当我们检查指针是否有效时,通常我们会执行以下操作:

if (p == NULL){
     // error handling
}
Run Code Online (Sandbox Code Playgroud)

但是,在VC++中,即使p不是NULL(又名0),它也是0x00000004,也就是Bad Ptr.然后导致异常,我们知道这个地址是受保护的,不能被覆盖.

我在这里搜索过类似的问题,但我没有得到答案.

我的问题是:

  1. 当Bad Ptr发生时,为什么不将此指针设置为0?
  2. 由于Bad Ptr的值不为零,如何在VC++中检查指针是否有效?

c++ pointers window visual-c++

0
推荐指数
1
解决办法
9307
查看次数