有没有办法返回null而不是自定义对象(不是指针)?

时间:2022-01-04 21:43:58

is there a way to do what I'm trying to do without using pointers or references?

有没有办法在不使用指针或引用的情况下做我想做的事情?

Here's the code:

这是代码:

Node getSmallest() {

    if (openList.empty())
        return 0;         // syntax error

    // some other code
}

4 个解决方案

#1


3  

It seems you want to return the copy of the Node object, when your code is successful.

当您的代码成功时,您似乎想要返回Node对象的副本。

struct Node{
    int a;
    bool isEmpty_;
    Node(bool isEmpty):isEmpty_(isEmpty)
};

Node getSmallest() {

    if (openList.empty())
        return Node(false);

    // some other code
}

There is no other way, you have to return an object, which internally could have a isEmpty flag set to signify the error.

没有别的办法,你必须返回一个对象,在内部可以设置一个isEmpty标志来表示错误。

#2


7  

You can use boost::optional to return an optional value. Or wait for C++14 and use std::optional.

您可以使用boost :: optional返回可选值。或者等待C ++ 14并使用std :: optional。

#3


6  

Yes. You can throw an exception.

是。你可以抛出异常。

#4


2  

The return type of your getSmallest() function is defined as a Node object, which in C++ means that returned expression must be of type Node, and at runtime the returned object's memory will be copied back to the caller.

getSmallest()函数的返回类型定义为Node对象,在C ++中表示返回的表达式必须是Node类型,并且在运行时,返回的对象的内存将被复制回调用者。

Since, you can not return integer 0.

因为,你不能返回整数0。

What you can do, instead, is defining a particular instance object for Node that represents a NULL Node. This basically depends on Node's definition, supposing the following:

相反,您可以为Node定义一个表示NULL节点的特定实例对象。这基本上取决于Node的定义,假设如下:

class Node {
   // Some representative field
   int a;

   // Some basic constructor
   Node(int a){ 
      this->a = a; 
   }
}

You can define a NULL Node in a way similar to:

您可以使用类似于以下的方式定义NULL节点:

class Node {
   // Some representative field
   int a;

   // Some basic constructor
   Node(int a){ 
      this->a = a; 
   }

   static Node NULL_NODE(-1);
}

The above example assumes that you actually never assign field a with the value -1 in other Node objects. If -1 does not fit your purpose, you can choose a value you assume to never use. If you have multiple fields in the node, you can represent the NULL_NODE with a combination of values.

上面的示例假定您实际上从未在其他Node对象中为字段a分配值-1。如果-1不符合您的目的,您可以选择一个您从不使用的值。如果节点中有多个字段,则可以使用值的组合表示NULL_NODE。

EDIT: As innosam points out, you can also (and probably better) add a boolean field to the Node class to represent if the node is NULL or either.

编辑:正如innosam指出的那样,您也可以(并且可能更好)向Node类添加一个布尔字段来表示该节点是否为NULL或者是其中之一。

With the above said, you can now implement your function like this:

有了上面说过,你现在可以像这样实现你的功能:

Node getSmallest() {

   if (openList.empty())
      return Node.NULL_NODE;         // syntax error

   // some other code
}

Otherwise, you can use a third-party tool that allows you do to the same thing. Refer to other people's answers for this case.

否则,您可以使用第三方工具,允许您执行相同的操作。有关此案例,请参阅其他人的答案。

#1


3  

It seems you want to return the copy of the Node object, when your code is successful.

当您的代码成功时,您似乎想要返回Node对象的副本。

struct Node{
    int a;
    bool isEmpty_;
    Node(bool isEmpty):isEmpty_(isEmpty)
};

Node getSmallest() {

    if (openList.empty())
        return Node(false);

    // some other code
}

There is no other way, you have to return an object, which internally could have a isEmpty flag set to signify the error.

没有别的办法,你必须返回一个对象,在内部可以设置一个isEmpty标志来表示错误。

#2


7  

You can use boost::optional to return an optional value. Or wait for C++14 and use std::optional.

您可以使用boost :: optional返回可选值。或者等待C ++ 14并使用std :: optional。

#3


6  

Yes. You can throw an exception.

是。你可以抛出异常。

#4


2  

The return type of your getSmallest() function is defined as a Node object, which in C++ means that returned expression must be of type Node, and at runtime the returned object's memory will be copied back to the caller.

getSmallest()函数的返回类型定义为Node对象,在C ++中表示返回的表达式必须是Node类型,并且在运行时,返回的对象的内存将被复制回调用者。

Since, you can not return integer 0.

因为,你不能返回整数0。

What you can do, instead, is defining a particular instance object for Node that represents a NULL Node. This basically depends on Node's definition, supposing the following:

相反,您可以为Node定义一个表示NULL节点的特定实例对象。这基本上取决于Node的定义,假设如下:

class Node {
   // Some representative field
   int a;

   // Some basic constructor
   Node(int a){ 
      this->a = a; 
   }
}

You can define a NULL Node in a way similar to:

您可以使用类似于以下的方式定义NULL节点:

class Node {
   // Some representative field
   int a;

   // Some basic constructor
   Node(int a){ 
      this->a = a; 
   }

   static Node NULL_NODE(-1);
}

The above example assumes that you actually never assign field a with the value -1 in other Node objects. If -1 does not fit your purpose, you can choose a value you assume to never use. If you have multiple fields in the node, you can represent the NULL_NODE with a combination of values.

上面的示例假定您实际上从未在其他Node对象中为字段a分配值-1。如果-1不符合您的目的,您可以选择一个您从不使用的值。如果节点中有多个字段,则可以使用值的组合表示NULL_NODE。

EDIT: As innosam points out, you can also (and probably better) add a boolean field to the Node class to represent if the node is NULL or either.

编辑:正如innosam指出的那样,您也可以(并且可能更好)向Node类添加一个布尔字段来表示该节点是否为NULL或者是其中之一。

With the above said, you can now implement your function like this:

有了上面说过,你现在可以像这样实现你的功能:

Node getSmallest() {

   if (openList.empty())
      return Node.NULL_NODE;         // syntax error

   // some other code
}

Otherwise, you can use a third-party tool that allows you do to the same thing. Refer to other people's answers for this case.

否则,您可以使用第三方工具,允许您执行相同的操作。有关此案例,请参阅其他人的答案。