I have a question regarding this concept in my C++ class. I just would like to know whether a "call-by-reference" is more commonly used than a "call-by-address"? I would also like to know which is the more efficient method of the two in C++ programming. I'm new to C++ and at the moment am having a hard time seeing why both are being presented at the same time as something we need to learn if they do the same thing.
A question for C++ people about calling.?
Yes call by reference is more commonly used in C++ and its more efficient method.
Reference: Is an alias assinged to either a premitive variable or an inbuild datatype or an object of the type class.We can pass the object directly to the function.
It works the same way, i.e. you pass an `int' by value, but there are no references in C. So you must pass a pointer to an int, in order to have call by reference.
Reply:A call by reference means that it is an indirect call. That is, the code that is generated will get an address to the function from a variable, and then the function calls the address that is in the variable. Object Oriented Programming objects and COM objects work this way. This is one of the reasons why I don't like OOP and COM objects.
A direct call (call by address) simply calls a function directly. In other words, the address is hard coded in memory and there is no need to first look up the function's address from a variable. Direct calls are more efficient because they result in less code and you don't have to access memory in order to get the function's address. (In code optimization, memory accesses should be kept to a minimum.)
Direct Call Pseudocode:
call hello()
or rather:
call %26lt;memory address for hello() function%26gt;
Indirect Call Pseudocode:
addvariable=%26lt;address to hello() function%26gt;
get %26lt;addvariable%26gt; from memory
call %26lt;addvariable%26gt;
Reply:In some languages, they both mean one and the same, the other part is call by value.
In C++, depending on the context and design, it is decided. if u r using overloading or call to member function with private / friends, then by address is used and if calling with data integrity (less cohesive) situations, call by value is used.
Reply:The are typically about as efficient. Call-by-reference lets you use dot notation, and call-by-address uses -%26gt; notation. Both let you change the value of the parameter (unless you use const).
Reply:Yeah, they do the same thing.
Call-by-reference is supposed to be "cleaner" somewhat. In languages like Java that's the only way available. In C++, however, they left call-by-pointer for backward compatibility with C.
So, go with call-by-reference. As far as which is more efficient - I haven't tested it frankly - but i'd be surprised if there's any difference at all, since after all, the two methods indeed do the same thing!
flowers uk
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment