Friday, May 11, 2012

Assignment operator in RVALUE

I came across a line of code which looked a bit strange to me. It reads like this..
x= m * (n=7);
The above code is actually equivalent to
n=7
x=m*n

Actually c++ supports using assignments on the right hand side of assignments operator. And we are all familiar with that. I bet you must have seen assignments like this…
a=b=c;
This and the previous code follows the same rule.

Calling constructor from constructor


I was trying a few whiteboard programs, just for fun. In one of them I called a constructor from within another constructor. Code compiled without any fuss,but code didn’t worked the way I was expecting.  I found out that when calling a constructor from within a constructor a temporary unnamed constructor is created. That is the reason why program is not behaving the way I was expecting. Check out this…


using namespace std;
class foo
{
int x;
public:
foo()
{
x=20;
}
foo(int d)
{
x=d;
foo();//value should be changed to 20, but it wont
}
void show()
{
foo a;
a.show();// will print 20
foo b(10);
b.show();// will print 10. I was expecting it to print 20
return 0;
}
(This one is from my old blog.)

Thursday, May 10, 2012

Constant functions and Mutable

For a c++ rookie this syntax may look interesting ..

void myFunc() const;

Normally we love to see const at the beginning of a line . like "const int i = 10;". Actually above line is perfect. It tells that the function can't modify it's calling object. It is referred as read only function in some text books. It can call some more constant functions from inside. But it can't call any normal function.  This type of functions can be called from any ordinary functions as well as constant functions.

Suppose you want to declare a constant function, but at the same time you want it to modify some particular data. In that case you need to use mutable key word while declaring your variables.

 mutable int x;






Saturday, May 5, 2012

A new project(Data structure)

I was looking to start something new for a while. Am planning to work on a new STL like project. It's kind of template library. What we planned is using hash tables underneath a wrapper. User will use vector , map and other stuffs. Our library will save the data in a hash map. We plan to use this algo. My first stop is reading  G. M. Morton's paper on hashing. Will setup sun studio tomorrow. Lets see what comes next :)

Saturday, February 4, 2012

Function Pointer


Just like variables, functions also has their own memory addresses . So we have pointers for them too.
void (*fPtr)();// a void function pointer declaration which takes no args.
fPtr = &myFunc;
(*fPtr)();

int (*ptr2)(int,int);// a int function pointer declaration which takes 2 int args.
ptr2 = &myOtherFunc;
int i=0,j=2;
int result = (*ptr2)(i,j);

#{Header}

Hi,
    It's late night in 4th feb. I was wasting time re inventing wheels , i mean i was working on some basic c++ codes. I have practiced them a number of times, but at present i couldn't remember them. I think it's time to make a note of all these. So I am creating this blog for myself. I will post all cpp related stuffs which i feel necessary for me. If you find this site useful, please get in touch. I would love to see you as a contributer.

Regards
Rwik