answersLogoWhite

0


Best Answer

A and B can be any type, even different types, so long as one can be statically cast to the same type as the other. Thus A can be the same class as B, or A can be a base class of B. But if B is not derived from A, then B must implement a static cast to type A. If such a cast would make no logical sense, then A=B makes no sense either, and would therefore be an illegal assignment.

Illegal assignments are not a bad thing as it actively prevents people from doing silly things. Twisting the logic in order to make an assignment legal won't necessarily make the assignment logical. You might not be able to stop them, but you can at least hinder them.

Imagine if A were Apples and B were Bananas. Does the assignment Apples=Bananas make any sense? On the surface, you would haver to say no. But if they were both derived from a common base class, say Fruit, then the assignment Apples=Bananas could theoretically be statically downcast to Apples::Fruit=Bananas::Fruit, in which case it has the potential to be a legal assignment. That is, only the Fruit portion of A would be assigned the Fruit portion of B. However, the logic behind Apples=Bananas is somewhat ambiguous. If the end-user really wanted to perform this type of assignment then the end-user should explicitly cast the types themselves, rather than hide the implementation behind an implicit Apples=Bananas assignment.

As a rule of thumb, overloading an assignment operator should always produce a predictable and logical result. If the logic of an assignment is unclear even to to you, then imagine how confusing it would be to the end-user, or to someone else simply reading your code (bafflement and bewilderment spring to mind). So if there's ever any ambiguity, it's best avoided. You can't stop people trying to do illogical things with assignments, but actively encouraging them by including illogical operators yourself says more about you than it does about them. If in doubt, leave it out. Don't make it too easy.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What types of A and B is the simple assignment statement A equals B legal in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is statement of ownership?

Statement of ownership is a sworn statement made by a person affirming the legal ownership of real property. It is a legal document where a person swears that they do own something.


Is there an error in let x plus 3 5 equals in BASIC?

If your statement is "let x+3 5 =" it sn't legal BASIC. Neither is "let x+3 = 5" - which may be what you meant. However: "let x = 5 - 3" - is legal.


What is a legal staement?

A legal statement: a brand is the exclusive property of a company. The company own the


Break statement in while loop?

Perfectly legal.


Is it legal to photo copy paper money with the statement legal tender on it?

No sir do not do that it can land you in jail


Is jargon simple legal English?

Jargon refers to specialized language or terminology used within a particular profession or field. Legal English, on the other hand, refers to the language and terminology used specifically in the legal profession. While legal English can include jargon, not all jargon is necessarily considered legal English as it can be present in various other fields or industries as well.


What are the key legal documents every startup should have?

Employment Agreement Ip Assignment Shareholders Agreement


A legal statement of how an individual property is to be distributed after death is?

A living will


What term is for the introductory statement of the purpose of a legal document?

preamble


What is the term for the introductory statement of the purpose of a legal documents?

preamble


What is the term for introductory statement of the purpose of a legal document?

preamble


What is the term for the introductory statement of a purpose of legal document?

preamble