This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to prevent compiler-generated member functions?


Nicolas F Rouquette <nicolas dot rouquette at jpl dot nasa dot gov> writes:

> Suppose we have this code:
> 
> class foo {
> public:
>    ~foo();
> };
> 
> void bar()
> {
>     foo f1; // (1)
>     foo f2; // (1)
>     f1 = f2; // (2)
>     foo f3 = foo(); // (3)
> }
> 
> Is there a compiler switch where (1), (2), (3) would all produce errors?
> 
> (1) => no default constructor defined
> (2) => no copy assignment operator defined
> (3) => no copy constructor defined
> 
> Without any flags, this code compiles cleanly.

I think this question is about C++, and not unique to gcc.

The way to prevent the compiler from generating these functions is to
delcare them:

class foo
{
  foo(); //Private, and no definition provided.
         //If used, there will be a compile time error.

  foo(foo const&);//Likewise.
  foo& operator=(foo const&);//Likewise.
public:
  ~foo();

};




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]