Reputation: 2297
Consider this class from the WinAPI:
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT, NEAR *NPRECT, FAR *LPRECT;
I am enhancing it in a class named Rect
which allows you to multiply/add/subtract/compare two Rect
s, along with other features. The only real reason I need my Rect
class to know about RECT
is because the class features a conversion operator that allows a Rect
to be passed as a RECT
, and to be assigned a RECT
.
But, in the file Rect.h
, I do not want to include <Windows.h>
, I only want to include <Windows.h>
in the source file so that I may keep my inclusion tree small.
I know that structures can be forward declared like so: struct MyStruct;
But, the actual name of the structure is tagRECT
and it has an object list, so I am kind of confused as to how to forward declare it. Here is a portion of my class:
// Forward declare RECT here.
class Rect {
public:
int X, Y, Width, Height;
Rect(void);
Rect(int x, int y, int w, int h);
Rect(const RECT& rc);
//! RECT to Rect assignment.
Rect& operator = (const RECT& other);
//! Rect to RECT conversion.
operator RECT() const;
/* ------------ Comparison Operators ------------ */
Rect& operator < (const Rect& other);
Rect& operator > (const Rect& other);
Rect& operator <= (const Rect& other);
Rect& operator >= (const Rect& other);
Rect& operator == (const Rect& other);
Rect& operator != (const Rect& other);
};
Would this be valid?
// Forward declaration
struct RECT;
My thought is no, since RECT
is just an alias of tagRECT
. I mean, I know the header file would still be valid if I did this, but when I create the source file Rect.cpp
and include <Windows.h>
there, I fear that is where I am going to experience problems.
How could I forward declare RECT
?
Upvotes: 1
Views: 1749
Reputation: 7353
You doesn't need to know the function definition before actually dereferencing the type.
So you can forward declare in your header file (because you will not make any dereferencing here) then include Windows.h
in your source file.
[edit] Didn't seen that it was a typedef. However, the other answer is wrong : there is a way to (kind of) forward declare a typedef.
Upvotes: 3
Reputation: 137800
You can multiply declare a typedef name, and also forward declare the struct name at the same time:
typedef struct tagRECT RECT;
Note that you can't call a function returning incomplete type, so the conversion operator RECT() const
cannot be called if tagRECT
is only forward declared.
Upvotes: 3