Casting syntaks with pointers

Is this same thing:
1. T*(someVoidPtr)
2. (T*)someVoidPtr
No.
The first one is a declaration and declares someVoidPtr to be a pointer to type T.
This line can exist on it's own.

the second one is a cast and creates a temporary variable of pointer to T (an L value) from the existing pointer someVoidPtr
So this line cannot stand on it's own - the variable being casted must already been declared.
I am sorry i should provide it as whole:
1.
1
2
3
4
5
T* foo()
{
    void* someVoidPtr = GetFromSomeone();
    return T*(someVoidPtr);
}

2.
1
2
3
4
5
T* foo()
{
    void* someVoidPtr = GetFromSomeone();
    return (T*)someVoidPtr;
}


Is same?
Last edited on
Try both methods (you should find the first one is a syntax error)
Actually they should both do exactly the same. C-style cast.
Do they though :-)
Ha! ;-)
You are right, from my example it is indeed syntaks error. But how then from someone elses code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <memory>
template< typename T >
class allocator
{
public:
    typedef T* pointer;
...
        pointer allocate(size_type cnt)
	{
		return pointer(operator new(cnt * sizeof(T)));
	}
};

i am confused then as what is this if it isn't casting?
It is casting.

I was only partially right. The problem was the '*'. If you try your example with T instead of T* it should behave like normal casting.

Like
1
2
3
4
5
6
typedef T* pT;
T* foo()
{
    void* someVoidPtr = GetFromSomeone();
    return pT(someVoidPtr);
}


Last edited on
My brain just melt down. Why typedef makes difference in this example? pT is still T* not T, right?
There must be something behind-scene that i don't see.
It probalby has to do with operator precedence and ambiguity issues.

consider this:

1
2
3
4
5
6
7
8
9
10
double val = 6.2;
double* pd = &val;

// look at the following:
(int*)(pd);  // clearly casting pd to an int*
(int)(*pd);  // clearly dereferencing pd to get a double, then casting the double to an int

// take away the parenethesis and all of the sudden it's not clear:
int * pd;  // ??  wtf?  Looks more like you're declaring a pointer, rather than casting
int *(pd); // ??  wtf?  same 


The thing that makes it ambiguous is the * operator.

T(foo) is a valid cast, but T*(foo) is not because it's ambiguous, as illustrated above.

The typedef here eliminates the need for the * operator, therefore removes the ambiguity.
Last edited on
typedef allows you to define a type that is equal to another already defined type, just an alias. It is not like a #define statement in that it is handled by the compiler and not the preprocessor. If you typedef int as Number, then the compiler recognizes Number as an alias for an int; it does not actually replace the text Number with int.

In C++, because of the multiple uses of the asterisk *, trying to cast without parenthesis like that confuses the compiler. Using typedef creates an alias for it, and so the syntax is no longer confusing and clearly a cast.
Thanks all of you.
Topic archived. No new replies allowed.