While functions in C and C++ are fundamentally similar, C++ provides additional functionalities such as true pass-by-reference and default arguments.
C: Pass-by-reference is not directly possible in C. Instead, it is emulated using pointers.
void foo(int *param){ /* Passing pointer by value */
*param = 7; /* Dereference pointer to "emulate" reference and modify the value stored at param */
}
int Value = 3;
foo(&Value); // Pass memory address of Value
C++: C++ introduces a reference type, enabling true pass-by-reference.
void foo(int &Param){ // & passes a reference to Param instead of a copy
// True pass by reference
Param = 7;
}
int Value = 3;
foo(Value); // Directly pass Value
C++ allows functions to have default arguments, which simplifies function calls by providing default values for parameters.
// The default argument for param is 0 in function declaration
int foo(int param = 0);
// The default argument is not repeated in the function definition
int foo(int param){
return param + 3;
}
// Call location can provide the argument or not
int x = foo(6); // x will be 9
x = foo(); // x will be 3, equivalent to foo(0)
Structs in C++ are similar to those in C. However, C++ (especially from C++11 onwards) provides enhanced capabilities for structs.
C: Structs are defined and used with typedef.
struct NAME_TAG{
int DataMember;
};
// Required to just use Name
typedef struct NAME_TAG Name;
C++: Structs can be defined more succinctly.
struct Name{
int DataMember;
};
Both C and C++ allow instantiation and initialization of structs, but C++ provides additional features.
// C
/* Assume Name is defined as above */
Name MyName;
// C++
// Assume Name is defined as above
Name MyName;
// C
/* Assume Name is defined as above */
Name MyName = {4};
// C++
// Assume Name is defined as above
Name MyName = {4};
Designated initializers allow you to initialize specific members of a struct.
// C
Name MyName = {.DataMember = 4};
// C++ (C++20)
Name MyName = {.DataMember = 4};
C++ allows default initialization of struct members, simplifying the initialization process.
struct Name{
int DataMember = 4; // C++11
};
Name MyName; // DataMember will have value of 4
Both C and C++ allow functions to operate on structs, but C++ supports member functions directly within the struct definition.
/* typically in header file */
typedef struct NAME_TAG{
int DataMember;
} Name;
void Name_foo(Name *name, int param);
/* typically in c file */
void Name_foo(Name *name, int param){
name->DataMember += param;
}
/* typically in another c file */
Name MyName = {4};
Name_foo(&MyName, 7);
// Typically in header file
struct Name{
int DataMember;
void foo(int param); // Member function
};
// Typically in cpp file
void Name::foo(int param){
DataMember += param; // Python "self" is implied
// could also do this->DataMember = param;
}
// Typically in another cpp file
Name MyName = {4};
MyName.foo(7);
C++ supports static members within structs, allowing shared data across all instances of the struct.
C: Emulated through global variables.
/* Typically in header file */
extern int Name_DataMember;
/* Typically in c file */
int Name_DataMember = 4;
C++: True static members are supported.
// Typically in header file
struct Name{
static int DataMember;
};
// Typically in cpp file
int Name::DataMember = 4;
C: Emulated through normal functions.
/* Typically in header file */
extern int Name_DataMember;
void Name_foo(int param);
/* Typically in c file */
int Name_DataMember = 4;
void Name_foo(int param){
Name_DataMember += param;
}
C++: Supports static member functions.
// Typically in header file
struct Name{
static int DataMember;
static void foo(int param);
};
// Typically in cpp file
int Name::DataMember = 4;
void Name::foo(int param){
DataMember += param;
}