Java的最终与C ++的const [英] Java's final vs. C++'s const

查看:122
本文介绍了Java的最终与C ++的const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java for C ++程序员教程说(highlight is my自己):


关键字final是大致
相当于C ++中的const


在这种情况下,粗略是什么意思?



有什么区别吗?

c> c> c> c> $ c>实例。 Java没有等价于此。例如:

  class Foo {
public:
void bar
void foo()const;
};

void test(const Foo& i){
i.foo(); // fine
i.bar(); // error
}






被分配,一次,稍后在Java只有例如:

  public class Foo {
void bar(){
final int a;
a = 10;
}
}

在Java中是合法的, / p>

  public class Foo {
void bar(){
final int a;
a = 10;
a = 11; //不合法,即使在Java中:a已经分配了一个值。
}
}






在Java和C ++中,成员变量可以分别是 final / const



在Java中,它们必须在构造函数完成之前设置,这可以是通过两种方式之一实现:

  public class Foo {
private final int a;
private final int b = 11;
public Foo(){
a = 10;
}
}



在C ++中,您需要使用初始化列表 const 会员a值:

  class Foo {
const int a;
public:
Foo():a(10){
//这里赋值为不合法
}
};






在Java中,final可以用来标记事物不可覆盖。 C ++(pre-C ++ 11)不这样做。例如:

  public class Bar {
public final void foo(){
}
}

public class Error extends Bar {
//错误在java中,不能覆盖
public void foo(){
}
}

但在C ++中:

  class Bar {
public:
virtual void foo()const {
}
};

类错误:public Bar {
public:
// Fine in C ++
virtual void foo()const {
}
} ;

这很好,因为标记成员函数的语义 const 是不同的。 (你也可以通过在一个成员函数上使用 const 来重载 (注意,C ++ 11允许成员函数标记为最后,请参阅C ++ 11更新部分)






C ++ 11更新:



C ++ 11实际上允许您将类和成员函数标记为 final ,与Java中的相同特征具有相同的语义,例如在Java中:

  public class Bar {
public final void foo(){
}
}

public class Error extends Bar {
// java中的错误,不能覆盖
public void foo(){
}
}

现在可以用C ++ 11完全写成:

  class Bar {
public:
virtual void foo()final;
};

class Error:public Bar {
public:
virtual void foo()final;
};

我不得不编译这个例子与预发布的G ++ 4.7。请注意,在这种情况下,这不会替换 const ,而是增加它,提供类似Java的行为,没有看到与最接近的等价C ++关键字。所以如果你想成员函数 final const ,你会这样做:

  class Bar {
public:
virtual void foo()const final;
};

const 和<$



以前没有直接对应 const 成员函数虽然使函数非 - virtual 将是一个潜在的选项,尽管在编译时不会导致错误。



同样的Java:

  public final class Bar {
}

public class Error扩展Bar {
}

p>

  class Bar final {
};

类错误:public Bar {
};

(以前 private



有趣的是,为了保持与前C ++ 11代码的向后兼容性 final 不是通常的关键字。 (以琐碎,合法的C ++ 98示例 struct final; 来了解为什么使它成为关键字会破坏代码)


The Java for C++ programmers tutorial says that (highlight is my own):

The keyword final is roughly equivalent to const in C++

What does "roughly" mean in this context? Aren't they exactly the same?

What are the differences, if any?

解决方案

In C++ marking a member function const means it may be called on const instances. 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 only 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, even in Java: a has already been assigned a value.
   }
}


In both Java and C++ member variables may be final/const respectively. 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 const members 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 const are different. (You could also overload by only having the const on 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 as final, 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 const in 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 final and const you would do:

class Bar {
public:
  virtual void foo() const final;
};

(The order of const and final here is required).

Previously there wasn't a direct equivalent of const member functions although making functions non-virtual would 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 private constructors was probably the closest you could get to this in C++)

Interestingly, in order to maintain backwards compatibility with pre-C++11 code final isn'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)

这篇关于Java的最终与C ++的const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆