Re: When are compiler generated functions generated?
Andrew Shepherd wrote:
Suppose I had this class:
class NonCopyable
{
private:
NonCopyable(const NonCopyable&);
NonCopyable& operator=(const NonCopyable&);
};
//////////////////////////////////////////////////////////
// BigClass.h
class BigClass : NonCopyable
{
private:
typedef std::vector<Widget> VecWidget;
typedef std::set<VecWidget> VecWidgetSet;
typedef std::multi_map<DooDad, VecWidgetSet> OneReallyUglyMap;
OneReallyUglyMap m_reallyUglyMap;
};
/////////////////////////////////////////////////////////////////////////////
In my project, there are 100 .CPP files that include BigClass.h. As
the code indicates, the class is never copied.
Is the compiler going to go ahead and generate a copy constructor and
assignment operator, even though I don't want or need one?
No.
If, upon reaching the closing brace of a class definition, no
copy-constructor has bee declared for that class, then the compiler
will add a *declaration* for a copy constructor and make a note that
the definition has to be generated the first time it is actually
needed.
If the copy-constructor is never used, no code for it may be generated.
The same holds for the other automatically generated members, such as
default constructor, destructor and copy-assignment operator.
If the compiler is going to generate these functions, will it generate
these functions once, or for every .CPP file that includes this?
It will be generated for each .CPP file where that member function is
actually needed.
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]