0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
There are cases when you need to define a constant pointer to a variable/object; for instance, when taking a function address, or when you want to protect a pointer from unintended modifications such as assignment of new address, pointer arithmetic, etc. In fact, an object’s this is a constpointer. A constant pointer is declared:

Code:
int j = 10;
	int *const cpi = &j; //cpi is a constant pointer to an int
	*cpi = 20; 	//OK, j is assigned a new value
	cpi++; 	//Error; can not change cpi
If you’re confused by the syntax, remember: const defines a constant pointer, whereas a const variable is declared like this:

Code:
const int k = 10; //j’s value may not be changed
And a const pointer to a const variable:

Code:
 int *const cpi = &k; //cpi is a constant pointer to a const int

	*cpi = 20; //Error; k’s value cannot be modified
	cpi++; //Error; can not modify a const pointer
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Although the syntax of pointers to members may seem a bit confusing at first, it is consistent and resembles the form of ordinary pointers, with the addition of the class name followed by the operator :: before the asterisk. For example, if an ordinary pointer to int looks like this:



Code:
 int * pi;
you define a pointer to an int member of class A like this:


Code:
class A{/**/};
  int A::*pmi; // pmi is a pointer to an int  member of A
You can initialize a pointer to member:


Code:
class A
  {
  public:
    int num;
    int x;
  };
  int A::*pmi = &A::num; // 1
The statement numbered 1 defines a pointer to an int member of class A and initializes it with the address of the member num. Now you can use the pointer pmi to examine and modify the value of num in any object of class A:


Code:
A a1;
  A a2;
  int n = a1.*pmi; // copy the value of a1.num to n
  a1.*pmi = 5; // assign the value 5 to a1.num 
  a2.*pmi = 6; // assign the value 6 to a2.num
Similarly, you can access a data member through a pointer to A:


Code:
A * pa = new A;
  int n = pa->*pmi; // assign to n the value of pa->num 
  pa->*pmi = 5; // assign the value 5 to pa->num
Or using a pointer to an object derived from A:


Code:
class D : public A {};
  A* pd = new D;
  pd->*pmi = 5; // assign a value of 5 to pd->num
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Pointers to member functions consists of the member function's return type, the class name followed by ::, the pointer's name, and the function's parameter list. For example, a pointer to a member function of class A that returns int and takes no arguments is defined like this (note that both pairs of parentheses are mandatory):



Code:
class A  {
  public:
    int func ();  
  };

  int (A::*pmf) (); /* pmf is a pointer to some member 
  function of class A that returns int  and takes no 
  arguments*/
In fact, a pointer to a member functions looks just like an ordinary pointer to function, except that it also contains the class's name immediately followed by the :: operator. You can invoke the member function to which pmf points like this:


Code:
  pmf = &A::func; //assign pmf
  A a;
  A *pa = &a;
  (a.*pmf)();  // invoke a.func() 
// call through a pointer to an object
  (pa->*pmf)();  // calls pa->func()


Pointers to member functions respect polymorphism. Thus, if you call a virtual member function through a pointer to member, the call will be resolved dynamically:


Code:
 class Base{
  public:
    virtual int f (int n);
  };
  class Derived : public Base {
  public:
    int f (int h); //override
  };

  Base *pb = new Derived;
  int (Base::*pmf)(int) = &Base::f;
  (pb->*pmf)(5); // call resolved as D::f(5);


Note that you cannot take the address of a class's constructor(s) and destructor.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Does you compiler accept the following code?


Code:
 class A
  {
    public:
    void f(int);
  };
  void (A::*pm)(int) = A::f; // #1 no ampersand before A::f
A standard-compliant compiler should flag line #1 as an error. Let's examine it in further detail to see why. The expression A::f is called a qualified-id. A qualified-id denotes a member of a class. According to the C++ standard, there is no implicit conversion from a qualified-id denoting a member function to the type "pointer to member function". In other words, an ampersand must appear before the qualified-id if you want to take the address of the member function. For example:


Code:
 void (A::*pm)(int) = &A::f; // now OK
Programmers who are new to the concept of pointers to members are often confused by this subtlety. After all, when dealing with ordinary functions, implicit conversion from a function type to the type "pointer to function" does exist:


Code:
void g();
  void (*pf) () = g; // OK, implicit conversion of g to &g
However, there are many differences between a qualified-id and a plain function's name. Enabling implicit conversion of a qualified-id to a member function's address can therefore cause confusion and ambiguities. To avoid this, C++ requires that a class member's address be taken explicitly by preceding an ampersand to the qualified-id. Even if your compiler happens to accept the code in line #1, you should add an ampersand to avoid maintenance problems in the future, and to make your code more readable.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
One of the new features in the recently approved C standard C99, is the restrict pointer qualifier. This qualifier can be applied to a data pointer to indicate that, during the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. The 'restrict' keyword thus enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed through another pointer. Now you're probably asking yourself, "doesn't const already guarantee that?" No, it doesn't. The qualifier const ensures that a variable cannot be changed through a particular pointer. However, it's still possible to change the variable through a different pointer. For example:



Code:
 void f (const int* pci, int *pi;); // is *pci immutable?
  {
    (*pi)+=1; // not necessarily: n is incremented by 1
     *pi = (*pci) + 2; // n is incremented by 2
  }
  int n;
  f( &n, &n);

In this example, both pci and pi point to the same variable, n. You can't change n's value through pci but you can change it using pi. Therefore, the compiler isn't allowed to optimize memory access for *pci by preloading n's value. In this example, the compiler indeed shouldn't preload n because its value changes three times during the execution of f(). However, there are situations in which a variable is accessed only through a single pointer. For example:



Code:
FILE *fopen(const char * filename, const char * mode);
The name of the file and its open mode are accessed through unique pointers in fopen(). Therefore, it's possible to preload the values to which the pointers are bound. Indeed, the C99 standard revised the prototype of the function fopen() to the following:


Code:
/* new declaration of fopen() in <stdio.h> */
  FILE *fopen(const char * restrict filename, 
                        const char * restrict mode);
Similar changes were applied to the entire standard C library: printf(), strcpy() and many other functions now take restrict pointers:


Code:
 int printf(const char * restrict format, ...);
  char *strcpy(char * restrict s1, const char * restrict s2);
C++ doesn't support restrict yet. However, since many C++ compilers are also C compilers, it's likely that this feature will be added to most C++ compilers too.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
In low-level programming and hardware interfaces, you often need to assign a pointer to a specific physical address. To do that, you have to cast the address value using the reinterpret_cast operator. Here's an example that shows how this is done:


Code:
void *p;
// assign address 0x5800FF to p
p = reinterpret_cast< void* > (0x5800FF);
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Many times, a program crashes due to multiple deletes on the same pointer. In some cases, this is due to a programming error that can be removed. There are situations, however, where it may be unclear whether to delete a pointer or not. Let's consider the code given below:


Code:
void function(){
	char *pcMemory;
	try {
		pcMemory = new char[25];//allocate memory
		//perform some operations
		delete[] pcMemory; //delete memory
	}
	catch(...){ }
	//perform some operations
	//Delete pcMemory or Not??
}

If everything works fine, pcMemory will be properly deleted at the end of the try block. But if one of the operations between allocation and deletion throws an exception, pcMemory will remain allocated. At the end of function, it's not known whether to delete pcMemory or not. Deleting it may cause a double deletion followed by a crash. Similarly, if it's not deleted, it may result in a memory leak. The trick is to always set the memory pointer to NULL after deletion:


Code:
void function(){
	char *pcMemory;
	try {
		pcMemory = new char[25];//allocate memory
		//perform some operations
		delete[] pcMemory; //delete memory
		pcMemory = NULL;
	}
	catch(...){ }
	//perform some operations
	delete[] pcMemory; //Deleting NULL is legal and not an error or crash.
}
Because a delete statement can easily be put at the end of function, whether pcMemory has been deleted or not, deleting NULL is absolutely safe.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Pointers to objects or functions of the same type are equal if and only if in they are both NULL:


Code:
 int *p1 = NULL, p2 = NULL;
  bool equal = (p1==p2); //true
Or if they point to the same object:


Code:
 char c;
  char * pc1 = &c;
  char * pc2 = &c;
  equal = (pc1 == pc2); // true
Additionally, pointers are equal if they point one position past the end of the same array.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
A reader posted a message on one of the C++ newsgroups recently. He had to port a huge project from Borland C++ 5.0 to Visual C++ 6.0. The project uses the callback mechanism through pointers to member functions. Unfortunatly, these pointers are assigned member functions with incompatible signatures. For example:


Code:
class Base;
  //a pointer to a member function that takes no arguments and returns bool
typedef bool (Base::*meth1)(); 
  //a pointer to a member function that takes long and returns bool
typedef bool (Base::*meth2)(long);

class Base{
public:
  bool Base::perform(meth1 m, long param);
};
Due to a historical design mistake, the pointer to member m (the first argument of perform) is sometimes called with more than one argument, and sometimes it is called with no arguments. Of course, a brute force cast is needed to enable that, as in the line numbered 1:


Code:
bool Base::perform(meth1 m, long param)
{
 meth2 *p = (meth1 *)(&m); // 1.cast pointer to meth1 to pointer to meth2
 meth2  q = *p; 
// force m to take an argument, although it's not supposed to take any
 return (this->*q)(param);  //2
}
For some reason, the application built under Borland's compiler tolerated this hack. However, under Visual C++, it crashes when the line numbered 2 executes. The reader wanted to know if there was a simple way to make this code execute under Visual C++ without encountering a runtime crash and without having to make code corrections.

Whatever the answer may be (perhaps there is a way to tamper with the underlying code generation of Visual C++ and make this code somehow work), there is no escape from fixing the code so that it avoids incompatible casts of pointers to member functions. There are several reasons for that. First, even if there is a magic trick that will make this code run under Visual C++ 6.0 without a crash, that is only a temporary band-aid. There's no guarantee that this trick will work with future releases of Visual C++. Furthermore, the C++ Standard clearly says that using incompatible pointers to member functions yields undefined behavior. Founding an application on such shaky foundations -- clearly knowing that the code violates basic rules of the language -- is an ominous sign regarding the quality and the reliability of the software in question.
0
subhasish's Avatar, Join Date: Mar 2006
Go4Expert Member
Callback functions have a fixed signature so you cannot alter the number or type of the arguments it takes. For example, the standard qsort() function takes a pointer to a function that has the following signature:



Code:
 int (cmp*)(const void *, const void *) //user's comparison function
This signature allows you to pass exactly two arguments to your custom-made comparison function. However, suppose that the comparison function has to compare strings and you want to pass a third argument to indicate whether the comparison should be case-sensitive or not. You cannot simply add a third argument to the function call, because the compiler will complain about it (don't even think about casting the function pointer; see this tip). Instead, you can define a struct that contains two members:



Code:
 struct Comparison
  {
    char *name;
    bool caseSensitive;
  };
Now instead of passing a pointer to char as the second argument, you can pass a pointer to an instance of that struct. The comparison function will unpack the struct and compare the strings accordingly:


Code:
  int MyCompareFunc(const void *pfirst, const void * psecond)
  {
    const Comparison * pcmp = (Comparison *) psecond; 
    const char * pstr1 = (const char *) pfirst; 
    const char * pstr2 = pcmp->name; //extract string from struct
    if (pcmp->caseSensitive == true) //the second field of the struct
    { 
       return strcmp(pstr1, pstr2)
    }
    else    
    { 
      //perform case-insensitive comparison
    }
}