1

What is the proper thing for me to return here?

char BCheckString::operator[](int index)
{
    if (index < 0 || this->length() <= index)
    {
        throw IndexOutOfBounds();
        ???Do I need to return something here???
    }
    else
    {
        return ?????;
    }
}

I tried return this[index] but VS2013 says: "no suitable conversion function from "BCheckString" to "char" exists. And I have no idea what to return after the throw.

I have:

class BCheckString : public string
{
private:
    bool checkBounds();
public:
    BCheckString(string initial_string);
    char operator[](int index);
    class IndexOutOfBounds{};
};

and

BCheckString::BCheckString(string initial_string) : string(initial_string)
{
}

char BCheckString::operator[](int index)
{
    if (index < 0 || this->length() <= index)
    {
        //throw IndexOutOfBounds();
        cout << "index out of bounds" << endl;
        return 'A';
    }
    else
    {
        return 'A';
    }
}

Obviously this is homework ;)

abalter
  • 9,663
  • 17
  • 90
  • 145
  • 3
    Why are you publicly deriving from `std::string`? http://stackoverflow.com/questions/6006860/why-should-one-not-derive-from-c-std-string-class – PaulMcKenzie Feb 27 '15 at 22:29
  • 1
    @PaulMcKenzie god save us. Theachers tell their students to derive from classes which are not designed to be derived. That's why most software is f***ed up. – Giulio Franco Feb 27 '15 at 22:42
  • Anyway, I see the answers down here will help you solve your problem. I suggest that you also take the quote from "Effective C++" that explains why you shouldn't derive from `std::string` and show it to your teacher (after you understood it, that is). – Giulio Franco Feb 27 '15 at 22:45
  • Indeed, I wonder how much bad code would break if std::string was changed to "final". – jbruni Feb 27 '15 at 23:11

4 Answers4

3

While observing that what you're doing here is unnecessary, the syntax is thus:

return string::operator[](index);

You're calling the operator[] of your string parent. This should be preferred to using c_str because string::operator[] does bounds checking in debug builds.

It's also worth noting that .at already does bounds checking in release builds, and throws std::out_of_range.

For the first question, no. After throwing an exception, you don't need to have a return statement. In fact, if you do, the compiler may warn you about "unreachable code".

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • 1
    In fact, the proper implementation (in as much such nonsense can ever be properly implemented) would simply by `{ return string::at(index); }` – Walter Feb 27 '15 at 22:47
3

First, deriving from std::string is not recommended: Why should one not derive from c++ std string class?

As to your questions:

1) After throw you don't return anything.

2) Your attempt to use operator[] is incorrect as you are not calling the parent class's std::string::operator[].

To call the correct operator[]:

 else
 {
    return std::string::operator[](index);
 }
Community
  • 1
  • 1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
2

this is a pointer, therefore this[index] will erroneously consider this as pointing to an array of instances to access the index-th of them. That will be an instance of the class itself and there's no implicit conversion from that to the declared return type char (this is what the error message is complaining about).

You need to get the char from the base string, and this is done with

return this->string::operator[](index);
6502
  • 112,025
  • 15
  • 165
  • 265
1

If you derived from std::string you can just use c_str() method to access data.

return this->c_str()[index];