Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate C++ Constructor into Java

Tags:

java

c++

porting

I'm working on translating a small package from C++ to Java. I've never really used C++, so some of the syntax is a bit of a mystery. In particular, I'm having difficulty working out what the Java equivalent to this would be:

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'm happy with (in Java):

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

  }
}

but I'm not sure what to do with the stuff after the : in the C++. Are they fields? Optional parameters? Apologies for trivial question, but haven't got far with Google on this...

like image 203
fredley Avatar asked Dec 06 '22 01:12

fredley


1 Answers

Everything after ":" is called the member initialization list, in C++ this is one way of initialising the members of this class. For example from your code, "data" is a member of SomeClass, so the equivalent in Java would be a simple assignment in the body of the constructor.

this.data = data;

etc. for all the other members

like image 78
Nim Avatar answered Dec 08 '22 02:12

Nim