Re: How do java programmers cope with java missing c++ const?



VisionSet wrote:
"Jeffrey Schwab" <jeff@xxxxxxxxxxxxxxxx> wrote in message
news:pe0Nf.27304$915.8884@xxxxxxxxxxxxxxxxxxx
Vitaly wrote:
C++ const in java is final!!!
No, it's not.

final (on fields and locals) is like a subset of const. But then const is like a subset of what you would expect const to be like if you didn't know the peculiarities of C++ (notably if you like pimpls).

class Foo {

private final StringBuilder builder = new StringBuilder();

void methodToCallAnyTimeILike() {

builder.append("Not particularly constant is it really?");
}
}

(My C++ is a bit rusty.)

class ArrayList {
private:
int *const data;
public:
ArrayList(const int length) : data(new int[length]) {
}
~ArrayList() {
delete[] data;
}
void set(const int index, const int value) const {
data[index] = value;
}
int get(const int index) const {
return data[index];
}
};
class Foo {
private:
const ArrayList list;
public:
Foo() : list(1) {
}
void methodToCallAnyTimeILike() const {
list.set(0, "Not particularly constant is it really?" [0]);
}
};

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
.



Relevant Pages