Can I change the code for classes without copying so that the VS2010 compiler notes an error on the violation line?

Can I change the code so that the VS2010 compiler error message points to a line with a code violation?

class NoCopy
{ //<-- error shows up here
   NoCopy( const NoCopy& ); //<-- and error shows up here
   NoCopy& operator=( const NoCopy& );
 public:
   NoCopy(){};
};

struct AnotherClass :NoCopy
{
}; //<-- and error shows up here

int _tmain(int argc, _TCHAR* argv[])
{
  AnotherClass c;
  AnotherClass d = c; //<-- but the error does not show up here
  return 0;
}

Note that 'NoCopy (const NoCopy &) = delete;' not compiled in VS2010. I can not use boost.

This was added at the suggestion of Micah:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>  Test.cpp
1>c:\Test\Test.cpp(16): error C2248: 'NoCopy::NoCopy' : cannot access private member declared in class 'NoCopy'
1>          c:\Test\Test.cpp(8) : see declaration of 'NoCopy::NoCopy'
1>          c:\Test\Test.cpp(7) : see declaration of 'NoCopy'
1>          This diagnostic occurred in the compiler generated function 'AnotherClass::AnotherClass(const AnotherClass &)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
+3
source share
2 answers

The error does not appear on the correct line, because Visual Studio does not know where it came from, it is automatically compiled AnotherClass(const AnotherClass&). You must explicitly define this so that Visual Studio continues to find where the error came from.

class NoCopy {
   NoCopy( const NoCopy& );
   NoCopy& operator=( const NoCopy& );
 public:
   NoCopy(){};
};

struct AnotherClass :NoCopy
{
    AnotherClass();  // Since there is another constructor that _could_ fit,
                     // this also has to be defined
private:
    AnotherClass(const AnotherClass&);  // Define this one
};

int _tmain(int argc, _TCHAR* argv[])
{
  AnotherClass c;
  AnotherClass d = c; //<-- error is now shown here
  return 0;
}

Now you will receive:

1 > \main.cpp(20): C2248: 'AnotherClass:: AnotherClass': , 'AnotherClass'

"" .

+1

, :

Error   1   error C2248: 'NoCopy::NoCopy' : cannot access private member declared in class 'NoCopy' main.cpp    11  1

, (, , - -).

, : , =? , , .

0

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


All Articles