In C++, these two have no difference.
char *pC;
char* pC2;
I thought that the type char and char* have got to be different types and all the C++ programmers had to use this form char* p instead of char *p, because when you do char *p it doesn’t seem to specify that the programmer is using a pointer to char.
However, when you take a look at this code below, which is the same as nullptr from C++0x, it seems that T doesn’t recognize any pointer type.
const class{
public:
// T is char, not char* in this case.
// T* is char*, but I thought it would be char**
template <class T>
operator T*() const{
cout << "Operator T*() Called" << endl;
return 0;
}
template <class C, class T>
operator T C::*() const{
cout << "Operator C T::*() Called" << endl;
return 0;
}
private:
void operator&() const;
}AAA = {};
int main(){
// operator T*() const is called,
// but it's awkward,
// because it looks like T will be char* type, so T* will be char**
char* pC = AAA;
}
Thanks in advance!
You’re correct that
charandchar*are different types. Whether programmers saychar* porchar *pis irrelevant. Those are three separate tokens, and there can be as many or as few spaces between them as you want; it has no effect on the type ofp.When you initialize
pCwithAAA, the compiler needs to choose a conversion operator fromAAA. Since it’s initializing achar*variable, it wantsoperator char*. To make that work, the compiler needs to chooseT = char. SinceTischar, there’s no wayT*could bechar**.If you want
Tto be a pointer type, then useAAAin a context where a pointer-to-pointer type is expected, such aschar**. ThenTwill bechar*.