const(C++) vs final(java)
2012-02-06 17:49
399 查看
In C++ marking a member function
Values can be assigned, once, later in Java, e.g.:
is legal in Java, but not C++ whereas:
In both Java and C++ member variables may be
In Java they must be set before the constructor has finished, this can be achieved in one of two ways:
In C++ you will need to use initialisation lists to give
In Java final can be used to mark things as non-overridable. C++ (pre-C++11) does not do this. E.g.:
But in C++:
this is fine, because the semantics of marking a member function
overload by only having the
Can now be exactly written in C++11 as:
I had to compile this example with a pre-release of G++ 4.7. Note that this does not replace
(The order of
Previously there wasn't a direct equivalent of
Likewise the Java:
becomes in C++11:
(Previously
Interestingly, in order to maintain backwards compatibility with pre-C++11 code
http://stackoverflow.com/questions/4971286/javas-final-vs-cs-const
constmeans it may be called on
constinstances. Java does not have an equivalent to this. E.g.:
class Foo { public: void bar(); void foo() const; }; void test(const Foo& i) { i.foo(); //fine i.bar(); //error }
Values can be assigned, once, later in Java, e.g.:
public class Foo { void bar() { final int a; a = 10; } }
is legal in Java, but not C++ whereas:
public class Foo { void bar() { final int a; a = 10; a = 11; // Not legal, a has already been assigned a value. } }
In both Java and C++ member variables may be
final/
constrespectively. These need to be given a value by the time an instance of the class is finished being constructed.
In Java they must be set before the constructor has finished, this can be achieved in one of two ways:
public class Foo { private final int a; private final int b = 11; public Foo() { a = 10; } }
In C++ you will need to use initialisation lists to give
constmembers a value:
class Foo {const int a;
public:
Foo() : a(10) {
// Assignment here with = would not be legal
}
};
In Java final can be used to mark things as non-overridable. C++ (pre-C++11) does not do this. E.g.:
public class Bar { public final void foo() { } } public class Error extends Bar { // Error in java, can't override public void foo() { } }
But in C++:
class Bar { public: virtual void foo() const { } }; class Error: public Bar { public: // Fine in C++ virtual void foo() const { } };
this is fine, because the semantics of marking a member function
constare different. (You could also
overload by only having the
conston one of the member functions. (Note also that C++11 allows member functions to be marked final, see the C++11 update section)
C++11 update:
C++11 does in fact allow you to mark both classes and member functions asfinal, with identical semantics to the same feature in Java, for example in Java:
public class Bar { public final void foo() { } } public class Error extends Bar { // Error in java, can't override public void foo() { } }
Can now be exactly written in C++11 as:
class Bar { public: virtual void foo() final; }; class Error : public Bar { public: virtual void foo() final; };
I had to compile this example with a pre-release of G++ 4.7. Note that this does not replace
constin this case, but rather augments it, providing the Java-like behaviour that wasn't seen with the closest equivalent C++ keyword. So if you wanted a member function to be both
finaland
constyou would do:
class Bar { public: virtual void foo() const final; };
(The order of
constand
finalhere is required).
Previously there wasn't a direct equivalent of
constmember functions although making functions non-
virtualwould be a potential option albeit without causing an error at compile time.
Likewise the Java:
public final class Bar { } public class Error extends Bar { }
becomes in C++11:
class Bar final { }; class Error : public Bar { };
(Previously
privateconstructors was probably the closest you could get to this in C++)
Interestingly, in order to maintain backwards compatibility with pre-C++11 code
finalisn't a keyword in the usual way. (Take the trivial, legal C++98 example
struct final;to see why making it a keyword would break code)
http://stackoverflow.com/questions/4971286/javas-final-vs-cs-const
相关文章推荐
- 探究Java和C++中的关键字:final vs. const
- 【Java】Final 与 C++ Const的区别
- Java中的Final 与 C++中的const
- java的修饰引用变量的final和C++的const区别真的很大
- common and different of final in java and const in C++
- java中的final与C++中的const的区别
- 【转】java中的final与C++中的const的区别
- Java中final 与C++中Const的区别
- 【Java】Final 与 C++ Const的差异
- JavaFinal 与 C++ Const的区别 .
- java中的final与C++中的const的区别
- POJ 3157 Java vs C++ 可能会
- C++程序员学Java系列之二一:final 关键字
- Final 与 C++ Const的区别
- C++ Special:const关键字和常量指针 vs 指针常量
- Java vs C++ 之多态
- java vs c++
- Python vs. Perl vs. Java vs. C++ Runtimes
- C++ Special:const关键字和常量指针 vs 指针常量
- Java vs C++