Results 1 to 5 of 5

Thread: default parameters in constructor class

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default default parameters in constructor class

    hello, I've got one class constructor with two option parameter; so I can create the object like this:
    Qt Code:
    1. Myobj::Myobj( char* parone="", char* partwo="") :
    2. _name(parone), _value(partwo) {........}
    3.  
    4. Myobj obj(); // in this case warning
    5. Myobj obj("one", "two"); //this ok.
    6.  
    7. //warning is: prototyped function not called (was a variable definition intended?)
    To copy to clipboard, switch view to plain text mode 
    I suppose that to using default parameters is necessary has first parameters not default. Is this? Anyway: I'd like avoid a second constructor because I had to duplicate its contents (and It could be large). Ideas?

    THanks..
    Regards

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: default parameters in constructor class

    Qt Code:
    1. Myobj obj;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: default parameters in constructor class

    in that way compiler gets:
    error: no appropriate default constructor available
    Regards

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: default parameters in constructor class

    The default parameter values should be defined in the header file, not in .cpp file.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: default parameters in constructor class

    Be also aware that the default parameter method allows someone to call your constructor with just the first parameter filled in. If you want to have either both or none specified, you need two constructors. You can delegate the construction process to another function, so you won't have code duplication.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.