object and pointer declarations


Like we have class:
class ABC
{};

Then what is difference b/w two following declarations:

ABC abc; and ABC *abc;

Where are use cases of these object and pointer declarations?
See my post in this thread:

http://cplusplus.com/forum/general/105593/

for some reasons to use pointers.
What is the meaning of pass by reference??
in java its simple everything is a reference, in c++
abc is an object, * abc is reference capable of pointing to any object of same type.
so if u say
1
2
3
ABC abc;
ABC *bca=abc; // just acts as a reference to abc object
ABC cda=abc; // creates a new object copying the contents of abc object 

abc is an object, * abc is reference capable of pointing to any object of same type.

No. It's a pointer. In C++, a reference is different from a pointer.

Please don't confuse people by giving them misleading information.

Ok my mistake, here u go amit
1
2
ABC &bca=abc;// is a reference
ABC *bca=abc;// is a pointer 


only difference is reference cannot be null and once assigned it cannot be re-assigned.A pointer can be null, and can be made to point to different variables.However internally compilers implement references using pointers.
Last edited on
only difference is reference cannot be null and once assigned it cannot be re-assigned.A pointer can be null, and can be made to point to different variables.

No, that's not the only difference. The syntax for using a reference is different from the syntax for using a pointer. References have no equivalent of pointer arithmetic.

Please don't confuse people by giving them misleading information.
Milky Buoy, please give a detailed explanation so that itllb convenient for amit , i suppose u do something more than chiiping into others answers :D
closed account (z05DSL3A)
anirudh sn wrote:
Milky Buoy, please give a detailed explanation so that itllb convenient for amit , i suppose u do something more than chiiping into others answers :D

just because someone pulls you up on your inaccuracy doesn't mean you have the right to bait him.
ABC* ptr; create a pointer that can point to any object of type ABC.

a pointer is no more than a 4byte variable (on 32bit platforms), it is NOT a complete object of that class.
the task of a pointer is to hold the memory address of some object.
in this case you say that the pointer POINTS to that object.
you can use the referencing operator "&"to get the address of any object (even the address of a pointer), use it like this:
ptr = & object;

you can use the de-referencing operator "*" to get the object pointed to by your pointer:
1
2
ABC object_2;
object_2 = (*ptr);


ABC object; creates a full instance of the class ABC
Milky Buoy, please give a detailed explanation so that itllb convenient for amit

There's absolutely no point me spending time re-typing information that is already readily available out there online. I'm happy to help with problem-solving, and to help identify and fix problems with people's code, as my posting history here shows. But if information is easily found elsewhere, I don't see the purpose in retyping it.

It would have been trivial for Amitk to type a Google search - or consult his/her textbook - to find the information they were looking for in seconds.
Topic archived. No new replies allowed.