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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:15:06+00:00 2026-06-06T23:15:06+00:00

In C++, I have an object A that has a constructor that accepts an

  • 0

In C++, I have an object A that has a constructor that accepts an istream (to load itself from a file). I have another class that has an A as a member. I can’t call A‘s constructor from the initialization list because I didn’t open the istream yet. Once I open it in the constructor of my class, it is too late to call the constructor of A. Is there some way to open an istream in the initialization list into some temporary object so that I can send it to A‘s constructor?
And if there is, is there any type of guarantee on the order the initialization list is called so that the istream would get initialized before the A?

An example may help:

class A {
public:
  A(std::istream const&);
}

class B {
public:
  B(std::istream const&);
}

class MyClass {
  A a;
  B b;
public:
  MyClass() : a(is), b(is) {  // <-- How to do this?
    std::istream is("path");
  }
}
  • 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-06T23:15:08+00:00Added an answer on June 6, 2026 at 11:15 pm

    Here’s yet another idea… If you want to avoid dynamic allocations and use a single, temporary istream, you can move the load phase of A and B away from their constructor:

    class A {
    public:
      Load(std::istream const&);
    }
    
    class B {
    public:
      Load(std::istream const&);
    }
    
    class MyClass {
      A a;
      B b;
    public:
      MyClass() {
        std::istream is("path");
        a.Load(is);
        b.Load(is);
      }
    }
    

    This, BTW, calls for A and B inheriting the load abilities from some shared parent, but that’s obviously beyond the scope of this question.

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

Sidebar

Related Questions

I have an object called Result<T> that has a constructor which accepts an argument
In C++ I have a constructor that accepts an object of class descriptor .
I have an object that has a child collection: class Person { public string
Lets say i have a Shape object that has a constructor like this: Shape(
I have a complex value object class that has 1) a number or read-only
I have an object of class which has private constructor: class CL_GUIComponent { //
I have a case class that has a few parameters for its constructor, and
I have an object that downloads a file from a server, saves it into
I have a constructor that accepts an object of type Material : SomeClass::SomeClass( const
I have a class that has a const member, const pointer and enum class

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.