When compiler need to know the size of a C (class) object: For example, when allocating a C on the stack or as a directly-held member of another type
From C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
Does that mean for a heap allocated object, size is not necessary?
Class C;//just forward declaration
C * objc = new C();
To answer your specific question:
Does that mean for heap allocated object size is not necessary?
Class C;//just forward declaration C * objc = new C();
C++ will not let you do that.
Even if it could let you perform a 'new
' on an incomplete type by magically resolving the size at a later time (I could envision this being technically possible with cooperation from the linker), the attempt will fail at compile time because of at least 2 reasons:
operator new
can only be used with complete types. From the C++98 standard 5.3.4 - "[the allocated] type shall be a complete object type, but not an abstract class type or array thereof"
the compiler has no idea what constructors exist (and are accessible), so it would also have to fail for that reason.