C++ Flashcards
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is SFINAE (Substitution Failure Is Not An Error) in C++?
SFINAE is a C++ language feature that allows template specialization based on type traits or conditions without causing compilation failure.
template typename std::enable_if::value, void>::type process(T val) { std::cout << "Integral type "; } template typename std::enable_if::value, void>::type process(T val) { std::cout << "Non-integral type "; }
What is the difference between std::vector
and std::array
?
std::vector
is a dynamic array with size management, while std::array
is a fixed-size array.
std::vector vec = {1, 2, 3}; std::array arr = {1, 2, 3};
What is the Rule of Three in C++?
The Rule of Three states that if a class requires a custom destructor, copy constructor, or copy assignment operator, it likely requires all three.
class Example { int* data; public: Example(int val) : data(new int(val)) {} ~Example() { delete data; } Example(const Example& other) : data(new int(*other.data)) {} Example& operator=(const Example& other) { if (this == &other) return *this; *data = *other.data; return *this; } };
Explain the difference between new
and malloc
in C++.
new
initializes objects, calls constructors, and returns a typed pointer. malloc
only allocates memory and returns a void pointer.
int* p1 = new int(5); // calls constructor int* p2 = (int*)malloc(sizeof(int)); // does not call constructor
What is RAII (Resource Acquisition Is Initialization)?
RAII is a programming idiom where resources are acquired and released by objects. Resources like memory or file handles are tied to object lifetimes.
class File { std::ofstream file; public: File(const std::string& name) { file.open(name); } ~File() { file.close(); } };
What is the difference between shallow copy and deep copy in C++?
A shallow copy copies only the pointer, not the object itself, while a deep copy duplicates the actual data the pointer refers to.
class Shallow { int* data; public: Shallow(int val) : data(new int(val)) {} ~Shallow() { delete data; } Shallow(const Shallow& other) : data(other.data) {} // Shallow copy }; class Deep { int* data; public: Deep(int val) : data(new int(val)) {} ~Deep() { delete data; } Deep(const Deep& other) : data(new int(*other.data)) {} // Deep copy };
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid
What is the difference between a deep copy and a shallow copy in C++?
A:
- Shallow copy: Copies the memory addresses (pointers), leading to multiple objects pointing to the same memory location.
- Deep copy: Copies both the values and the memory locations, creating independent objects.
class MyClass { int* ptr; public: MyClass(int val) { ptr = new int(val); } // Shallow Copy Constructor MyClass(const MyClass& obj) { ptr = obj.ptr; } // Deep Copy Constructor MyClass deepCopy(const MyClass& obj) { ptr = new int(*obj.ptr); } };
What are virtual tables (vtable) and virtual table pointers (vptr) in C++?
A:
- vtable: A table of function pointers that helps resolve dynamic method dispatch in classes with virtual functions.
- vptr: A hidden pointer in each object of a class that points to the class’s vtable.
class Base { public: virtual void show() { std::cout << "Base\n"; } }; class Derived : public Base { public: void show() override { std::cout << "Derived\n"; } };
The vptr
is set at runtime to point to the vtable of the Base
or Derived
class depending on the type of the object.
Explain the memory layout of a C++ object.
A:
A typical C++ object layout includes:
- Non-static data members: Stored directly in the object.
- vptr (if applicable): Points to the virtual table for dynamic dispatch.
- Padding: May be added for alignment.
class A { int x; // 4 bytes char y; // 1 byte // Padding of 3 bytes for alignment };
How does dynamic_cast work internally?
A:dynamic_cast
performs a runtime type check (RTTI). It uses the object’s vptr to determine its most derived type and checks if the cast is valid. If the cast is illegal, it returns nullptr
for pointers or throws std::bad_cast
for references.
class Base { virtual void func() {} }; class Derived : public Base {}; Base* base = new Derived(); Derived* derived = dynamic_cast(base); // Valid