Thread: Why would a class allocate an instance of itself?

  1. #1
    Guest
    Guest

    Why would a class allocate an instance of itself?

    I'm currently learning the Qt framework and doing my first tutorial. Straight away I saw something that baffled me:

    notepad.h
    Code:
    namespace Ui {
    class Notepad;
    }
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit Notepad(QWidget *parent = 0);
        ~Notepad();
    
    private:
        Ui::Notepad *ui;
    };
    notepad.cpp
    Code:
    Notepad::Notepad(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::Notepad)
    {
        ui->setupUi(this);
    }
    
    Notepad::~Notepad()
    {
        delete ui;
    }
    Note the ui pointer and the heap allocation in the class constructor; I can't wrap my head around why one would do this. What's going on here?

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    Ui::Notepad and ::Notepad are seperate classes.

    I believe Ui::Notepad contains the generated UI code, but i am not that familiar with Qt.

  3. #3
    Guest
    Guest
    Yes, that makes sense. I guess it's an unfortunate case of names overlapping, i.e. the two Notepads aren't directly related. Since UI::Notepad does not appear to be included in the header, it's probably a forward declaration. The source file does include a special header file that hasn't been created yet because I haven't built the unfinished project.

    I was concerned this was some advanced C++ technique that I wasn't aware of. Thanks for the hint!

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Guest View Post
    Yes, that makes sense. I guess it's an unfortunate case of names overlapping, i.e. the two Notepads aren't directly related. Since UI::Notepad does not appear to be included in the header, it's probably a forward declaration. The source file does include a special header file that hasn't been created yet because I haven't built the unfinished project.

    I was concerned this was some advanced C++ technique that I wasn't aware of. Thanks for the hint!
    I think the definition of Ui::Notepad is the auto generated class definition from a .ui file (created with the Designer). It should contain a C++ equivalent of the XML in the .ui file. The header generated might have a 'ui_' prefix, and it will contain a lot of other headers (so it's probably best to forward declare like this in another header, instead of including it)

    Edit: One example of a class allocating itself though is called a singleton. You basically have a private constructor, and access the allocated instance with something like this:

    Code:
        static EventHandler* Instance()
        {
            if(!s_pInstance)
            {
                s_pInstance = new EventHandler();
            }
            return s_pInstance;
        }
    A singleton is basically a fancy global though. You can access it like this anywhere the header is included (the declaration of s_pInstance being in a source file for the class):

    Code:
       EventHandler::Instance()->getMousePosition();
    Last edited by Alpo; 10-04-2014 at 07:12 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Singleton is also considered an anti-pattern by many, though. Just so you know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    This is also an pretty example of "PIMPL" or "pointer to implementation" technique. Described best by the original author:
    Pimpls - Beauty Marks You Can Depend On

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  2. Delete instance of a class array
    By wbeasl in forum C++ Programming
    Replies: 6
    Last Post: 10-22-2007, 07:56 AM
  3. Declaring an instance of a class inside a class
    By nickodonnell in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2005, 11:46 PM
  4. pointer to an instance of a different class
    By xlix in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2003, 04:19 PM
  5. Instance of a class returning itself
    By Boksha in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 11:14 AM

Tags for this Thread