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

  • Home
  • SEARCH
  • 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 6330985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:58:22+00:00 2026-05-24T17:58:22+00:00

I would like the following code to be equivalent: f = abc; h.data =

  • 0

I would like the following code to be equivalent:

f = "abc";
h.data = f;

EDIT: I’d also like the ability to do the following:

f += "def"; // f.data == "abcdef";
std::string s = f; // s = "abcdef";

std::cout << f << std::endl;
std::cin >> f;

std::vector<std::string> v (f);
v.push_back(h);

// This would be overkill.
printf("%s", (f + std::string("...\n")).c_str());

Would I need to “inherit” std::string or something? (I’m new to this stuff, so could you show me how?)


Here’s my class:

class Foo
{
public:
    std::string data;
} f, h;
  • 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. Editorial Team
    Editorial Team
    2026-05-24T17:58:25+00:00Added an answer on May 24, 2026 at 5:58 pm

    EDIT: I’d also like the ability to do the following: […]

    [ This would have been better served as a new question but I don’t think you could have foreseen that. ]

    No, you don’t need to inherit from std::string. One possible way to do what you want is to add a conversion operator. (I won’t address how to implement operator+=, it can be looked up elsewhere.)

    class foo {
        std::string data;
    public:
        foo&
        operator=(std::string); // See Kerrek's answer for implementation
    
        operator std::string const&() const
        { return data; }
    };
    

    This will do what you want. But I strongly advise you not to use that. Surprising implicit conversions are frowned upon; I recommend reading Herb Sutter to learn why.

    Alternatively you can make the conversion operator explicit (as in, declaring it explicit operator std::string const&() const;) to suppress implicit conversions. But that’s quite less convenient and readable than adding a member function with an appropriate name:

    class foo {
        // as before
    
        operator std::string const&() const
        { return as_string(); }
    
        std::string const&
        as_string() const
        { return data; }
    };
    
    foo f;
    
    // Contrast the uses:
    // std::string s0 = f; Not ok; would be an implicit conversion
    std::string s0(f); // Ok; explicit conversion
    std::string s1 = f.as_string(); // Ok; std::string s1(f.as_string()) works too
    
    std::vector<std::string> v;
    // v.push_back(f); Not ok; would use an implicit conversion
    v.push_back(static_cast<std::string const&>(f)); // Ok; inconvenient
    v.push_back(f.as_string()); // Ok; convenient
    

    Whatever you choose, I still recommend implementing appropriate operators for working with streams:

    std::ostream&
    operator<<(std::ostream& os, foo const& f)
    {
        return os << f.as_string();
    }
    
    std::istream&
    operator>>(std::istream& is, foo& f)
    {
        std::string extracted;
        if(is >> extracted) {
            f = std::move(extracted);
        }
        return is;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do something like the following: def add(a, b): #some code
I would like to do the equivalent of the following xaml in code, but
I would like to know the VB.NET equivalent of the following C# code: unsafe
In the following code I would like the div with 'y' to match the
I have the following code, which works as I expect. What I would like
I have the following html code: <h3 id=headerid><span onclick=expandCollapse('headerid')>&uArr;</span>Header title</h3> I would like to
I have the following snippet where I would like to extract code between the
I have code, similar to the following, that I would like to modify: Sub
I would like to do something like the following with spark. <viewdata model=IList[[string]] />
I'm doing the following in Java, which I would like to do the equivalent

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.