Thread: How to initialize in constructor?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    How to initialize in constructor?

    I have a class that I want to hold a value in like this -
    Code:
    class guiwindow {
    public:
        guiwindow();
        ~guiwindow();
    
        Fl_Window& getWindow();
    private:
        Fl_Window window;
    };
    #endif
    Then I want to initialize it in the constructor like this -

    Code:
    guiwindow::guiwindow() {
        Fl_Window win( 300,200,"Testing");
        win.begin();
        Fl_Button but( 120, 90, 70, 30,"Click me" );
        win.end();
        but.callback(but_cb);
        win.show();
        Fl::run();
        window = win;   //HERE
    }
    Why won't this work? Is there a way to make it work without overloading the = operator or declaring it as a pointer in the header? Any help is appreciated.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Use the initializer list.
    Code:
    guiwindow::guiwindow(): window( 300,200,"Testing") {
        window.begin();
        Fl_Button but( 120, 90, 70, 30,"Click me" );
        window.end();
        but.callback(but_cb);
        window.show();
        Fl::run();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-21-2011, 02:19 AM
  2. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  3. Replies: 6
    Last Post: 05-19-2010, 04:03 AM
  4. Replies: 10
    Last Post: 06-02-2008, 08:09 AM
  5. using constructor to initialize data
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2002, 08:55 PM