By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

forward declarations of a data structure

P: n/a
I am rewriting some memory management code and I need to have a forward
declaration of a data structure. I am not converting this data structure into a
class (yet). How do I generate a forward reference of a data structure ? For
a class, I just say:

class SomeClass;

"struct SomeStructure;" does not work to forward declare the data structure.

This is the structure that I am trying to generate a vector of pointers to:

typedef struct
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
} STREAM_E;
typedef STREAM_E STREAM;

Thanks,
Lynn McGuire
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Lynn wrote:
I am rewriting some memory management code and I need to have a forward
declaration of a data structure. I am not converting this data structure into a
class (yet). How do I generate a forward reference of a data structure ? For
a class, I just say:

class SomeClass;

"struct SomeStructure;" does not work to forward declare the data structure.
Huh? What do you mean by "does not work"?

This is the structure that I am trying to generate a vector of pointers to:

typedef struct
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
} STREAM_E;
typedef STREAM_E STREAM;


So, you have a typedef to a typedef? I really don't understand the need
for that. Can't you simply say

struct STREAM {
DWORD ID; ...

???

V
Jul 22 '05 #2

P: n/a
> class (yet). How do I generate a forward reference of a data structure ? For

BTW, I am trying to generate forward references to this typedef'd data
structure in:

std::vector <STREAM *> hClipStrmList; /* List of streams on the clipbrd */

This forward refererence works OK, it is the formal declaration of the STREAM_E
data structure which is blowing up.

Thanks,
Lynn
Jul 22 '05 #3

P: n/a
> So, you have a typedef to a typedef? I really don't understand the need
for that. Can't you simply say

struct STREAM {
DWORD ID; ...


I am on the fifth version of this data structure. There is typedef's to
STREAM_A to STREAM_E. The latest STREAM_E structure is
typedef'd to the name STREAM. So, all throughout my code, all
I have is references to STREAM or STREAM *.

Thanks,
Lynn
Jul 22 '05 #4

P: n/a

"Lynn" <NO****@NOSPAM.com> wrote in message
news:co********@library2.airnews.net...
class (yet). How do I generate a forward reference of a data structure ?
For


BTW, I am trying to generate forward references to this typedef'd data
structure in:

std::vector <STREAM *> hClipStrmList; /* List of streams on the clipbrd */

This forward refererence works OK, it is the formal declaration of the
STREAM_E
data structure which is blowing up.

Thanks,
Lynn


Don't use that old C-style typedef stuff. Just declare the struct like
this:

struct STREAM
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
};

By the way, there is no difference between a struct and a class, except the
fact that the default visibility of the members of a class is private, while
for a struct it is public. (I think the same applies for the default type
of inheritence, but I forget now.) So if you can forward-declare a class,
you can forward-declare a struct, too. I seem to recall that there was a
version of C++ long ago where a struct was used when we just wanted to have
data in the object, and a class was used when we wanted to introduce
functions to it. That's simply not the way class and struct are defined, at
least not now. It's still common practice though, to use struct for simple
data-holding objects (which have no member functions), but that's really
just because the default visibility is public. One could just as easily use
a class, and put the public: specifier before the member variables. One
other thing: just because you don't declare any member functions, doesn't
mean they don't exist. The compiler will generate certain required
functions for you, such as a constructor, destructor, copy constructor and
assignment operator, when it needs them. It's just that there's no
"user-defined" version of them. My point is: stop thinking of struct as
simply data.. it's just like a class object, except for the
public/privatedifference(s).

-Howard

Jul 22 '05 #5

P: n/a
> Don't use that old C-style typedef stuff. Just declare the struct like this:

struct STREAM
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
};


OK, I wrote my forward reference as:

struct STREAM_E;
typedef STREAM_E STREAM;

And, I changed my structure declaration to be:

struct STREAM_E
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
};
typedef STREAM_E STREAM;

Seems to compile OK at the moment using VC++ 2003.

I do not like having to forward declare STREAM to be typedef'd to the
STREAM_E struct but this seems to be an evil necessity.

I need the STREAM_E because some day I expect to have STREAM_F,
STREAM_G, etc ... and there are binary versions of this data structure
(and it's previous representations) in customer's files for the last 15 years.

This code was originally C code written in the middle 1980s and was
"loosely" converted to C++ about 1995. Life is a legacy !

Thanks,
Lynn McGuire
Jul 22 '05 #6

P: n/a
Lynn wrote:
Don't use that old C-style typedef stuff. Just declare the struct like this:

struct STREAM
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
};

OK, I wrote my forward reference as:

struct STREAM_E;
typedef STREAM_E STREAM;

And, I changed my structure declaration to be:

struct STREAM_E
{ /* Used from v5.0 */
DWORD ID; /* unique ID of stream, remains unchanged in drawing */
DWORD VertxCount; /* Number of vertices on stream */
DWORD VertxIndex; /* Index of first vertex of stream */
DWORD Layer; /* Layer that stream is drawn on */
DWORD Style; /* Line-style - dotted, dashed etc */
BYTE Hatch; /* Line-hatching - cross, sine etc */
BYTE Status; /* Status of stream */
DWORD Type; /* Line-type, ALWAYS 0 at present */
};
typedef STREAM_E STREAM;

Seems to compile OK at the moment using VC++ 2003.

I do not like having to forward declare STREAM to be typedef'd to the
STREAM_E struct but this seems to be an evil necessity.

I need the STREAM_E because some day I expect to have STREAM_F,
STREAM_G, etc ... and there are binary versions of this data structure
(and it's previous representations) in customer's files for the last 15 years.

This code was originally C code written in the middle 1980s and was
"loosely" converted to C++ about 1995. Life is a legacy !


I would actually use macros instead of typedefs.

V
Jul 22 '05 #7

P: n/a
The following link describes the use of forward declarations:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Generate Sequence Diagrams in PDF and MS-Word

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.