Class Pointer initialization C++

Hello everyone,

I have a question, that are the following ways of pointer intialization same ?

ClassA *point;

point = 0;

point = new ClassA;

Thanks a load in advance!!

Regards,

Of course they're not the same, one of them isn't even valid C++ syntax.

I thought they are same in the sense that :

point = 0; //intializes the pointer to zero value and

point = new ClassA(); //intializes the pointer sm memory bt. not any value means zero value

as far as I remember, in

ClassA * point;
point = new ClassA;

`new' will allocate memory for exactly one object of type `ClassA' and yield the address of the 1-st memory cell at which the definition of the object will start. In

ClassA *point;
point = 0;

you're not creating any object at all but just explictily stating that the pointer `point' will point to the zero-th cell. In that case your pointer merely becomes the so called `null pointer' - a special kind of pointers which do not point to any valid memory address.

I might err, so please correct me, if I'm wrong

---------- Post updated at 11:20 AM ---------- Previous update was at 11:20 AM ----------

as far as I remember, in

ClassA * point;
point = new ClassA;

`new' will allocate memory for exactly one object of type `ClassA' and yield the address of the 1-st memory cell at which the definition of the object will start. In

ClassA *point;
point = 0;

you're not creating any object at all but just explictily stating that the pointer `point' will point to the zero-th cell. In that case your pointer merely becomes the so called `null pointer' - a special kind of pointers which do not point to any valid memory address.

I might err, so please correct me, if I'm wrong

I get what you guys are saying;

point = 0;

//intializing the pointer a NULL value...means pointing to nthing!

point = new ClassA();

//intializing the pointer value of the object of the Class A and hence ofcorse points to a valid memory!

Thanks a tonnn guys!!

That's still not even valid C++ syntax. It will fail to build with compiler errors. You can't assign an integer to a pointer in C++ without a typecast.

gotcha: //only if I'm right this time!

ClassA{};

ClassA *point;
*point = 0;

ClassA *point = new ClassA();

Now you've turned it from invalid syntax, into something that's likely to crash... Think about what you're doing: You're trying to assign the contents of an unassigned pointer to NULL -- in other words, picking some random, arbitrary bit of memory and trying to write 0 into it.

Fortunately, this is a compiler error anyway since 0 isn't a ClassA.

How about:

classA *point=NULL;

NULL is like zero, but is of type void, which C++ won't complain about.

Allright, my C++ knowledge are definitively rusty... But I believe, there is a slight inaccuracy in the statements claimed.

ClassA *point;
point = 0;
point = new ClassA;

What let you believe this? Using 0 for NULL is perfectly legal in C++, there are actually equal. Stroustrup himself prefers to use "0": Should I use NULL or 0

This line may also work:

point = new ClassA;

as long as the ClassA provides a default constructor.

Cheers, Lo�c

Even though any other integer isn't. Hooray, more weird special cases to learn in the C++ language :wall:

Naturally he'd prefer to use 0, and so not need to include stdio.h, but it's a strange inconsistency for the C++ language to let you assign 0 when it refuses to let you assign any other integer pointer value. That's what the void type used to be for, which is what NULL was of..

As you mentioned already, C++ is a strongly typed language (or a least claims to be so).
If you try the following assignment:

ClassA* point = ((void*) 0); // ((void*)0) is the C definition for NULL

Then the compiler shall complain that you're trying to perform an invalid conversion from void* to ClassA* ...

Lo�c