0

I have the following code

#include <iostream>
#include <string>

class A : public std::string {
public:
  A(int){};
};

int main() {
  A a(5);
  std::cout << (a == A(5)) << std::endl;
  std::cout << (a == 5) << std::endl;
}

The first line works, the number is explicit converted to an "A" and then the comparison operator from std::string is used to compare the object.

My problem is that I want to avoid the explicit conversion, but if I do it like in the second line, the compiler doesn't recognize the possibility to implicit convert the number and than use the std::string comparison.

Is there a way to change the class A in such a way, that the implicit convert/comparison work?

rawrex
  • 4,044
  • 2
  • 8
  • 24
gerum
  • 974
  • 1
  • 8
  • 21
  • 4
    The `std::`types were not designed to be inherit from (unless explicitly noted). – Richard Critten Jun 28 '21 at 11:35
  • Does this answer your question? [Why should one not derive from c++ std string class?](https://stackoverflow.com/questions/6006860/why-should-one-not-derive-from-c-std-string-class) – Richard Critten Jun 28 '21 at 11:35
  • The best thing to do by a country mile here is to have a `std::string` as a member of `A`. Then implement the operators &c. as you need. – Bathsheba Jun 28 '21 at 11:38
  • 1
    Thats's because the `operator==` is defined as a template, so the type deduction occurs (and fails) before the parameter conversion is considered. – Igor R. Jun 28 '21 at 11:38
  • What is the actual and underlying problem you need to solve? Why do you think using inheritance of `std::string` will solve that problem? – Some programmer dude Jun 28 '21 at 11:42
  • @Someprogrammerdude Its a legacy code, so I do not know why they decided to to so, but they did. – gerum Jun 28 '21 at 11:43
  • _"... comparison operator from std::string is used to compare the object...."_ but as `std::string` does not know about `class A` the sliced object is test for equality not the whole object. – Richard Critten Jun 28 '21 at 11:47
  • @Someprogrammerdude But the idea of inheriting from std::string was, that we want to create a class, that have all features of a std::string, but a few additional methods. – gerum Jun 28 '21 at 11:48
  • @gerum Does your real code have member variables added in `A` so it's not just a `std::string` with some extra member functions? – Ted Lyngmo Jun 28 '21 at 11:48
  • @TedLyngmo No, there are no member variables added. – gerum Jun 28 '21 at 11:49
  • 1
    Someone, somewhere did a very bad design choice. I'm sorry you have to live through maintaining such code. – Some programmer dude Jun 28 '21 at 11:49
  • @gerum Ok, then perhaps [this](https://godbolt.org/z/5crehKGhM)? I'm a bit reluctant to putting it up as an answer though :-) – Ted Lyngmo Jun 28 '21 at 12:08

1 Answers1

0

I think that you can solve your problem with a user-defined conversion function.

In your case you could do something like this:

#include <iostream>
#include <string>

class A : public std::string {
public:
    A(int i) : m_i { i } {};
    operator int() const { return m_i; }
    int m_i;
};

int main() {
    A a(5);
    std::cout << (a == A(5)) << std::endl;
    std::cout << (a == 5) << std::endl;
}

Note however that, as pointed out in comments, you should not derive from std::string.

Dundo
  • 714
  • 8
  • 12