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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:32:41+00:00 2026-05-20T17:32:41+00:00

I come from a Java background and recently decided to try creating a C++

  • 0

I come from a Java background and recently decided to try creating a C++ Qt GUI application “for fun”. I’ve been struggling with many of the finer differences between Java & c++, but I’ve learned a lot.

I’m attempting to match “java-style” syntax as closely as possible in my C++ coding. While this may or may not be a “best practice” for C++, I feel it helps when learning to keep a familar & consistent syntax. One of the java syntax carryovers is this:

//Java
MyObject o = new MyObject();

//C++
MyObject o = MyObject();

Now, I understand C++ has a syntax shortcut for the above:

//C++
MyObject o();

This is nice and all, but as I said, I don’t want to use different syntax, yet. Everything was working fine, but I received a surprise when I attempted the following Qt code:

QString filepath = "C:\\somefile";
QFile file = QFile(filepath);

And got a compile error:

c:\QtSDK\Desktop\Qt\4.7.2\mingw\include/QtCore/qfile.h:195: error: 'QFile::QFile(const QFile&)' is private within this context

I read the Qt docs and discovered that there is indeed no public constructor QFile::QFile(const QFile&). Where my code previously worked for other classes, there was such a constructor. I can make a guess here and say that the line:

QFile file = QFile(filepath);

is actually making calls to two constructors. Can someone explain?

  • 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-20T17:32:41+00:00Added an answer on May 20, 2026 at 5:32 pm
    //Java
    MyObject o = new MyObject();
    
    //C++
    MyObject o = MyObject();
    

    These are not equivalent. The c++ version creates a new object, on stack, something java can only do with primitive types. Once o goes out of scope it will be destructed. What actually happens above is logically MyObject o(MyObject()); (incorrect actual syntax) – construction followed by copy-construction.

    MyObject * o = new MyObject();
    

    This is as close as you gets to your java version. The caveat, of course, is that in C++ you’ll need to also delete o; – it’s not going to get garbage collected.

    QFile file = QFile(filepath);
    

    This creates a temporary QFile, again on stack, and then creates file by copy-constructing it from the temporary. It is equivalent to QFile file(QFile(filepath));. In your case this will not work, QFile does not allow copy construction, so you’ll need to avoid the temporary by constructing QFile directly,

    // on stack:
    QFile file(filepath);
    
    //or in heap:
    QFile * file = new QFile(filepath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from a Java background and have been using C# for the last
I come from a statically/strongly typed language background (java), and I recently started to
I come from a Java background where the web application is always resident in
I’m relatively new to working with Scala having recently come from a java background.
I come from a Java background, where try/catch is common practice. After few months
I come from a strong Java background and in recent years have been also
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background and am getting more into .NET, what are
I've come from a java background, so i'm still getting to grips with some
I'm new to PHP but come from a Java background and I'm trying to

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.