0
class MyString:public string
{
    public:
            MyString(){ string();}
            MyString(const char* name){
                string(name);
            }
            MyString(const MyString& a){
                    *this = a;
            }
            MyString(const string& a):string(a){}
            MyString operator()(int start,int end){
                MyString ret(substr(start,end));
                return ret;
            }
};

when I write this, it shows that

‘const char* name’ previously declared here
   10 |             MyString(const char* name){
      |                      ~~~~~~~~~~~~^~~~
and  string(name);
      |                       

what should I do?

just like words written above

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    what did you expect this line to do `string(name);` ? – 463035818_is_not_an_ai Feb 06 '23 at 09:04
  • 1
    or this `string();` ? – 463035818_is_not_an_ai Feb 06 '23 at 09:04
  • 1
    1) Do not tag `C` when you are using `C++`. 2) Why are you deriving from `std::string`? – PaulMcKenzie Feb 06 '23 at 09:05
  • btw this is not the right way to provide a new method for `std::string`. Not everything must be a member. And not everything makes sense as overloaded operator. Whats wrong with using `substr` as you can already without `MyString` ? – 463035818_is_not_an_ai Feb 06 '23 at 09:05
  • 1
    And while it's possible, please note that `std::string` was not meant to be inherited. There's no polymorphism in `std::string`. – Some programmer dude Feb 06 '23 at 09:06
  • 2
    `string(name);` declares a `string` called "name". It looks like you could use a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – molbdnilo Feb 06 '23 at 09:07
  • 3
    frankly, the way to fix this code is to not write it at all. You need to tell us what your aim is, what the purpose of this code should be. Only then we can tell you how to do it right. – 463035818_is_not_an_ai Feb 06 '23 at 09:07
  • you are looking the the member initializer list, and somehow mixed up the syntax for it. But fixing the syntax is only the smaller problem in this code – 463035818_is_not_an_ai Feb 06 '23 at 09:08
  • 1
    Clearly what you are intending is this `MyString(const char* name) : string(name) {}` but as explained above, this is rubbish code. If your string inherits from the standard string, it's not really your string is it? – john Feb 06 '23 at 09:10
  • The `std::string` class is well-documented and recognized by every competent C++ programmer. Deriving from `std::string` has little to no benefit, and some would consider it a drawback to derive from `std::string`. – PaulMcKenzie Feb 06 '23 at 09:10

1 Answers1

2

If you really want to write your own string class based in the standard string class then the way to do it is to use composition not inheritance. Based on the code written above, something like this

class MyString
{
public:
    MyString() {}
    MyString(const char* name) : my_string(name) {}
    MyString(const std::string& name) : my_string(name) {}
    MyString operator()(int start, int end) const {
        return my_string.substr(start, end);
    }
private:
    std::string my_string;
};
john
  • 85,011
  • 4
  • 57
  • 81