PDA

View Full Version : condused with copy construcotrs



high_flyer
24th September 2006, 21:09
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:


class myClass(QString port)
{
private:
QString m_port;
//code
};

now, after initialization, port will be assigned to m_port.
If I don't define my own copy constructor, then the code:


myClass A("ttyS0");
myClass B(A);

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

jacek
24th September 2006, 21:13
Is there a way for me to disable the default copy constructor?
Just declare it as private along with operator=().

jpn
24th September 2006, 21:14
Is there a way for me to disable the default copy constructor?
You may declare it as private (and leave unimplemented)..