Showing posts with label const functions. Show all posts
Showing posts with label const functions. Show all posts

Sunday, December 6, 2009

Using pascal keyword

1)
main()
{
int i=10;
void pascal f(int,int,int);
f(i++,i++,i++);
printf(" %d",i);
}
void pascal f(integer :i,integer:j,integer :k)
{
write(i,j,k);
}
Answer:
Compiler error: unknown type integer
Compiler error: undeclared function write
Explanation:

Pascal keyword doesn’t mean that pascal code can be used. It means that
the function follows Pascal argument passing mechanism in calling the functions.



2)





void pascal f(int i,int j,int k)
{
printf(“%d %d %d”,i, j, k);
}
void cdecl f(int i,int j,int k)
{
printf(“%d %d %d”,i, j, k);
}
main()
{
int i=10;
f(i++,i++,i++);
printf(" %d\n",i);
i=10;
f(i++,i++,i++);
printf(" %d",i);
}
Answer:
10 11 12 13
12 11 10 13
Explanation:
Pascal argument passing mechanism forces the arguments to be called
from left to right. cdecl is the normal C argument passing mechanism where the
arguments are passed from right to left.


Saturday, December 5, 2009

Access non const member varible inside a const member function

class MyClass {
public:
      void F() const {
        const_cast(this)->c = 10;
        //c = 10;
      }
    private :
      int c;

}; 

Other solution is mutable.