Does C have references? Ask Question. Asked 10 years, 11 months ago. Active 9 years, 8 months ago. Viewed 31k times. Kevin Reid Add a comment. Active Oldest Votes. No, it doesn't.
It has pointers , but they're not quite the same thing. Community Bot 1 1 1 silver badge. Jon Skeet Jon Skeet 1. Martin: It won't be for long ; — Jon Skeet. Martin, had the exact same thought — JaredPar. I know there is good in you. The Emperor hasn't driven it from you fully.
That's why you won't bring me to your Emperor now. Conceptually, C has references, since pointers reference other objects. Most of the time we add more layers of indirection to manage situations like these. Or whatever.
Why would you want to change an argument? Well, often we have to, but often we have an alternative: produce a new value. For a reader, incr2 is likely easier to understand. That is, incr1 is more likely to lead to mistakes and errors. What if you do want to change the argument, should you use a pointer or use a reference? One style is to use a pointer when you want to modify an object because in some contexts that makes it easier to spot that a modification is possible.
Also, Stroustrup chose this to follow Simula usage, rather than the later Smalltalk use of self. References What is a reference? An alias an alternate name for an object. What happens if you assign to a reference? You change the state of the referent the referent is the object to which the reference refers. What happens if you return a reference? The function call can appear on the left hand side of an assignment operator.
It chains these method calls, which is why this is called method chaining. How can you reseat a reference to make it refer to a different object? No way. Use references when you can, and pointers when you have to. What does it mean that a reference must refer to an object, not a dereferenced null pointer? Is it a pointer? Exercise: Predict the output of the following programs. If there are compilation errors, then fix them. Skip to content. Change Language.
Related Articles. Object Oriented Programming. Exception Handling. File Handling. Table of Contents. Save Article. Improve Article. Like Article. Previous Opaque Pointer.
0コメント