Showing posts with label Web programming. Show all posts
Showing posts with label Web programming. Show all posts

Sunday, June 26, 2011

C++: Pointer to members

It's been long since I felt excited about any new mobile OS. Maemo 5 seems to be a very promising mobile OS for the next generation of mobile devices.

Multiple desktops, multitasking, Mozilla based browser, desktop widgets, cool UI, speed all seems to be best of what other OS has to offer. And guess what, it runs on Linux. The Symbian OS 9.4, Windows Mobile 6.5 or iPhone OS 3.. all seem to be moving ahead somewhat slowly. Comparing to them, Maemo 5 seems to be a leap ahead. It seems to be the next best thing after iPhone OS 1 came out thousands of years ago :).

Nokia will release N900 in a few months, the company's first phone running on Maemo 5. N900 itself has good spec with 600 MHz ARM processor, 256 MB RAM, 32 GB ROM, high-end cam, etc.

You can check more about Maemo at: http://maemo.nokia.com/ and more about N900 at http://maemo.nokia.com/n900/.
Posted by Mohammad Kaisar Ul Haque at 3:35 AM 3 comments Links to this post
Labels: linux, mobile applications
Wednesday, February 18, 2009
C++: Pointer to members
Pointer to member operators are not an everyday operators that we use but those can be useful if used in the correct way. Pointer to member operator ::* is used to declare a pointer that points to a member variable, pointer or function of a class. The operators .* and ->* are used to access it.

Following is an example of how we can use Pointer to member function (also called member function pointer).

1 #include
2 using namespace std;
3
4 class A{
5 public:
6 A(){
7 }
8 int Func(int a){
9 return a * 2;
10 }
11
12 };
13
14 int main(){
15 int (A::*fp)(int) = &A::Func;
16
17 A a;
18 cout << (a.*fp)(10) << endl; 19 20 return 0; 21 } 22 Following is another (almost random) example of how we can write generic classes to wrap pointer to member function to use with multiple classes. 1 #include
2 using namespace std;
3
4 class A{
5 public:
6 int Func1(int a){
7 return a * 2;
8 }
9
10 };
11
12 class B{
13 public:
14 int Func2(int a){
15 return a / 2;
16 }
17
18 };
19
20 template
21 class MemFnPtr
22 {
23 public:
24 MemFnPtr(int (T::*fp)(int), T &t) : m_fp(fp), m_t(t){
25 }
26 int Fn(int n)
27 {
28 return ((m_t).*(m_fp))(n);
29 }
30
31 //member vars
32 int (T::*m_fp)(int);
33 T &m_t;
34 };
35
36 int main(){
37 A a_obj;
38
39 MemFnPtr fp_a(&A::Func1, a_obj);
40 cout << fp_a.Fn(10) << endl; 41 42 B b_obj; 43 44 MemFnPtr fp_b(&B::Func2, b_obj);
45 cout << fp_b.Fn(10) << endl;
46
47 return 0;
48 }
49

Programming: C++ And C

C++ compilers (g++ or visual c++ for example) can compile many C codes, but not necessarily all of them. Here is a simple but lame example:

int class, template;

Above code compiles fine with a C code but not with C++, as 'class' and 'template' are keywords in C++. But again, it's really a lame example.

A good example is the feature set of C99. Here are some codes that are valid in C but not in C++:

int vec[5] = { [1]=10, [3]=20 }; // designated initializers

typedef struct
{
char name[20];
int ID;
int age;
}Employee;

Employee emp = {.ID = 0, .age = 0};

int main()
{
}

The reason for not having the support for the C99 code in some major C++ compilers is that C++ was standardized in 98 and C99 standards came after that.
WordLight is a nice little add-in for Visual Studio 2008. When you select some text in the editor, it searches and highlights the same string in rest of the code.

URL: http://code.google.com/p/wordlight/

If you have VisualAssist, you may not need this. Otherwise, you'll enjoy this add-in.