Results 1 to 3 of 3

Thread: condused with copy construcotrs

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default condused with copy construcotrs

    Hi,

    AFAIK, a "default" copy constructor (i.e the one the compiler will implicitly generate if you didn't define your own copy constructor) will copy all non static members using their copy constructors.

    But what do I do, when I have a class that has various parameters that MUST be given at initialization?
    Lets say I have the following class:
    Qt Code:
    1. class myClass(QString port)
    2. {
    3. private:
    4. QString m_port;
    5. //code
    6. };
    To copy to clipboard, switch view to plain text mode 
    now, after initialization, port will be assigned to m_port.
    If I don't define my own copy constructor, then the code:
    Qt Code:
    1. myClass A("ttyS0");
    2. myClass B(A);
    To copy to clipboard, switch view to plain text mode 
    will create class B with m_port == "ttyS0".
    Which is a dangerous thing.

    So my question is:
    Is there a way for me to disable the default copy constructor?
    Defining my own copy constructor is not possible, since I it need to get the port parameter from the user, and each class needs to have its own and different from the others port parameter.

    Thanks.
    EDIT: subject should read "Confused with copy construcotrs
    Last edited by high_flyer; 24th September 2006 at 22:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: condused with copy construcotrs

    Quote Originally Posted by high_flyer View Post
    Is there a way for me to disable the default copy constructor?
    Just declare it as private along with operator=().

  3. The following user says thank you to jacek for this useful post:

    high_flyer (24th September 2006)

  4. #3
    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: condused with copy construcotrs

    Quote Originally Posted by high_flyer View Post
    Is there a way for me to disable the default copy constructor?
    You may declare it as private (and leave unimplemented)..
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    high_flyer (24th September 2006)

Similar Threads

  1. Copy progress bar
    By safknw in forum Newbie
    Replies: 13
    Last Post: 16th September 2006, 10:02
  2. file copy in Qt 3.3.4 ?
    By npc in forum Newbie
    Replies: 6
    Last Post: 31st March 2006, 15:43
  3. implicit or shared copy?
    By SkripT in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2006, 15:14
  4. how can i copy a directary using QDir
    By monaem in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2006, 13:12

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.