Question

How to determine type equivalence in C++. Explain it in several sentences with a simple example.

How to determine type equivalence in C++. Explain it in several sentences with a simple example.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Type Equality

The meaning of basic operations such as assignment (denoted by = in C) is specified in a language definition. Thus, for example, the meaning of statements such as

x = y;

here the value of object y is copied into the memory locations for variable x.

However, before an operation such as an assignment can be accepted by the translator, usually the types of the two operands must be the same (or perhaps compatible in some other specified way).

Thus a language translator must decide whether two types are equal in some cases. We now consider what it means to say that two types are "equal" (or equivalent).

There are two standard ways to determine whether two types are considered the same: name equivalence and structural equivalence.

Name equivalence is the most straightforward: two types are equal if, and only if, they have the same name. Thus, for example, in the code (using C/C++ syntax)

   typedef struct {
           int data[100];
           int count;
           } Stack;

   typedef struct {
           int data[100];
           int count;
           } Set;

   Stack x, y;
   Set r, s;

if name equivalence is used in the language then x and y would be of the same type and r and s would be of the same type, but the type of x or y would not be equivalent to the type of r or s. This means that statements such as

   x = y;
   r = s;

would be valid, but statements such as

   x = r;

would not be valid (i.e., would not be accepted by a translator).

Using structural equivalence:, two types are equal if, and only if, they have the same "structure", which can be interpreted in different ways.
A strict interpretation would be that the names and types of each component of the two types must be the same and must be listed in the same order in the type definition.
A less stringent requirement would be that the component types must be the same and in the same order in the two types, but the names of the components could be different.

Again looking at the example above, using structural equivalence the two types Stack and Set would be considered equivalent, which means that a translator would accept statements such as

x = r;
Add a comment
Know the answer?
Add Answer to:
How to determine type equivalence in C++. Explain it in several sentences with a simple example.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT