This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x->what() = A | |
y->what() = B | |
z->what() = B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x.what() = A | |
y.what() = B | |
z.what() = A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct A { | |
virtual std::string what() { | |
return "A"; | |
} | |
}; | |
struct B : public A { | |
std::string what() override { | |
return "B"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Person const person = { "John", "Smith" }; | |
person.firstName = "Bob"; // Will not compile! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person { | |
std::string firstName; // No need for const here! | |
std::string lastName; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Person person = new Person("John", "Smith"); | |
person.firstName = "Bob"; // Will not compile! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Person { | |
public final String firstName; | |
public final String lastName; | |
public Person(String firstName, String lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |