Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ structures Pointer
Hi
 
Are two (a) in this codes equal?
 
class A ();
A a;
 
OR
 
A* a;
 
And also it about structures
Posted 23-Mar-13 6:55am
hor_313632
Comments
vinodkumarnie at 25-Mar-13 4:42am
   
nice question
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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.
 
{
    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
  Permalink  
Comments
Medall_full at 23-Mar-13 17:30pm
   
gracias colega!!
hor_313 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In additionally to previous answers.
 
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:
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.
  Permalink  
v2

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100