forward declarations of a data structure

L

Lynn

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
 
V

Victor Bazarov

Lynn said:
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
 
L

Lynn

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
 
L

Lynn

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
 
H

Howard

Lynn said:
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
 
L

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 */
};

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
 
V

Victor Bazarov

Lynn said:
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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top