如何分配C ++字符串空指针参数?

时间:2017-08-02 18:18:18

标签: c++ pointers

我正在自学C ++,正在通过教程书中的练习。在当前的问题中,我应该传入一个空指针,指向一个触发提示分配姓氏的姓氏参数的方法。

我的问题是:

  1. 引发异常。
  2. 如果我指定了避免例外的方法,则不会在方法之外带回。
  3. 根据我当前的搜索结果,似乎需要双指针或引用。但是,我需要为方法使用指针参数(我使用引用参数作为另一个练习),并且文本中尚未引入双指针(因此我觉得这是作弊/缺少课程的重点)。
  4. 如何在不使用引用或双指针的情况下将其保留在方法之外的字符串空指针?

    当前代码:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void getNameByPointer(string* firstName, string* lastName)
    {
        cout << "Please enter your first name: ";
        cin >> *firstName;
    
        // Check for null. 
        // Only request last name IF null, as per the book exercise statement.
        if (!lastName) 
        {
            string tempLastName;
            cout << "Please enter your last name: ";
            cin >> tempLastName;
    
            // Note: All assignment methods below fail in some way if lastName is null. 
            // I cannot find a way to assign to a null pointer that carries out 
            // of the method without using a double pointer or pointer 
            // reference, which are not yet introduced in the book.
    
            // (1) Throws exception 'Read Access violations': std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Myres(...) returned 0x18.
            // *lastName = tempLastName;    
    
            // (2) This assignment is limited to the method scope.
            // lastName = &tempLastName;
    
            // (3) This assignment is limited to the method scope.
            // lastName = new string(tempLastName);     
        }
    }
    
    int main()
    {
        string firstName;
        string lastName;
    
        getNameByPointer(&firstName, &lastName);
        cout << "Your name is " << firstName << " " << lastName << '\n';
    
        cout << "Calling method with NULL...\n";
        string firstName1;
        // Null pointer
        string *lastName1 = nullptr;
    
        getNameByPointer(&firstName1, lastName1);
        if(lastName1)
        {
            cout << "Your name is " << firstName1 << " " << *lastName1 << '\n';
        }
        else
        {
            cout << "Your name is " << firstName1 << '\n';
        }
    }
    

    编辑:为了更好地说明我是否误解了问题,或者更好地传达了我的约束,问题如下:

    练习3

    修改为练习1编写的程序,这样只有当调用者传入姓氏的NULL指针时,才会提示用户输入姓氏,而不是总是提示用户输入姓氏。

    (练习1已经解决但是练习3的基础。)

    练习1

    编写一个功能,提示用户输入他或她的名字和姓氏,作为两个单独的值。此函数应通过传递给函数的其他指针(或引用)参数将两个值都返回给调用者。首先尝试使用指针,然后使用引用。

    (对于练习3,通过检查变量是否为空来对参考函数进行了类似的解决方案,因为在参数参数中无法检查NULL。)

2 个答案:

答案 0 :(得分:2)

编辑: 我相信在练习3中它完全是错误的,它应该是“not-NULL”而不是NULL指针的情况,但它不是一个正确的答案,所以让我们分析给定的情况,看看它是否会导致矛盾。

因此,当我们给函数一个nullptr时,我们必须返回值。由于指针的值为null,因此它不携带哪个对象的值应该起作用的信息。只有变通方法,比如你的第3点,但它并没有改变答案:“我如何分配一个字符串空指针,以便在不使用引用或双指针的情况下将其保留在方法之外?” - 你根本不能,因为只有nullpointer携带它的信息才是null。没有关于调用者范围的内容,因此无法对其进行修改。

OLD: 您的问题出在if语句中:

    // Check for null
    if (!lastName || lastName->empty()) 

尝试

    if (lastName) 

!lastName使它进入如果它等于nullptr。您不必检查它是否为空(除非您不想覆盖当前字符串)。更重要的是,如果它是nullptr你不能调用它的成员函数(虽然在这里它不会被调用,因为它没有被计算,因为!lastName是真的。)

其余的代码工作正常,所以它不是关于空指针传递:)

答案 1 :(得分:1)

我认为这里的问题是对运动1的误解导致运动3出现问题。我从提供的文本中相信运动措辞不佳,因此混乱。这是一种可行的解决方案。

#include <string>
#include <iostream>

void ExerciseOne(std::string *, std::string *, std::string *);

int main(){

    std::string first, second, result;

    ExerciseOne(&first, &second, &result);

    return 0;
}

void ExerciseOne(std::string *first, std::string *last, std::string *result){
    std::string temp;
    std::cout << "What is your first name?\n";
    std::getline(std::cin, temp);
    *first = temp;
    std::cout << "What is your last name?\n";
    std::getline(std::cin, temp);
    *last = temp;
    *result = *first + ' ' + *last;
    return;
}

请注意以下声明:

  

此函数应通过附加功能将两个值都返回给调用者   传递给函数的指针(或引用)参数。

为什么我们需要额外的参数?我只能假设这意味着应该有更多的输入,而不仅仅是名字和姓氏。没有看到完整的文字,我无法确定。也许重点是表明指针(和引用)可以用作输入和输出。练习3通过简单地检查其中一个指针是否有效来进行。

void ExerciseThree(std::string *first, std::string *last, std::string *result){
    *result = *first + ' ';
    if (last == NULL){
        std::string temp;
        std::cout << "What is your last name?\n";
        std::getline(std::cin, temp);
        *result += temp;
    }
    else {
        *result += *last;
    }
    return;
}

正如您在测试中看到的那样,您无法为指针分配本地string并使其在您的函数之外保持有效。没有更多的输入参数,练习并没有多大意义。当然,您可以使函数只返回string并完全避免第三个参数。