Understanding Key C++ Features for C Programmers
Boolean Type
A boolean type is used for logical evaluations, such as condition checks, flow control, and logical operations.
Key Points
boolis a data type that can hold one of two values:trueorfalse.- Example definition:
bool isActive = true; - It occupies 1 byte of memory.
- Boolean variables can be assigned numeric values, following the rule that zero is
falseand any non-zero value istrue.
Inline Functions
Inline functions optimize performance by reducing function call overhead through code expansion at the call site.
Overview
- Inline functions avoid the jump to a separate function body and the associated stack operations, trading space for time.
- They are defined by prefixing the functon with the
inlinekeyword.
Characteristics
- Inline functions behave like regular functions in terms of calling syntax.
- They do not involve stack push/pop operations or jumps to a function body.
- For functions with extensive code, the performance gain from inlining may be negligible.
Usage Guidelines
- Avoid inlining functions with long bodies, loops, or recursion.
- Prefer inlining for simple assignment or return statements that are called frequently.
Comparison with Macros
Inline functions differ from parameterized macros in several ways:
- Inline functions are actual functions executed at runtime, without stack overhead.
- Macros are processed during preprocessing and involve simple text substitution.
- Inline functions have parameter types, return types, and return values, whereas macros lack these features.
References
References provide an alias for an existing variable or object, distinct from typedef, which aliases types.
Definition Example
int value = 0;
int &alias = value;
alias = 1;
In this example, alias is a reference to value. Modifying alias changes value, so value becomes 1.
Usage
- References do not allocate additional memory.
- They can be used with structures:
struct Point {
int x;
int y;
};
Point p;
Point &ref = p;
ref.x = 1; // Equivalent to p.x = 1;
- As function parameters, references allow modification of arguments, similar to pointers:
void modifyValues(int &a, int &b) {
a = 1;
b = 1;
}
- Avoid returning references to local variables, as they become invalid after function exit.
Notes
- References must be initialized upon definition.
- The
&symbol denotes a reference on the left side of an assignment and an address operator on the right. - References do not create new variables or allocate memory.
- The type of a reference must match the type of the variable it aliases.
Comparison with Pointers
- References are aliases without separate memory allocation; pointers are entities that store addresses.
- References must be initialized and cannot be reassigned; pointers can be uninitialized or reassigned.
- There are multi-level pointers but no multi-level references.
- References allow direct access, while pointers require dereferencing for indirect access.
Function Overloading
Function overloading enables multiple functoins with the same name but different parameter lists within the same scope.
Overloading Conditions
Overloading occurs when functions differ in:
- Number of parameters.
- Types of parameters at corresponding positions.
Ambiguity and Resolution
Ambiguity can arise when calls match multiple overloads due to implicit conversions. For example:
int process(int val) {}
int process(float val) {}
process(2.3); // Ambiguous: double can convert to int or float
Solutions:
- Add an overload for the specific type:
int process(double val) {}. - Use explicit casting in the call:
process((int)2.3);.
Default Parameters
Default parameters allow functions to be called with fewer arguments by providing predefined values.
Concept
Default values are specified in function definitions or declarations, and used when arguments are omitted.
Definition Example
void display(int a, int b = 2) {}
// Function calls:
display(1); // Uses default b = 2
display(1, 3); // Overrides default with b = 3
Guidelines
- Default parameters must be specified from right to left.
- Default values must be constants, not variables.
- If a function has both declaration and definition, defaults should be in the declaration.
Interaction with Overloading
Combining default parameters and overloading can cause ambiguity:
int sum(int a, int b, int c = 0);
int sum(int a, int b);
sum(1, 2); // Ambiguous: matches both functions
Resolution: Avoid using default parameters with overloaded functions that could lead to conflicts.