简体   繁体   English

C++空值和this指针

[英]C++ null value and this pointer

I'm a newbie in C++, trying to study C++.我是 C++ 的新手,正在尝试学习 C++。 I have a block of code in Java like this:我在 Java 中有一个这样的代码块:

public List<String> getDiagnosticTroubleCode() {
    if (diagnosticTroubleCode == null) {
        diagnosticTroubleCode = new ArrayList<String>();
    }
    return this.diagnosticTroubleCode;
}

How can I compare the disgnosticTroubleCode with null value?如何将disgnosticTroubleCodenull值进行比较? and set it as a new List .并将其设置为新的List I already override the std::list to make it use like List in Java.我已经覆盖了std::list以使其像 Java 中的List一样使用。 And then I want to return the diagnosticTroubleCode field within the object this .然后我想返回对象thisdiagnosticTroubleCode字段。 I hope that you guys can help me with this.我希望你们能帮我解决这个问题。 Trying to study about this pointer and null .试图研究this指针和null

Here is my header in C++ :这是我在 C++ 中的标题:

class RUNTIME_EXPORTS DiagnosticTroubleCode {
            protected:
                List diagnosticTroubleCode;
            public:
                List getDiagnosticTroubleCode();
            };

Your code appears to desire only instantiating diagnosticTroubleCode on first use.您的代码似乎只希望在第一次使用时实例化diagnosticTroubleCode Frankly, I find that somewhat odd, since an instance of an otherwise-empty std::list<std::string> would be rather benign.坦率地说,我觉得这有点奇怪,因为一个否则为空的std::list<std::string>的实例会相当温和。 Regardless, this is one way to do that.无论如何,这是做到这一点的一种方式。


class DiagnosticTroubleCode 
{
protected:
    std::unique_ptr<std::list<std::string>> diagnosticTroubleCode;

public:
    std::list<std::string>& getDiagnosticTroubleCode()
    {
        if (!diagnosticTroubleCode)
            diagnosticTroubleCode = std::make_unique<std::list<std::string>>();
        return *diagnosticTroubleCode;
    }
};

Note that the member getDiagnosticTroubleCode will return a reference to the new (or existing) instantiated list.请注意,成员getDiagnosticTroubleCode将返回对新(或现有)实例化列表的引用。 If you decide to forego latent instantiation (I recommend doing so), the code becomes considerably simpler:如果您决定放弃潜在实例化(我建议这样做),代码会变得相当简单:

class DiagnosticTroubleCode 
{
protected:
    std::list<std::string> diagnosticTroubleCode;

public:
    std::list<std::string>& getDiagnosticTroubleCode()
    {
        return diagnosticTroubleCode;
    }
};

If the latter is possible (and frankly, I cannot see how it isn't), pursue that first.如果后者是可能的(坦率地说,我看不出它是如何不可行的),那么首先追求它。 IN both cases above the member is returned by reference, not value or address.在上述两种情况下,成员都是通过引用返回的,而不是值或地址。 This would most-closely resemble what you're probably familiar with.这将非常类似于您可能熟悉的内容。

Best of luck.祝你好运。

I remember in Java everything are pointers.我记得在 Java 中一切都是指针。 So in Java diagnosticTroubleCode == null is essentially comparing diagnosticTroubleCode with a null pointer.所以在 Java 中, diagnosticTroubleCode == null本质上是将diagnosticTroubleCode与空指针进行比较。 In C++ we do not have null, we have NULL or nullptr .在 C++ 中,我们没有 null,我们有NULLnullptr In C++ an object cannot really be null because it is not.在 C++ 中,对象不能真正为 null,因为它不是。 An object takes up a block of memory when it gets constructed, so it can never be null.对象在构造时会占用一块内存,因此它永远不会为空。 So try to familiarize yourself with pointers and use that in your advantage.因此,请尝试熟悉指针并将其用于您的优势。

On the matter of this .关于此事this If you want to return a member variable, you don't really need to write return this.variable , you can simply return the variable by writing return variable .如果你想返回一个成员变量,你真的不需要写return this.variable ,你可以简单地写return variablereturn variable

The difference here is that in Java all complex structure declarations are really declarations of references to such structures.这里的区别在于,在 Java 中所有复杂的结构声明实际上都是对此类结构的引用的声明。 In C++, that's the equivalent of saying List& foo or List* foo .在 C++ 中,这相当于说List& fooList* foo C/C++ declarations are true instances, which is one of the reasons why there can be a bit of complexity with memory management, but it comes with the benefit of information integrity: For example, when you pass a Java List<String> as an argument of a method and change the contents of that argument, it's changed in the calling scope. C/C++ 声明是真实的实例,这是内存管理可能有点复杂的原因之一,但它带来了信息完整性的好处:例如,当您将 Java List<String>作为方法的参数并更改该参数的内容,它在调用范围内更改。 C/C++, on the other hand, will copy the argument regardless of object complexity, and when the function/method returns, the one passed as an argument is otherwise preserved.另一方面,C/C++ 将复制参数而不考虑对象的复杂性,并且当函数/方法返回时,作为参数传递的参数将被保留。

Your C++ class example should look something like this:您的 C++ 类示例应如下所示:

#include <list>
#include <string>
using namespace std;

class RUNTIME_EXPORTS DiagnosticTroubleCode {
  protected:
    list<string>* diagnosticTroubleCode{nullptr};
  public:
    list<string>* getDiagnosticTroubleCode();
};

list<string>* DiagnosticTroubleCode::getDiagnosticTroubleCode(){
  if (diagnosticTroubleCode == nullptr) {
        diagnosticTroubleCode = new List<string>();
    }
    return diagnosticTroubleCode;
}

There are better implementations that don't require returning a pointer or reference, but they impose additional design requirements (such as what the adding a trouble-code mechanism should be).有更好的实现不需要返回指针或引用,但它们强加了额外的设计要求(例如添加故障代码机制应该是什么)。

You could use something like this你可以使用这样的东西

#include <iostream>
using namespace std;

Function

class DiagnosticTroubleCode 
{
protected:
    std::unique_ptr<std::list<std::string>> diagnosticTroubleCode;

public:
    std::list<std::string>& getDiagnosticTroubleCode()
    {
        if (!diagnosticTroubleCode)
            diagnosticTroubleCode = std::make_unique<std::list<std::string>>();
        return *diagnosticTroubleCode;
    }
};

My .cpp file :我的 .cpp 文件:

shared_ptr<List> DiagnosticTroubleCodes::getDiagnosticTroubleCodes()
            {
                if (this->diagnosticTroubleCodes == nullptr) {
                    this->diagnosticTroubleCodes = make_shared<List>();
                }
                return diagnosticTroubleCodes;
            }

And My header file:还有我的头文件:

class RUNTIME_EXPORTS DiagnosticTroubleCodes {
            protected:
                shared_ptr<List> diagnosticTroubleCodes;
            public:
                shared_ptr<List> getDiagnosticTroubleCodes();
            };

I come up with this solution.我想出了这个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM