How to prevent compiler-generated member functions?

LLeweLLyn Reese llewelly@lifesupport.shutdown.com
Mon Apr 21 20:33:00 GMT 2003


Nicolas F Rouquette <nicolas.rouquette@jpl.nasa.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();

};





More information about the Gcc-help mailing list