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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:28:30+00:00 2026-06-11T13:28:30+00:00

I come from a Java background, but I learned C++ after and have been

  • 0

I come from a Java background, but I learned C++ after and have been programming with it for a few years now (mostly debugging and writing fixes, not designing programs from scratch). However, I ran into an issue today and frankly, I’m a little surprised it took this long to come across it.

Let’s say I have a class named Class1 whose header file contains (among other code):

class Class1 {
    private:
        Class2 object;
}

The Class2 class doesn’t have a default constructor specified. Now, in the Class1 constructor, I’m reading the binary header of a file and using the info I parse from that to initialize Class2, as shown with the pseudo-code below:

Class1::Class1(std::string) {
    // Read some binary info from a file here

    // Parse that binary info

    object2 = Class2(info);

In Java, since it’s not using the RAII paradigm, that would be perfectly legal. However, since C++ uses RAII, object 2 was already initialized with its default constructor by the time I do object2 = Class2(info);. I couldn’t have just called that constructor originally (in the Class1 header file) because I didn’t have the info I needed to create object yet. However, I can’t just make object2 local to the constructor, because I need other functions to be able to see/use it.

Clearly this doesn’t work. What’s the standard approach for this stuff? I actually thought about just changing Class1 to having a Class2 pointer like so:

class Class1 {
    private:
        Class2* objectPointer;
}

and then calling *objectPointer = Class2(info). However, “Class2” in my case is an ifstream and it seems that the operator= function has been deleted and doesn’t work with either approach.

So… how do I do this?

  • 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-11T13:28:31+00:00Added an answer on June 11, 2026 at 1:28 pm

    because your object is not const, that’s all perfectly legal. However, if you want to initialise objects in the initialisation phase, you must provide the information. You can do that such

     Class1::Class1(std::string file_name) : object(InfoFromFile(file_name)) {}
    

    where InfoFromFile() would either be a standalone function (declared in an anonymous namespace within a .cc file) or a static member function of Class1. If more information than file_name is required to generate the information needed for Class2, you can provide it to that function.

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

Sidebar

Related Questions

I come from Java Background and so used to Debugging using Eclipse but have
I come from a strong Java background and in recent years have been also
I come from a Java background and have been using C# for the last
I've been iPhone programming for 6 months and come from a PC/Java/Eclipse background and
I primarily come from a PHP background but I have been asked to develop
I come from a Java background, where try/catch is common practice. After few months
I'm new to PHP but come from a Java background and I'm trying to
I come from Java background, where we have data structures with interfaces that if
I come from a java background, but I'm working on a django-based website. To
I'm new to Kohana but come from a Spring/Java background. In Spring I'm used

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.