Skip to content

Instantly share code, notes, and snippets.

View njlr's full-sized avatar
🌏
F# ing

njlr njlr

🌏
F# ing
View GitHub Profile
x->what() = A
y->what() = B
z->what() = B
int main() {
A* x = new A();
  B* y = new B();
  A* z = y;
 
  std::cout << "x->what() = " << x->what() << std::endl;
  std::cout << "y->what() = " << y->what() << std::endl;
  std::cout << "z->what() = " << z->what() << std::endl;
 
x.what() = A
y.what() = B
z.what() = A
@njlr
njlr / xyz.cpp
Created September 7, 2017 10:43
int main() {
A x;
  B y;
  A z = y;
 
  std::cout << "x.what() = " << x.what() << std::endl;
  std::cout << "y.what() = " << y.what() << std::endl;
  std::cout << "z.what() = " << z.what() << std::endl;
 
@njlr
njlr / ab.cpp
Created September 7, 2017 10:42
struct A {
 virtual std::string what() {
  return "A";
  }
};
struct B : public A {
 std::string what() override {
  return "B";
  }
Person const person = { "John", "Smith" };
person.firstName = "Bob"; // Will not compile!
struct Person {
  std::string firstName; // No need for const here! 
  std::string lastName;
};
final Person person = new Person("John", "Smith");
person.firstName = "Bob"; // Will not compile!
public final class Person {
public final String firstName;
  public final String lastName;
 
  public Person(String firstName, String lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
  }
}
final List<String> xs = new ArrayList<>();
// We can modify xs internally
xs.add("a");
xs.add("b");
xs.add("c");
// ... but we cannot change where it points to!
xs = new ArrayList<>(); // Will not compile