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 7816749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:58:54+00:00 2026-06-02T05:58:54+00:00

How do I go about using the constructor for a member which is a

  • 0

How do I go about using the constructor for a member which is a string? Here is an example (which is wrong I realize)

class Filestring {
public:
    string      sFile;

    Filestring(const string &path)
    {
        ifstream filestream(path.c_str());
        // How can I use the constructor for the member sFile??
        // I know this is wrong, but this illustrates what I want to do.
        string sFile((istreambuf_iterator<char>(filestream)), istreambuf_iterator<char>());
    }
};

So basically I want to be able to use the member sFile’s constructor without doing a string copy. Is there a way to achieve this through assignment?

  • 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-06-02T05:58:57+00:00Added an answer on June 2, 2026 at 5:58 am

    The best you can use is string::assign:

    sFile.assign(istreambuf_iterator<char>(filestream), istreambuf_iterator<char>());
    

    But in C++11 there is a move assignment operator for string so doing the following is almost as efficient (there is no copy of character data, the character data are moved):

    sFile = string(istreambuf_iterator<char>(filestream), istreambuf_iterator<char>());
    

    Yet another trick when C++11 move assignment is not avaliable is to use std::swap or string::swap. The efficiency would be probably almost identical to the move assignment variant.

    string newContent(istreambuf_iterator<char>(filestream), istreambuf_iterator<char>());
    std::swap(sFile, newContent); // or sFile.swap(newContent);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In N3257 I found an example using initializing members without a constructor , which
I have a question about performance of move constructor, pseudo C++ class: typedef tuple<std::string,
I have the following code: public static class ScraperMasterUriDetails { public static Dictionary<Guid, string>
I have questions about a couple techniques I'm using in designing a class. I've
I'm thinking about using the java application object to implement a simple cache, to
I read an article about using CLR integration in sqlserver and was wondering what
How would I go about using Stephen Celis' fantastic SCListener singleton? ( http://stephencelis.com/2009/03/02/now-i-just-need-an-audience.html )
The Apple guidelines talk about using a source list - how do I create
I've been asked about using the Qt database interface for Oracle. What's a good
I read about using <context:component-scan base-package=tld.mydomain.business> <context:include-filter type=annotation expression=org.springframework.stereotype.Service/> </context:component-scan> and annotate my service

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.