-2

I have created two classes and I declare an object of one class in other class as shown below, but I am getting a warning that the class must declare a canonical assignment operator and needs a copy constructor.

class Base {
public:
    int value;
    virtual int getData() { retun 0; };
}

class test {
public:
    int data;
}

class B : public Base {
    test *var;
}

How do I fix this warning?

David G
  • 94,763
  • 41
  • 167
  • 253
indra
  • 832
  • 4
  • 17
  • 33

1 Answers1

0

This is basic C++: whenever you put a pointer into a class that class requires an explicit copy constructor and assignment operator.

Otherwise you will end up in situations where a class and its copies all try to delete the same object. Or some copies point to an object that was deleted some time ago.

If you are doing tricky things with object lifetimes and raw pointers then declare the copy constructors anyway and write comments explaining what is happening.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Since I am new to the c++ , Can you provide some more info on how to aa the explicit copy constructor and assignment operator. – indra Feb 19 '13 at 15:57
  • @ZanLynx: A copy constructor is not required, which is why this is only a warning. – Sebastian Mach Feb 19 '13 at 16:46
  • @phresnel: I covered that I believe. IF you don't write a copy constructor and assignment operator *bad things* are almost sure to happen. And if you have designed around those bad things then *document them* explicitly. – Zan Lynx Mar 12 '13 at 15:20
  • @ZanLynx: It is not a believe. The standard does not mention that, in case of pointer members, you _need_ a copy constructor or follow the rule of three/five. The detail here is that I am not discussing good style, but only whether it is required or not. __My personal believe__ is that a good class has just one responsibility, and delegates everything else. In other words: Don't use raw pointers at all; delegate the memory-work (a responsibility) to C++ (by only having object members), smart pointers, containers, et al. I just don't want to teach a _de-jure-requiredness_ where there is none. – Sebastian Mach Mar 12 '13 at 16:19
  • @phresnel: Blindly using smart pointers is a performance killer (smart_ptr). In many other cases (unique_ptr) all it achieves is to make the class non-copyable which you could have done yourself. You wouldn't use a smart pointer to implement `std::map` would you? – Zan Lynx Mar 12 '13 at 22:35
  • @ZanLynx: I already mentioned the rule of three/five ;) And nobody mentioned you should use them blindly. Regarding "all it achieves is to make the class non-copyable which you could have done yourself" -> That is good, because with a raw pointer, the compiler will emit really bad copying semantics for your class (I'd call it a randomized bomb), whereas with a unique_ptr, you are forced to think about how to copy. Also, it is near-impossible to non-gurus to implement exception safe copying semantics wit raw pointers by hand. It's way simpler to implement safe copying with unique_ptr. – Sebastian Mach Mar 13 '13 at 09:44