Question about declaring classes

A

Antonio Rivas

Hello again :)

Lately I find code that declares a class this way:

mainheader.h
------------
#include "myClass.h"

....

class myClass;

.....

This declaration use to be in a header (mainheader.h in this case) that
calls another header in which that class is declared in a standard way,
i.e.:

myClass.h
---------
class myClass {

public:
myClass();
~myClass();
....
}; // myClass

What I don't understand is the purpose of the second empty declaration
of myClass after call myClass.h where is declared for the first time.
What puzzles me most is that there's no safeguard #ifdef to prevent
multiple declarations of myClass and the only reason I see for such
absence is that is legal code that won't throw a multiple declaration
error and that has a reason to make it this way.
An enlightment in this matter will be welcome.
 
O

Ondra Holub

Hello again :)

Lately I find code that declares a class this way:

mainheader.h
------------
#include "myClass.h"

...

class myClass;

....

This declaration use to be in a header (mainheader.h in this case) that
calls another header in which that class is declared in a standard way,
i.e.:

myClass.h
---------
class myClass {

public:
myClass();
~myClass();
...

}; // myClass

What I don't understand is the purpose of the second empty declaration
of myClass after call myClass.h where is declared for the first time.
What puzzles me most is that there's no safeguard #ifdef to prevent
multiple declarations of myClass and the only reason I see for such
absence is that is legal code that won't throw a multiple declaration
error and that has a reason to make it this way.
An enlightment in this matter will be welcome.

I do not know what could it be good for. I think it is nonsense.

The forward declaration of class is useful when such class is used
only be its pointers or references, so you can decrease header
dependencies:

// file my_class.hpp

#ifndef MYCLASS_HPP_INCLUDED
#define MYCLASS_HPP_INCLUDED

class MyClass
{
// ...
};

#endif


// file other_class.hpp

#ifndef OTHER_CLASS_HPP_INCLUDED
#define OTHER_CLASS_HPP_INCLUDED

class MyClass;

class OtherClass
{
// ...

// No need to #include myclass.hpp for following line
void Fn(MyClass& my_class);
};

#endif


// file other_class.cpp

#include "other_class.hpp"
#include "my_class.hpp"

void OtherClass::Fn(MyClass& my_class)
{
// For following line we need to have included my_class.hpp
my_class.method();
}
 
A

Antonio Rivas

Ondra Holub escribió:
I do not know what could it be good for. I think it is nonsense.
Was my first thought too the first time I saw it but as I said I find
such forward declarations quite often lately.
The forward declaration of class is useful when such class is used
only be its pointers or references, so you can decrease header
dependencies:

// file my_class.hpp

#ifndef MYCLASS_HPP_INCLUDED
#define MYCLASS_HPP_INCLUDED

class MyClass
{
// ...
};

#endif


// file other_class.hpp

#ifndef OTHER_CLASS_HPP_INCLUDED
#define OTHER_CLASS_HPP_INCLUDED

class MyClass;

class OtherClass
{
// ...

// No need to #include myclass.hpp for following line
void Fn(MyClass& my_class);
};

#endif


// file other_class.cpp

#include "other_class.hpp"
#include "my_class.hpp"

void OtherClass::Fn(MyClass& my_class)
{
// For following line we need to have included my_class.hpp
my_class.method();
}

This makes sense to me. Reduce header dependencies could be the reason.

Thank you for the enlightment :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top