Saturday, June 09, 2007

Casting in C++

Type casting in c++ can be done in two ways , using the traditional explicit ( ) cast operator or the new cast operators in c++ . The new operators in C++ are

  1. static_cast <>
  2. const_cast <>
  3. dynamic_cast <>
  4. reinterpret_cast

Implicit Type Conversion in C++

An implicit type conversion process in C++ , happens in two cases .

Case 1:
If there is a type T and another type F . An the type T has a constructor that takes in a Type F , in that case in expressions which involve the usage of Type T and an object of type F is passed . The type F is automatically converted to an object of type T . Lets see this is as example


class String {
char * string;
public:
String(const char * str = "");
};

void print(String & str);
Now here if print is invoked with "Hello , world" i.e print("Hello , world") is called then since we have a constructor for String that takes in an array of characters . So array of characters are automatically converted into a String object which is then passed to then print function .

Case 2:
Using
the conversion operator it is possible to implicitly convert one object to another type. If the String class had the following operator defined

operator const char* ( ) const { return string; }

and somewhere in some function if the following code was used

String s("hello world");
cout << style="font-weight: bold;">So, with that also an implicit type conversion happens . Implicit type conversion can get confusing , so should be used with care . Explicit case or function calls are much better as they indicate the intended operation more clearly .



Friday, June 08, 2007

Little Sugar (Compound Assignment operator And Implicit Type Conversion)

x += i

'+=' This is what is called the compound assignment operator in Java . An interesting thing about this operator is that , this operator automatically casts the type of expression on the right , which in this case is i to the type of the expression on the left which being x .

Wednesday, June 06, 2007

Little Sugar (Algorithm)

I came across an interesting property about numbers . If u have a number a and you consider multiples of the number , then for some number a the sum of the digits of the multiple is divisible by the number a

More than this , if you just check till the first 4 digit number , in that base then you get to know that for all multiple of the number the above mentioned property holds.

given a sequence [1 .. N] and a base ( b) , and number a . generating the multiples is as simple as doing 1 + a , 2 + a ....

finding the sum of the digits is a given base (b) is same as finding the sum of digits for base 10 that except for base 10 , base (b) is used

in code that looks like

while(n){
sum += n % base;
n = n/base;
}

the maximum 3 digit number is a given base is
base * base * base

Tuesday, June 05, 2007

Little Sugar 1 ( Java)

So often i come across something cool , but important so i thought using blogging about them would be nice idea , so here is my Little Sugar 1

when converting from 1 type to another , sign extension is performed if the source data is signed
eg

byte b = 0xff;
char c = (char) b

if the type is char , no sign conversion is peformed .

if converting from byte to char if no sign conversion is required than bitwise AND with 0xff is nice idea.


char c = (char) b & 0xff

Contract of Object.Hashcode in Java

In continuation with my ongoing summary series , today i am going to write about hashcode and objects in java . So lets get started .

The contract of hashcode in java
  • Inrrespective of the number of times hashcode is invoked on an object , it should return the same value.
  • If two objects are equal then the hashcode values should also be the same.
Generally , it s a good practise to store the implement the hashcode of two objects when you implement the equals of an object . You ask why ??

Well if you dont do that , then different instances of the object when stored into the hashtable , hashmap will be stored into the same hash bucket and when that happens all objects actually end up being stored in a linked list and hence cause drastically effect the performance of the program.

So what would be a good way of implementing the hashcode , well a simple way would be to pick a prime number and multiply it with the hashcode of all the fields , for primitive fields this would be their values

So let me give you a simple example , so if u have a class that has say two fields , field1 and field2 then the hashcode for that object can be

public int hashCode(){
int result = 17;
result += 37 * field1;
result += 37 * field2;
return result;
}

Choosing an i odd prime number reduces the chances of overflows . For immutable objects where calculating hashcode , might be an expensive process it makes sense to store the hashcode locally and to return the pre calculated hashcode value.

Monday, June 04, 2007

Implementing the Assignment operator for your class in C++

The other day i was reading about the assignment operator in C++ . Here is my small understanding of it . The job of the assignment operator is to overwrite the members of your class , with the new provided members.

Contract of the assignment operator
  • The assignment operator must work properly when an object is assigned to itself.
  • Since assignment is going to overwrite data for data of your object , the resources external to the object need to be free.
  • The assignment operator should return a constant reference to the assigned object.
Let me give you a reference implementation first . Then we can discuss about the contract.

1 const String& String::operator= (const String& other){
2 if(&other != this){
3 delete[] characters;
4 characters = new char[strlen(other.characters)+1];
5 strcpy(characters,other.characters);
}
}

Now lets discuss the implementation step by step .
if(&other != this)

We need to make sure that the object we are assigning is not the same object , other wise we would end up over writing the data members of the same object.

eg String s
s = s // this would not work if the above mentioned line is not there

Now lets move on to second point of the contract . Since the String class we have here is using characters which is something external to it . We need to delete it , re allocate it and then re initialize it .

// delete the characters
delete[] characters

// reallocate the characters
characters = new char[strlen(other.characters)+1];
// re initialize the characters
strcpy(characters,other.characters);

Now the third point is its important that we return a const reference to the object
being assigned . Thats the reason we are returning *this and thats the reason why
function signature reads .

const String& String::operator= (const String& other)

The reason we need this is to prevent users from trreating assignments as lvalue .
Basically

String s;
(a = b ) = s

something like this should be illegal .