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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:23:45+00:00 2026-05-28T14:23:45+00:00

What constructor can you use to instantiate an fstream if you declare it as

  • 0

What constructor can you use to instantiate an fstream if you declare it as a member of a class?

#include <fstream>
class Foo {
Foo();
// not allowed
std::fstream myFile("\\temp\\foo.txt", fstream::in | fstream::out | fstream::trunc);

// allowed
std::fstream myFile;
}

// constructor
Foo::Foo() {
// what form of myFile("\\temp\\foo.txt", fstream::in | fstream::out | fstream::trunc)  can I use here?


myFile = ???
}
  • 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-28T14:23:46+00:00Added an answer on May 28, 2026 at 2:23 pm

    In the new version of C++ (C++11), then the above code you have is perfectly fine; initializations are allowed inside the body of a class.

    In C++03 (the previous version of C++), you can initialize the fstream by using the member initializer list like this:

    Foo::Foo() : myFile("file-name", otherArguments) {
        // other initialization
    }
    

    Syntatically, this is done by adding a colon after the constructor name but before the brace, then listing the name of the field you want to initialize (here, myFile), and then in parentheses the arguments you want to use to initialize it. This will cause myFile to be initialized properly.

    Hope this helps!

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

Sidebar

Related Questions

So I can't use initializers in my class constructor because of using arrays, so
In the inherited class I use the base constructor, but I can't use the
Can we make a class copy constructor virtual in C++? How to use?
How come I can't instantiate an object of type Foo with above constructor? I
In SQL Server 2008, you can use the Row Constructor syntax to insert multiple
Can I use default arguments in a constructor like this maybe Soldier(int entyID, int
If a class has a private constructor then it can't be instantiated. So, if
I'm trying to avoid the use of eval . I can dynamically instantiate a
A constructor of a class can be a template function. At the point where
I have a class Foo<T, U> with the following constructor: public Foo() { clazz

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.