tags:

views:

117

answers:

5

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...

+1  A: 

This simply is the C++ way to initialize all the class members.

siukurnin
+9  A: 

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

Nim
A: 

They're default values.

So the C++ class gives you a hint as to how you'd write a default constructor.

The thing I don't understand is why there are more parameters after the ":" than there are member variables in your class. It suggests that there's a parent class that has to be initialized somewhere, but I don't see it.

duffymo
Yes, I know what that is. It will allow you to call the ctor with no arguments. You're just using a different name for the same concept.
duffymo
"Default value" is used for two concepts in C++. The default value of a type is the value of an object initialized with the default constructor (or `0` for builtins). The default value of a function parameter is a value specified in the function declaration: `void f(int a = 42); // 42 is the default value of int a`. The code shows an initializer list, not a default value.
MSalters
"initializer/default value" == "po-TAY-to/po-TAH-to". I understand the subtlety, but it's a bike shed argument in my opinion. I'll agree with you - call it an initializer list. I haven't written C++ in a while, so perhaps I'm not being rigorous enough.
duffymo
+2  A: 

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

kotlinski
Not so. But cipher=filename is as close as Java can get to what cipher(filename) does.
Kate Gregory
+4  A: 

Those are field initializer lists. They set the initial values for the fields.

The Java-Version is something like

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 this are not necessarily simple field setters, they may also be calls to the field type's constructor.

Thomas Lötzer