返回语句返回一个空指针值而不是期望的值

Return Statement Returning a Null Pointer Value and Not the Desired Value

本文关键字:返回 期望 一个 语句 空指针      更新时间:2023-10-16

我通过调试运行了这个,在String Substring函数中,直到返回语句,一切都正常工作。

在下面的代码中,

'returnString'在返回行处具有正确的值。但是,只要我转到下一行(后面的右括号),它就变成:

{Text=0x003ed0e0 "îþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþ... }String

,我追溯到析构函数,是删除String时的值。

现在,我认为值只有在传递之后才会被删除,但看起来它是先被删除的。你们知道怎么解决吗?就像我上面说的,这个函数工作得很完美(至少看起来是这样),只是它返回值的方式有问题。

调用我的字符串函数的行:String LocalString((Address.Substring(0, atIndex)));(地址在各自的头文件中被声明为'private'下的字符串)

Substring位于下标操作符的中间位置。如果我似乎遗漏了一个功能或文件,请要求它。谢谢你的阅读和(我希望)帮助。

String.h文件:
#pragma once
#include <iostream>
#include <sstream>
using namespace std;
// C++ String class that encapsulates an ASCII C-string
class String
{
public:
    // Default constructor
    String()
    {
        Text = NULL;
    }
    // MUST HAVE: Copy-constructor that performs deep copy
    String(const String& source)
    {
        Text = NULL;
        // Call the assignment operator to perform deep copy
        *this = source;     
    }
    // Init-constructor to initialize this String with a C-string
    String(const char* text)
    {
        Text = NULL;
        // Call the assignment operator to perform deep copy
        *this = text;
    }   
    // Destructor
    ~String()
    {
        delete[] Text;
    }
    // Returns the count of characters in a C-string text; NULL-terminator is not counted
    static int GetLength(const char* text)
    {
        int x = 0;
        while(text[x] != '')
            x++;
        return x;
    }
    // Assignment operator to perform deep copy
    String& operator = (const char* text)
    {
        // Ddispose of old Text
        delete[] Text;
        // +1 accounts for NULL-terminator
        int trueLength = GetLength(text) + 1;
        // Dynamically allocate characters on heap
        Text = new char[trueLength];
        // Copy all characters from source to Text; +1 accounts for NULL-terminator
        for ( int i = 0; i < trueLength; i++ )
            Text[i] = text[i];
        return *this;
    }
    // if length is not specified, then the substring spans from startPosition until the end of this String
    // throws an exception when startPosition is out of bounds
    String Substring(int startPosition, int length=0) const
    {
        char * str = this->GetText();
        int strLength = length;
        int x = 0;
        char* substring = new char[strLength];
        while(x < strLength && str[x+startPosition]!='')
        {
            substring[x] = str[x + startPosition];
            x++;
        }
        substring[x]='';
        String returnString = substring;

        return returnString;    
    }
    // Returns the count of characters in the String; NULL-terminator is not counted
    int GetLength() const
    {
        return GetLength(Text);
    }
private:
    // The encapsulated C-string
    char* Text;
};
String LocalString((Address.Substring(0, atIndex)));
String DomainString((Address.Substring(atIndex + 1)));
// or, in simpler syntax:
/*
 * String hell0 = String andGoodbye;
 * String smile = String andWave;
 */

尽管注释// Assignment operator to perform deep copy,类没有用户定义的赋值操作符。

计算机生成的默认赋值操作符执行浅拷贝。一旦其中一份副本被销毁,所包含的文本将丢失。