How can I find where the C ++ copy constructor is used through a compilation error?

In short: is it possible to somehow change the class definition so that it cannot be compiled to the point of use of the copy constructor, regardless of where it was used?

I have a very large project and I am clearing some class definitions. There is a class in which I clearly do not want to use copy constructors (let it ignore why this happens for the sake of this discussion), and in the interest of security, I decided that I would simply define the copy constructor as private, and not actually implement it ... like that this way it will throw a compilation error if I try to use it anywhere. Lo and behold, it compiles fine, but I have a linker error ... implementation of copy constructor not found! Presumably, this means that it is used somewhere, but I cannot find where it is used. By the way, this is Visual Studio 2010. So my question is, can I somehow change the class definition so that it does not compile at the point of use?

class Sample { private: // not implemented Sample( const Sample& rhs ); Sample& operator=( const Sample& rhs ); public: // implemented Sample(); ... }; Sample *samp1 = new Sample; Sample *samp2 = new Sample( *samp1 ); // <<-- inaccessible here! this works 

Presumably since I do not find a compilation error, but I click on the linker error, which means that the class (or friend) itself is doing the copy-built creation (since everything that will have access to the private constructor), but I'm sure that I can not find him!

+4
source share
6 answers

in C ++ 11 you can change the definition to

 class Sample { private: // not implemented Sample( const Sample& rhs ) = delete; Sample& operator=( const Sample& rhs ) = delete; public: // implemented Sample(); ... }; 

before C ++ 11, this is usually done by inheriting from a class that declares a private copy constructor, such as boost :: NonCopyAble (you can just copy this class, these are just a few lines). In this case, your class (or any friends or children) also cannot access the copy constructor, and it will generate a compile-time error.

+7
source

Inherit from noncopyable class:

 class noncopyable { private: // not implemented noncopyable( const noncopyable& rhs ); noncopyable& operator=( const noncopyable& rhs ); }; class Sample : private noncopyable { private: // not implemented Sample( const Sample& rhs ); Sample& operator=( const Sample& rhs ); public: // implemented Sample(); // ... }; Sample *samp1 = new Sample; Sample *samp2 = new Sample( *samp1 ); // <<-- compile-time error 

This works fine even if you don't have C ++ 11 (where the delete method mentioned elsewhere is probably preferable).

+5
source

What is the error that the linker creates? If this is LNK2019, then it will be easy to track a function using the copy constructor:

MSDN says its format is:

unresolved external symbol "symbol" specified in the function "function"

If you look at this error message, you may find a method that calls the undefined copy constructor.

+3
source

Are you trying to get the module number + line at compile time? Try making the design template a template:

 class A { public: template< typename T > A( A const & ) { } A() { } }; int main( void ) { A a; A b( a ); // main.cpp(43) : error C2558: class 'A' : no copy constructor available or copy constructor is declared 'explicit' return ( 0 ); } 
+1
source

As I recall, if you declare it inline , sometimes you will get a compiler error saying that it was declared built-in, but not defined. It was a long time ago, and with gcc. YMMV.

[It is shown that does not work; leaving this to posterity.]

0
source

If the member is private, you should already get an error at the place of use if it does not have permission to access private members.

To get the same error in functions that have private access, you must place a private copy-ctor declaration somewhere that they do not have access to, for example, as a private member of the base class.

VS2010 does not yet support it, but declaring a function as remote will also work.

0
source

Source: https://habr.com/ru/post/1393404/


All Articles