Content deleted Content added
m →Syntax: change width |
→Syntax: merge tables |
||
Line 112:
|-
| <syntaxhighlight lang="cpp">
class Foo {
int x = 0;
};</syntaxhighlight>
| <syntaxhighlight lang="java">
class Foo {
private int x;
public Foo() {
}
public int bar(int i) { // Member method bar()
Line 245:
// outputs 5, because d references the
// same object as c</syntaxhighlight>
* In C++, it is possible to declare a pointer or reference to a [[const]] object in order to prevent client code from modifying it. Functions and methods can also guarantee that they will not modify the object pointed to by a pointer by using the "const" keyword. This enforces [[const-correctness]].▼
* In Java, the <code>final</code> keyword is similar to the <code>const</code> keyword in C++, but its usage is more limited.{{sfn|Goetz|Peierls|Bloch|Bowbeer|2006|loc=§3.4.1 Final fields|p=48}} For the most part, const-correctness must rely on the semantics of the class' interface, i.e., it is not strongly enforced, except for public data members that are labeled <code>final</code>.▼
|-
▲
const Foo* a; // it is not possible to modify the object
// pointed to by a through a
</syntaxhighlight>
|<syntaxhighlight lang="java">▼
▲
final Foo a; // a declaration of a "final" reference:
// it is possible to modify the object,
|