Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking at an example from C++ Gems and have this example:
class shape{
public:
static shape* newShape(const shape&);
virtual void draw()=0;
virtual ~shape(){}
virtual shape* clone() const =0;
}

shape* shape::newShape(const shape& prototype)
{
   shape* S = prototype.clone();
   S->draw();

   return S;
}


my question is at this declaration:
static shape* newShape(const shape&);


shape* can be used here I have no problem, but for the parameter shape& declaration with incomplete type shape, which rule from C++ is applied or interpreted at this parameter declaration?

try to get confirmation from C++ gurus here. Thanks for your insights.

What I have tried:

searched cppreference.com and can not find the rules I needed...
Posted
Updated 10-Jul-21 11:36am
v2

It's a forward reference is all - called a "function prototype" - it's there to give the function signature before you get to the actual definition of the function (Which includes the body). It allows any function to call it no matter where in the class the calling function is declared relative to the function being called.

If you didn't do this, then this wouldn't work:
C++
void foo(int x)
   {
   bar(666 * x);
   }
void bar(int x)
   {
   ...
   }
As the system wouldn't "know" what parameters bar should accept.
 
Share this answer
 
Comments
Southmountain 10-Jul-21 21:28pm    
thank you OG!
OriginalGriff 11-Jul-21 0:50am    
You're welcome!
A class can be used before it is defined if the compiler doesn't need to know its size. So Class* and Class& are both OK as long as it's known, at that point, that Class is a class. This is often done with a forward declaration that precedes the appearance of Class:
C++
class Class;
For Class* and Class&, the size of Class doesn't have to be known because the first is just a pointer, and the second is also (secretly) a pointer. But take away the * or the &, and Class would need to have been defined, not just declared, at that point, because the compiler would need to know how many bytes to set aside for it.

A forward declaration is used to avoid the need for an #include. If a header uses Class "in name only" (that is, only as a pointer or reference), it can insert the above code rather than #include "Class.h". In a large system, doing this wherever possible can significantly speed up compilation time.

In your example, newShape is a factory for creating a shape from an existing one, much like a copy constructor. It must therefore be able to reference a shape before shape has been fully defined.

This can also happen with data members:
C++
class Queueable  // a virtual base class for items that can be queued
{
   Queueable* next_;  // next item in queue
};
In your example and this one, a separate forward declaration isn't needed because the class in question is currently being parsed when its name is used.
 
Share this answer
 
v2
Comments
Southmountain 10-Jul-21 21:29pm    
thank you! I get it now.
Greg Utas 11-Jul-21 10:00am    
Glad it helped.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900