Click here to Skip to main content
15,867,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Are two (a) in this codes equal?

class A ();
A a;

OR

A* a;

And also it about structures
Posted
Comments
vinodkumarnie 25-Mar-13 4:42am    
nice question

No.
When you declare a pointer to something, whatever it is, you are declaring a variable whose type is "pointer to object" - not an instance of the object itself. Initially, a pointer will be null - it points to no instance so any attempt to use it without assigning an instance address to it will cause an error. Within an application, all pointers will have the same length (though this can differ from system to system: a x86 pointer is 32bit or 4 bytes long, while a, x64 pointer will be 64 bits or 8 bytes long.)

When you declare a instance of a class or structure you are allocating space on the stack for the whole class / structure rather than just enough for a pointer.
 
Share this answer
 
For the purpose of your question (and ignoring the syntax problems), use of either 'class' or 'struct' generates identical behavior (there are other differences here but not in the scope of your question).

When you create 2 objects with:

C++
A a1;
A* a2 = new A();


then a1 is created on the stack, and a2 is created on the heap. Construction occurs differently. Destruction occurs differently. Methods and data members are accessed differently.

C++
{
    A a1;  // create a1 on stack
    A* a2 = new A(); // create a2 on heap
    a1.foo();
    a2->foo();
    a1.value = 42;
    a2->value = 42;
    delete a2;  // ~a2() called
}  // ~a1() called when scope exits
 
Share this answer
 
Comments
Reinaldo Peña 23-Mar-13 17:30pm    
gracias colega!!
hor_313 24-Mar-13 3:48am    
Thanks

When we use pointers to class/structure and when we use of instance and what are the differences between usage of them?
Stefan_Lang 25-Mar-13 7:22am    
As explained above, the difference is that the former will be allocated on the heap, and the latter on the stack. These are different memory management models. The stack is much more performant and efficient than the heap, so you should use heap allocation only when there are good reasons. E. g. when you want to allocate an array of objects, but don't know the size of the array at compile time, then you cannot allocate it on the stack, instead you must use the heap.
In additionally to previous answers.

C++
A a; // object created in stack
A * pa = &a; // But it still have a pointer and you can get it next way
// Now if you call any method of the those 2 instances you call it on same object.
a.Method();
pa->Method();
// And set property performed also on same object
a.value = 42;
// pa->value right now is also 42 as pa pointed to same object

As instance of a deleted in scope there it was created you can't use pa variable outside of that scope (in solution 2 you can see that)
Additionally you should not destruct pa object by calling delete
But you can compare the pointers as an example:
C++
A a1; // Initialize 2 instances of the A class
A a2;
A * pa = bConditionton ? &a1 : &a2; // according to bCondition variable init pa
//.....
if (pa == &a1) 
{
// statements if pa pointed to a1 object
}
else
{
// statements if pa pointed to a2 object
}

Regards,
Maxim.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900