From 67b6fd11cb47caa9860ff3c4d3bf1f800471fe31 Mon Sep 17 00:00:00 2001 From: bronifty Date: Thu, 26 Sep 2024 23:56:01 -0400 Subject: [PATCH] this --- docs/courses/cmu-db-oltp/cpp.mdx | 111 +++++++++++++++++++++++++++++++ docs/opinion/codes/llvm.mdx | 4 +- 2 files changed, 112 insertions(+), 3 deletions(-) diff --git a/docs/courses/cmu-db-oltp/cpp.mdx b/docs/courses/cmu-db-oltp/cpp.mdx index 9df2bf7..d304b64 100644 --- a/docs/courses/cmu-db-oltp/cpp.mdx +++ b/docs/courses/cmu-db-oltp/cpp.mdx @@ -2,3 +2,114 @@ [cpp primer](https://15445.courses.cs.cmu.edu/fall2024/project0/) +## move constructors + +pass by reference is passing in an object from the caller to the callee as a function argument to do as the callee wishes, effectively temporarily handing control (without moving the memory location) + +returning a reference is handing control of the callee's own variable to the caller without moving the memory location + +```cpp +#include +#include + +class Person { +public: + Person(std::string name, int age) : name_(std::move(name)), age_(age) {} + + // Function that takes a reference parameter + void incrementAge(int& yearsToAdd) { + age_ += yearsToAdd; + yearsToAdd = 0; // This change will be visible to the caller + } + + // Function that returns a reference + std::string& getName() { + return name_; // Returns a reference to name_ + } + + void printDetails() const { + std::cout << "Name: " << name_ << ", Age: " << age_ << std::endl; + } + +private: + std::string name_; + int age_; +}; + +int main() { + Person person("Alice", 30); + + // Demonstrating pass by reference + int years = 5; + std::cout << "Before incrementAge: years = " << years << std::endl; + person.incrementAge(years); + std::cout << "After incrementAge: years = " << years << std::endl; + person.printDetails(); + + // Demonstrating returning a reference + std::cout << "\nBefore modifying name: "; + person.printDetails(); + + person.getName() = "Alicia"; // Directly modifies the name_ member + + std::cout << "After modifying name: "; + person.printDetails(); + + return 0; +} + +``` + +## Reference v Pointer + +a reference has the same memory address as the object it references; it is an alias. + +a pointer has a different memory address and its value is the memory address of another object of the same type + +```cpp +#include +#include + +class Person { +public: + Person(std::string name, int age) : name_(std::move(name)), age_(age) {} + + Person& returnReference() { + std::cout << "Returning *this (reference)\n"; + return *this; + } + + Person* returnPointer() { + std::cout << "Returning this (pointer)\n"; + return this; + } + + void printDetails() const { + std::cout << "Name: " << name_ << ", Age: " << age_ << std::endl; + } + +private: + std::string name_; + int age_; +}; + +int main() { + Person alice("Alice", 30); + + std::cout << "Original Alice:\n"; + alice.printDetails(); + + std::cout << "\nUsing returnReference():\n"; + Person& aliceRef = alice.returnReference(); + std::cout << "Address of alice: " << &alice << std::endl; + std::cout << "Address of aliceRef: " << &aliceRef << std::endl; + + std::cout << "\nUsing returnPointer():\n"; + Person* alicePtr = alice.returnPointer(); + std::cout << "Address of alice: " << &alice << std::endl; + std::cout << "Value of alicePtr (address it points to): " << alicePtr << std::endl; + std::cout << "Address of alicePtr itself: " << &alicePtr << std::endl; + + return 0; +} +``` \ No newline at end of file diff --git a/docs/opinion/codes/llvm.mdx b/docs/opinion/codes/llvm.mdx index 2108dbd..3c80310 100644 --- a/docs/opinion/codes/llvm.mdx +++ b/docs/opinion/codes/llvm.mdx @@ -2,9 +2,7 @@ - a conversation with open ai llm: 01 mini -Q: rust (via rustc) zig and c (via clang) hit llvm apis to produce ir, which llvm compiles to machine code. theoretically, any language compiling to llvm ir can link its binary with any other one. - -llvm should compile not only to machine code but also wasm. it could be one of the modules. +Q: rust (via rustc) zig and c (via clang) hit llvm apis to produce ir, which llvm compiles to machine code. theoretically, any language compiling to llvm ir can link its binary with any other one. llvm should compile not only to machine code but also wasm. it could be one of the modules. A: it does, but through the compiler interface of each language for instance with rust