I was reading C ++ FAQ - "" and in particular in this statement:
Use when you can, and when you get the prompt then reference.
...
The above exception, where the parameter or return value of the function requires "Sentinel" reference? ??? A reference that does not refer to an object is usually best done / takes an indicator by returning, and this gives special significance to the tap pointer (reference should always be nickname, no dereferenced null pointer).
What I have seen, the requirement of a "watchdog" reference is often the reason to use the indicator rather than references. What am I thinking: Why is not there a special "zero value" for reference in C ++? It seems that this indicator will be almost unnecessary, which will solve many problems.
So why was not part of the language specification?
Edit:
I'm not sure my question is clear - I think I'm not saying politely about the humble references Often I have read that there is "context object" in C ++ and, in most OOP languages, objects can be Null-Pascal, C #, Java, Javascript, PHP etc. In all of these you can do some object:
someObject = null or . In fact, Pascal also supports the indicator but still does not allow objects to be
zero , because its use uses it, so why C ++ is somehow special and not a tap object is? Was it just an undiscovered or a real decision?
Because it carries a reference meaning, which indicates that a valid memory address Does not change; That is, it is dereferencing / secure and therefore no zero check is required. The reference can not be reconsidered by the design.
When you can tap the version and the client code is to handle that matter, then you can use a pointer. When you can guarantee a valid / initial memory address, you can use a reference.
An example of using pointers is an example to store "context" for an example that can not be known or enabled, although initially to be in the form of class, however, Members should create reference time (through initial lists) and their assignments can not be postponed.
If you allow a blank reference, then it is not different from one pointer other than syntax (same tap check will need to be replaced.)
Updates :
"And, in most OOP languages, objects can be taped - Pascal, C #Java, Javascript, PHP, etc. [...] So why C + + Especially special and does not have a tap object in it? Was it just an undiscovered or a real decision? " < / Blockquote>
I think you are a bit confused about this. Java and C # etc "NULL Objects" , but these object references are more or less like throwing simple syntax, GC instrumentation, and exceptions like C ++ pointer. In those languages, if you work on "Null object" then you will get an exception like NullReferenceException (C #). In Hell, Java it is called a NullPointerException
Before you can use them safely, you have to check for the
null Like ++ pointers (except for most managed languages, pointers are initialized by default to tap), while in C ++ it is usually to be determined to determine the initial pointer value (otherwise undefined) whichever is earlier Was present). / P>
C ++ likes about the scene, and therefore verbose is happening:
- How do you please, use a plain indicator to do this, < / Li>
- Use references that have a compiler-applied validity meaning / constraint.
- Roll your own Smart Pointers, which you want to book and the way You want, behave accordingly.
- If unnecessary of memory, if necessary Using a zero signal (carefully!) To refer to the block. / Li>
Comments
Post a Comment