Convert C ++ Constructor to Java

I am working on translating a small package from C ++ to Java. I have never used C ++, so some of the syntaxes are a bit cryptic. In particular, it’s hard for me to understand what the Java equivalent is:

file: SomeClass.cpp SomeClass::SomeClass( BitStream* data, const char* const filename ) : data( data ), cipher( filename ), iv( new Botan::byte [cipher.BLOCK_SIZE] ), ivBitsSet( 0 ), keyMaterialRemaining( 0 ), keyMaterial( new Botan::byte [cipher.BLOCK_SIZE] ) {} 

I am satisfied (in Java):

 public SomeClass{ public SomeClass(InputStream data, String filename){ } } 

but I'm not sure what to do with the material after : in C ++. Are these fields? Extra options? Apologies for the trivial question, but far from gone with Google on this ...

+4
source share
4 answers

Everything after the ":" is called a member initialization list; in C ++, this is one of the ways to initialize members of this class. For example, from your code, “data” are members of SomeClass, so the Java equivalent will be a simple assignment in the constructor body.

 this.data = data; 

etc .. for all other members

+10
source

These are lists of field initializers . They set the initial field values.

Java version is similar to

 public SomeClass{ public SomeClass(InputStream data, String filename){ //either set the field directly... this.data = data; //...or call the constructor, depending on the type this.cipher = new Cipher(filename); } } 

Note that these are not necessarily simple field installers; they can also be calls to the field type constructor.

+4
source

cipher (filename) is equivalent to writing cipher = filename;

+2
source

This is just a C ++ way to initialize all members of a class.

+1
source

All Articles