Harsha Rama
Harsha Rama

Reputation: 349

What does the statement class foo; mean

I have been going through the code of a project and have encountered the statement

 class foo;
.
.
.
.
 foo* f1;

in various places. The class has not been declared in any of the headers included either. Can anybody tell me what that means.

Upvotes: 0

Views: 5478

Answers (3)

hyde
hyde

Reputation: 62797

It is a forward declaration. It can be used for classes, structs and functions, and it tells compiler that this is defined elsewhere or later.

For classes, there are (at least) two use cases.

1. Full definition not needed

After forward declaration, compiler does not know size or members of class, only name. That is enough for pointers to the class (and references which are basically syntactic sugar around pointers). But often pointer is enough, and then you can avoid including entire header file in another. This helps compilation speed, by avoiding need to recompile everything when one header changes.

myfuncs.h

class MyClass; // forward declaration
void helpMyClass(MyClass &needy);

// here this would give compiler error about incomplete type:
//void badHelp(MyClass needy); // value needs definition

myfuncs.cpp:

#include "myclass.h" // entire MyClass definition
void helpMyClass(MyClass &needy) {
  needy.helpMe(false); // needs full definition
}

Important use case for this is the so called PIMPL idiom, also well covered here at SO under tag.

2. Two classes need to refer to each others

class node; // forward declarion

class collection {
    node *frist; // pointer enabled by forward declaration
}

class node {
    collection *owner; // already defined above so works too
}

In this case forward declaration is required to make this work nicely. Just saying in case you see it in the wild, there's the ugly way of using void pointer and casts, sometimes used when novice programmer does not know how this should be done.

Upvotes: 4

Brandon
Brandon

Reputation: 23498

Your declaration is incorrect? I'm not sure.. I do know that you can't have "any" space "name".. Perhaps you missed an underscore?

I believe you meant:

class foo any_name();

In that case, it's forward declaring a function called any_name that returns a class instance of foo.

Example:

#include <iostream>

class foo any_name();  //The forward declaration..

class foo   //the foo class implementation.
{
    public:
        foo(){std::cout<<"hey";}
};

class foo any_name() //The implementation of the function..
{
    std::cout<<"any_name";
    //return {};   //Can be used to return a constructed instance of foo.
};

 int main()
 {
     any_name();
 }

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62063

I think you're referring to a forward declaration. It tells the compiler that a class named foo will be defined later. Until then it is an "incomplete type", meaning that pointers and references to the class can be defined. Instances of the class cannot be created until it is fully defined.

Upvotes: 3

Related Questions