DRoc101 DRoc101 - 1 month ago 6
C++ Question

Setting the data member for a class derived from a class derived from std::string to a value

I am trying to figure out this assignment from my level 2 class.
We are to create a class called Number, derived from std::string.
the only code in this class is to be two constructors, a default and one that takes a string as an arguement.
I have:

class Number : public string {
public:
Number();
Number(string set);
}


The constructors are then coded as:

Number::Number() : string("0") { } // default constructor sets data string to "0"
Number::Number(string set) : string(set) { }


So far so good. Then we are to take two classes that we have been developing, Double and Integer, and derive them from Number. Within these two classes we used to have a double and an int respectively as the data member. Now, because of inheritance, we are supposed to have a built in data section (string). The issue that I am having is that all of my operator=() overloads are now "recursive on all paths" leading to a stackoverflow at runtime. I will show an example of the Double constructor that takes a string, and the equals function that causes the infinite recursion.

Double::Double(string d) : Number(d)
{
// overloaded constructor that takes a string argument
if (isNaN(d)) {
*this = "0.0";
}
else {
*this = d;
}

// used for setting the value of a data member with a string
void Double::operator=(string d)
{
if (isNaN(d)) {
*this = "0.0";
}
else {
*this = d;
}
}
}


I see where the recursion is happening as *this is calling the overloaded = which calls itself since I am using *this in the = function itself. So what would the proper syntax be for setting the data member to a supplied value. Before it was just this->dataMemberName = supplied value of proper type.

It should be noted that my non-string constructors do set the value of an instanced object:
Double::Double(int d) : Number(to_string(d)){}

but the second I try to do anything with them, + - * /, the = function is called and then the error happens again.

Answer

You can specify directly that you want to use std::string's operator=:

#include <string>

using namespace std;

class Number : public string
{
public:
    Number();
    Number(string set);
};


Number::Number() : string("0") {}//default constructor sets data string to "0"
Number::Number(string set) : string(set) {}

class Double : public Number {
public:
    //using Number::operator=;
    Double(string d);
    void operator=(string d);
    double val;
};

bool isNaN(string d) { return false; }

Double::Double(string d) : Number(d) {//overloaded constructor that takes a string argument
    if(isNaN(d))
    {
        string::operator=("0.0");
    }
    else
    {
        string::operator=(d);
    }
}

void Double::operator=(string d)//used for setting the value of a data member with a string
{

    if(isNaN(d))
    {
        string::operator=("0.0");
    }
    else
    {
        string::operator=(d);
    }
}

int main()
{
    Double d("3");
    d = "4";

    return 0;
}