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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:06:11+00:00 2026-05-24T12:06:11+00:00

I have a C++ static class with a method that creates an object. I

  • 0

I have a C++ static class with a method that creates an object. I would like to retrieve the object created by this method in a different function so that this new function takes ownership of the object. This is the code I have so far:

MessageBoxes.h

class MessageBoxes {

public:

    static int info(const QString& message, const QString& title = _("Information"), QMessageBox::StandardButtons buttons = QMessageBox::Ok);
    static int confirmation(const QString& message, const QString& title = _("Confirmation"), QMessageBox::StandardButtons buttons = QMessageBox::Ok | QMessageBox::Cancel);
    static int error(const QString& message, const QString& title = _("Error"), QMessageBox::StandardButtons buttons = QMessageBox::Ok);

private:

    static QMessageBox& createMessageBox(const QString& message, const QString& title = "", QMessageBox::StandardButtons buttons = QMessageBox::Ok);

};

MessageBoxes.cpp

QMessageBox& MessageBoxes::createMessageBox(const QString& message, const QString& title, QMessageBox::StandardButtons buttons) {
    QMessageBox msgBox;
    msgBox.setWindowTitle(title);
    msgBox.setText(message);
    msgBox.setStandardButtons(buttons);
    return msgBox;
}

int MessageBoxes::confirmation(const QString& message, const QString& title, QMessageBox::StandardButtons buttons) {
    QMessageBox m = createMessageBox(message, title, buttons);
    return m.exec();
}

The problem is that at the line QMessageBox m = createMessageBox(message, title, buttons), the compiler tells me that the copy constructor of QMessageBox is disabled. This is fine, however I do not want to make a copy, I want to get the actual object that was created in createMessageBox. I declared the return type of createMessageBox as QMessageBox& assuming that it would return the reference but it doesn’t seem to work that way. Any idea how I can do that?

  • 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-24T12:06:12+00:00Added an answer on May 24, 2026 at 12:06 pm

    0) We don’t do this “static class” thing in C++. Those are hacks to deal with the fact that Java and C# force you to put all your code into classes. C++ does not do that, so we shouldn’t hack around a limitation that doesn’t exist. Classes are not storage places for code; they exist to define a data type. What you’re really trying to do is organize the code by grouping it under a common name; we do that with a namespace.

    1) You may not return a reference to a local variable. References are for returning already-existing things.

    2) You don’t want to return a reference, because the purpose of createMessageBox is to create a message box. You return a value: the message box that was created.

    3) When you write

    Foo bar = something();

    the result from something() is copied, even if something() did happen to return a reference to an already-existing thing. This is because the type of bar is Foo, and not Foo&. bar must hold its own Foo; it cannot refer to one, because it isn’t a reference. And since the Foo returned by something() is a value in its own right, with its own location in memory, we can’t just cause it to “be” bar; the program must make a copy.

    To refer to the result from the function, you would write Foo& bar = something();. This will extend the lifetime of the returned object (which ordinarily would go out of scope right away), so there is no problem with referring to a temporary.

    4) However, optimizing compilers are smart, and this is unlikely to gain you anything. If you just return a value and assign by value, chances are good the copy will not actually happen (although the Standard says in effect that your code must be prepared for that possibility).

    5) Dynamic allocation is, honestly, a really bad idea here. If you must do it, at least use some kind of smart-pointer wrapper. Although, when your copy constructor is disabled, sometimes you’re stuck with this sort of thing. :/

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

Sidebar

Related Questions

Suppose that I have a Java class with a static method, like so: class
I have a static class that I would like to raise an event as
We have a static method in a utility class that will download a file
I have a public static class in which I would like to have a
I have a helper class that creates some objects, like a builder. The helper
I have a helper class that is just a bunch of static methods and
So I basically want to have a static method in my AR class to
I have the following class: public abstract class AbstractParent { static String method() {
I got a Utility module since VB.NET doesn't have static class like C# and
I want to create a static class in PHP and have it behave like

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.