GSI Forum
GSI Helmholtzzentrum für Schwerionenforschung

Home » CBM » CBMROOT » General » Effective C++
Effective C++: Copy constructor for classes with pointer members [message #13070 is a reply to message #13067] Fri, 17 February 2012 09:16 Go to previous messageGo to previous message
Volker Friese is currently offline  Volker Friese
Messages: 365
Registered: April 2004
Location: GSI CBM
first-grade participant
From: *gsi.de
Hi

The second common warning is the following.

Quote:


warning: 'class CbmFieldMap' has pointer data members
warning: but does not override 'CbmFieldMap(const CbmFieldMap&)'
warning: or 'operator=(const CbmFieldMap&)'



The problem here is that both functions are automatically created by the compiler if the are not declared. If the class has pointer data members one should explicetly define what both
functions should do with this pointers. If neither of the both functions are needed the solution of the problem is quite simple. If one needs the functions it get complicated (not discussed in this topic).

All of the automatically generated functions are public and can be accessed by everybody. If you declare both functions as private but don't implement them the problem is solved. The compiler will not generate the functions automatically and since the functions are private nobody can use them.
If now any of the two functions is called from another class there will be a compiletime error because the function is declared private.
If the functions are called from other functions of the same class or functions of a friend class there will be no compiletime but a linktime error. So in any case it will not be possible to use any of the two functions.

Add the following part to the header file.

private:
  CbmFieldMap(const CbmFieldMap&);
  CbmFieldMap operator=(const CbmFieldMap&);   


Thats it.
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Field maps available from the repository
Next Topic: Coordinate system for hit parameters
Goto Forum:
  


Current Time: Sat Dec 04 11:11:13 CET 2021

Total time taken to generate the page: 0.02437 seconds