Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 125377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:08:57+00:00 2026-05-11T05:08:57+00:00

when doing #include <string> class MyString : public std::string { public: MyString() {} };

  • 0

when doing

#include <string>  class MyString : public std::string  {  public:        MyString() {} }; 

But the usage below:

MyString s = 'Happy day'; MyString s('Happy Day'); MyString s = (MyString)'Happy day'; 

neither of them works.

It seems that there’s something to do with constructors/operators declaration/overridding, but can anyone help point out where may I find these resources?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:08:58+00:00Added an answer on May 11, 2026 at 5:08 am

    You need to define some constructors for the different types that you want to be able to convert into your strings. These constructors can basically just hand the parameters through to the underlying std::string.

    If you don’t manually create them, the compiler creates a default- and a copy-constructor for you:

    MyString() : std::string() { } MyString(const MyString &other) : std::string(other) { } 

    To allow construction from string literals, you need a constructor that takes a const char*:

    MyString(const char* other) : std::string(other) { } 

    A constructor that takes a const std::string& would also be useful to convert std::strings to your string type. If you want to avoid implicit conversions of normal strings, you should make it explicit:

    explicit MyString(const std::string &other) : std::string(other) { } 

    (Edited because my original version was full of errors and I can’t delete the accepted answer)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> class c1 { public: static std::string m1; static unsigned int
So, clearly I'm doing something wrong... #include <iostream> using namespace std; class thing1 {
Given the following code: [example.h] class MyClass { public: MyClass(); std::string name; std::string address;
What am I doing wrong? #include <vector> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> using namespace std;
When I try to compile this: #include <map> #include <string> template <class T> class
I have the following classes: public class Note { public string Text { get;
class Sequence{ public: Sequence(); virtual void buildTables(); protected: string seq; struct tables{ int a;
#ifndef LISTTEST_H #define LISTTEST_H #include <vector> #include <string> template <class T> class ListTest {
What is the difference between doing a #include in a .h file and in
I am using an include file by doing: <!--#include virtual=/includes/leftNav.shtml--> I changed the include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.