0

have written this little class, which generates a UUID every time an object of this class is created.

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>    

class myClass {

public:

    boost::uuids::uuid GetUUID();

    virtual ~myClass();

    myClass() {   // constructor
        mId = boost::uuids::random_generator()();
        std::cout<< "object created with uuid " << mId <<std::endl;
    }

private:

    boost::uuids::uuid mId;

}

At some point, I am pushing these objects to a vector and equating that vector with another vector using simple assignment operator. To ensure the objects in the new vector do not generate new UUIDs, I want to write a copy constructor. But as you can see, the constructor needs no arguments. So I am not sure how to write the copy constructor. Moreover, if I have multiple variables instead of just one UUID, how do I handle that situation?

Subhamoy S.
  • 6,566
  • 10
  • 37
  • 53
  • 1
    You are comparing using the assignment operator? Or what do you mean by `equating`? And the `constructor needs to arguments` did you mean `two` arguments or `no` arguments? – RedX Jul 04 '12 at 09:31
  • Sorry for the mistakes. I have corrected them. – Subhamoy S. Jul 04 '12 at 09:33

3 Answers3

5

You need the following signature:

 myClass(const myClass& other)
 {
     mId = other.mId;
 }

and also the assignment operator

 myClass& operator=(const myClass& other)
 {
     mId = other.mId;
     return *this;
 }

Note that if you don't implement these, the compiler-generated ones will do a shallow copy, so no new UUIDs will be generated. But you should have them there to obey the rule of three (you do have a destructor after all).

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • His destructor is only declared in order to be `virtual`, so it doesn't impose the other two. (On the other hand: `virtual` destructor means that the class should be polymorphic, so assignment and copy probably shouldn't be supported.) – James Kanze Jul 04 '12 at 09:32
  • @JamesKanze I prefer always implementing all three, even if only to explicitly state that it's not an oversight that they aren't there. – Luchian Grigore Jul 04 '12 at 09:34
  • 1
    In my own code, if the class has a virtual destructor, then it is designed to be used as a base class, and it normally won't support copy and assignment. Which is done today by inheriting from `boost::noncopyable`, and in which case, I don't provide a copy constructor nor an assignment operator. In his case, since he's talking about putting the objects in a container... Either he should drop the virtual destructor, since they can't be polymorphic, or he should rethink his design, possibly using a container of pointers. – James Kanze Jul 04 '12 at 09:38
  • @JamesKanze I doubt this is the exact code though. He doesn't even declare `mId` which is obviously there. – Luchian Grigore Jul 04 '12 at 09:39
  • Thank you very much for the response! Missing `mId` was a silly mistake on my part, which I fixed soon after. I am curious why both a copy constructor and an assignment operator signature are necessary. What would be wrong if only copy constructor was used and there was no assignment operator defined? – Subhamoy S. Jul 04 '12 at 11:11
  • @SubhamoySengupta http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) – Luchian Grigore Jul 04 '12 at 11:12
  • 1
    @SubhamoySengupta also this - http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – Luchian Grigore Jul 04 '12 at 11:12
2

The compiler will generate a copy constructor and an assignment operator for you. If the only thing this operations have to do, is to copy all members, then the compiler generated operations will exactly do this. If not, the signature for a copy constructor is:

myClass( const myClass& other )

The signature for the assignment is:

myClass& operator=( const myClass& other )

kind regards

Torsten

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35
0

In this case the default copy constructor and assignment operator generated by the compiler should do what you expect, so you don't need to define it explicitly.

Sdra
  • 2,297
  • 17
  • 30