0% found this document useful (0 votes)
45 views

CS205-2020 Spring - Lecture 7 PDF

C/C++ Programming covers topics like reference variables, function overloading, function templates, and inline functions. Reference variables allow a variable name to act as an alias for another variable, and are commonly used to pass large structures as function parameters by reference instead of value. Function overloading allows multiple functions to share the same name if they have different signatures. Function templates define generic functions in terms of a placeholder type that can be substituted for specific types. Inline functions may be inserted directly into the code instead of called, improving performance but using more memory.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

CS205-2020 Spring - Lecture 7 PDF

C/C++ Programming covers topics like reference variables, function overloading, function templates, and inline functions. Reference variables allow a variable name to act as an alias for another variable, and are commonly used to pass large structures as function parameters by reference instead of value. Function overloading allows multiple functions to share the same name if they have different signatures. Function templates define generic functions in terms of a placeholder type that can be substituted for specific types. Inline functions may be inserted directly into the code instead of called, improving performance but using more memory.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

C/C++ Programming

Language
CS205 Spring
Feng Zheng
2020.04.02
Content
• Brief Review
• Reference Variable
• Function Overloading
• Function Template
• Summary
Brief Review
Content of Last Class
• Various function applications
Ø Arrays
Ø C-style strings
Ø Structure
Ø String class and array objects
Ø Recursion
Ø Pointer to functions
Adventures in Functions
Mechanism of Function Call
• Codes are the data as well
Ø The product of the compilation process is an executable program
ü Consist of a set of machine language instructions
Ø The operating system loads these instructions into the memory
ü Each instruction has a particular memory address
ü Jump backward or forward to a particular address (loop or branching)
• Normal function: Jump forth and back
Ø Store the memory address of the instruction immediately following the
function call
Ø Copy function arguments to the stack
Ø Jump to the memory location that marks the beginning of the function
Ø Execute the function code
Ø Jump back to the instruction whose address it saved
C++ Inline Functions
• Compiler replaces the function call with
the corresponding function code
Ø Run a little faster than regular functions
Ø Come with a memory penalty
Ø Be selective about using inline functions
• Two steps
Ø Preface the declaration with the
keyword inline.
Ø Preface the function definition with the
keyword inline
• Inline versus macros
Ø Macros don’t pass by value
Reference Variables
• A new compound type to the language—the reference variable
Ø A reference is a name that acts as an alias

Ø An alternative name, for a previously defined variable

• Use a reference as an argument


Ø The function works with the original data instead of with a copy

Ø A alternative to pointers for processing large structures


Creating a Reference Variable
• The & symbol
Ø Indicate the address of a variable

Ø Declare references
ü int & means reference-to int
ü The reference declaration allows you to use two variables interchangeably
ü Both refer to the same value and the same memory location

Ø It is necessary to initialize the reference when you declare it

• Run program example 1


References as Function Parameters
• Passing by reference
allows a called function to
access variables in the
calling function

• Run program example 2


Temporary Variables, Reference
Arguments, and const
• The compiler generates a temporary variable
Ø 1: When the actual argument is the correct type but isn’t an lvalue
ü An lvalue is a data object that can be referenced by address including variable, array element, structure
member, reference, dereferenced pointer
ü Non-lvalues include literal constants and expressions with multiple terms
Ø 2: When the actual argument is of the wrong type, but it’s of a type that can be converted
to the correct type
Ø The reason to use: temporary variables cause no harm

• A function with reference arguments is to modify variables


• Use const when you can (non-modifiable lvalue)
Ø Protect you against programming errors that inadvertently alter data
Ø Process both const and non-const actual arguments when omits const in the prototype
Ø Generate and use a temporary variable appropriately
Using References with a Structure
• References were introduced primarily for use with C++’s user-defined
types, not for use with the basic built-in types
• Run program example 3
• Why return a reference?
Ø Normal return value: involve copying the entire structure to a
temporary location and then copying that copy
Ø But with a reference return value, the returned value is copied directly
to the variable in calling function, a more efficient approach
• Being careful about what a return reference refers to
Ø Remember when returning a reference is to avoid returning a reference
to a memory location that ceases to exist when the function terminates
Using References with a Class
Object
• Run program example 4
Ø string class defines a char *-to-string conversion

Ø A property of const reference formal parameters is that the


original data cannot be modified from inside the function

Ø A const string & parameter can handle a string object or a quoted


string literal, a null-terminated array of char, or a pointer
variable that points to a char (a char * or const char *)
When to Use Reference Arguments
• Two main reasons for using reference arguments
Ø To allow you to alter a data object in the calling function
Ø To speed up a program by passing a reference instead of an entire data
object (const)
• A function modifies data in the calling function
Ø If the data object is a built-in data type, use a pointer (more clear)
Ø An array, use your only choice: a pointer
Ø A structure, use a reference or a pointer
Ø A class object, use a reference
A function uses passed data without
modifying it
• If the data object is small, such as a built-in data type or a small
structure, pass it by value
• If the data object is an array, use a pointer because that’s your
only choice. Make the pointer a pointer to const
• If the data object is a good-sized structure, use a const pointer
or a const reference to increase program efficiency
• If the data object is a class object, use a const reference.
Default Arguments
• How do you establish a default value?
Ø You must use the function prototype
• When you use a function with an argument list, you must add
defaults from right to left

• The actual arguments are assigned to the corresponding formal


arguments from left to right; you can’t skip over arguments
• Run program example 5
Function Overloading
• Function overloading:
Ø Let you use multiple functions sharing the same name
Ø Comparison: in default argument, it can call the same function by using
varying numbers of arguments
• C++ enables you to define two functions by the same name,
provided that the functions have different signatures
Ø Function signature: function’s argument list
Ø Signature can differ in the number of arguments or in the type of
arguments, or both
• Run program example 6
Function Templates
• Define a function in terms of a generic type
Ø A specific type, such as int or double, can be substituted
Ø The process is termed generic programming
Ø The keywords template and typename (class)

• Run program example 7


Ø Apply the same algorithm to a variety of types
More Templates
• Overloaded templates
Ø Solved problem: not all types would use the same algorithm in templates
Ø Non-template functions take precedence over template functions
Ø Need distinct function signatures (overloading)
Ø Run program example 8

• Template limitations
Ø It’s easy to write a template function that cannot handle certain types
Ø In some cases, require different codes but the arguments would be the
same
Specializations
• Explicit specializations
Ø If the compiler finds a specialized definition that exactly matches a function
call, it uses that definition without looking for templates.
• Third-Generation Specialization
Ø A function name can have a non template function, a template function, and an
explicit specialization template function, along with all overloaded versions
Ø The prototype and definition for an explicit specialization should be preceded
by template <> and should mention the specialized type by name
Ø A specialization overrides the regular template, and a non template function
overrides both
Ø Run program example 9
Instantiations and Specializations
• Instantiation
Ø Template: merely a plan for
generating a function definition
Ø Instantiation: use the template
to generate a function
definition
ü Implicit: the compiler deduces the
necessity for making the definition
ü Explicit: using the <> notation to
indicate the type and prefixing the
declaration with the keyword
template
Which Function Version Does the
Compiler Pick?
• Multiple functions of the same name
Ø Include: function overloading, function templates, and function
template overloading……
• Overload resolution
Ø Phase 1—Assemble a list of candidate functions
Ø Phase 2—From the candidate functions, assemble a list of feasible
functions
ü Correct number of arguments
ü An exact match for each type of actual argument to the type of the corresponding
formal argument
Ø Phase 3—Determine whether there is a best viable function
Exact Matches and Best Matches
• C++ allows some “trivial conversions” when
making an exact match
Ø If there’s just one, that function is chosen
Ø If more than one are tied, but only one is a non
template function, that non template function is chosen
Ø If more than one candidate are tied and all are
template functions, but one template is more
specialized than the rest, that one is chosen
• Error
Ø If there are two or more equally good non template
functions
Ø If there are two or more equally good template
functions, none of which is more specialized than the
rest
Ø If there are no matching calls, that is also an error
Summary
• Inline function
• Reference variables
• Functions of the same name
Ø Default arguments
Ø Function overloading
Ø Function template
Thanks

[email protected]

You might also like