0

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;
3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by hystaspes
Featured Replies
  • 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. Read More

  • 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 … Read More

  • 1

    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:[CODE]std::vector<int> myVec;[/CODE] If you want to create an iterator for this vector, … Read More

0

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

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

0

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;
};
1

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.

0

Thanks again.

So with this:

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

there will be a problem in C?

1

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++.

1

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

Edited by Fbody: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.