What is the use of declarators in struct's definition?
and what is a "tag declarator"?

example from MSDN:

[template-spec] struct [ms-decl-spec] [tag [: base-list ]]
{ 
      member-list 
} [declarators];
[struct] tag declarators;

Recommended Answers

All 8 Replies

It's a tag followed by one or more declarators.

A tag, in turn, is the identifier that follows "struct," "class," or "union."

Thanks for your reply.

What is the use of declarator (D3DVECTOR) in the following struct definition?

typedef struct _D3DVECTOR {
    float x;
    float y;
    float z;
} D3DVECTOR;

Why it is not defined like this:

typedef struct D3DVECTOR {
    float x;
    float y;
    float z;
};

Perhaps because the author wants both C and C++ programmers to be able to use the same code, and to be able to write

D3DVECTOR foo;

with the same meaning in both languages.

Thanks again.

So with this:

typedef struct D3DVECTOR {
    float x;
    float y;
    float z;
};

there will be a problem in C?

Yes. In fact, I'm not sure that this declaration is valid at all, because it doesn't declare any variables. The general form of a typedef declaration is

typedef <type> <declarator-list>;

and in this case, the type is

struct D3DVEFCTOR { float x; float y; float z; }

and the declarator-list is empty.

If you omit the "typedef," the declaration is valid in C, but then to use it you would have to write

struct D3DVECTOR foo;

and you sould not need the "struct" in C++.

thanks a lot :)

That will most likely be a problem in both languages. The typedef isn't complete.

A typedef is similar to an "alias" in other languages. It is an alternative name for the dataType.

Take a vector of integers for example:

std::vector<int> myVec;

If you want to create an iterator for this vector, you must enter

std::vector<int>::iterator myIntIter;

Now imagine having to enter that a couple hundred times. Is there a way to cut down on the amount of typing and reduce the chances for an error? Yes, use a typedef:

typedef std::vector<int>::iterator vIntIter;

vIntIter myIntIter;  //declare an iterator for a vector of ints

Thank you Fbody for your explanation :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.