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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:36:19+00:00 2026-06-04T05:36:19+00:00

I have been using Java for a very long time and I have problem

  • 0

I have been using Java for a very long time and I have problem getting used to C++ programming styles.

How we can manage scenarios like below:

  • Instance variables are objects which cannot be created using default constructor. In java constructor parameters can be decided upon in higher level class constructor.

  • Instance variable is a reference type and we need to run a simple algorithm (condition, calculation,…) in the constructor and then create and assign an object to that reference.

There are possibly similar scenarios in which we need to initiate instance variables in places other than the constructor initialization list. I guess GCC would allow to do that (issues a warning), but VC++ does not seem to allow.

I guess most of these can be done using pointers but I am trying to avoid pointers as much as I can (to minimize run-time crash and also hard to debug problems).

  • 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-04T05:36:20+00:00Added an answer on June 4, 2026 at 5:36 am
    • Instance variables are objects which cannot be created using default constructor. In java constructor parameters can be decided upon in higher level class constructor.
    class A {
    public:
      A(int n);
    }
    
    class B {
    public:
      B(int n) : a1(n), a2(n+1) {}
    private:
      A a1, a2;
    }
    
    • Instance variable is a reference type and we need to run a simple algorithm (condition, calculation,…) in the constructor and then create and assign an object to that reference.
    static int n = 1;
    static int m = 2;
    class A {
    public:
      A(bool useN) : ref(useN ? n : m) {}
    private:
      int &ref;
    }
    

    You can hide more complicated computations in (static) helper functions, of course, having ref(f(parameters)) in the initializer list.

    If you need to create an object first and then assign it to the reference, where does that object primarily live? A reference, after all, is just someone pointing at someone else saying “that’s me, over there.” If your outer object is actually the one owning this object, you don’t want a reference. You either want an object or a smart pointer.

    A Java reference is probably closest to C++11’s std::shared_ptr, one of the smart pointers of the standard library highly recommended for everyday use. In this kind of setting, you might also want to consider std::uniqe_ptr, which has a little less overhead, but comes with limitations. Whether the fact that it requires you to create a proper copy constructor is a problem is a matter of taste – pretty often, the default constructor combined with shared_ptr is not the behavior you want, anyway.

    Stay clear of std::auto_ptr, which is only in the language for backwards compatibility – it’s tricky to use correctly in a lot of situations.

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

Sidebar

Related Questions

I have been using Java for a long time, and for quite some time
I have been using Eclipse for a long time, the feature that is less
I have not been using envers for very long, and i have hit a
I am very new to spring and java. I have been using mostly springsource.org
I have been using the new feature of Spring 3.1.1 the java based configuration.
I have been tasked to develop an interactive website using java & mysql: using
I come from a Java background and have been using C# for the last
I have been using Weblogic as my company used it. Now I want to
I have been using the 'Web Site Administration Tool' that you can bring up
hello i have a basic client-server system running using java sockets. my problem is,

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.