Click here to Skip to main content
13,054,615 members (59,697 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Are two (a) in this codes equal?

class A ();
A a;


A* a;

And also it about structures
Posted 23-Mar-13 5:55am
vinodkumarnie 25-Mar-13 4:42am
nice question
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
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.value = 42;
    a2->value = 42;
    delete a2;  // ~a2() called
}  // ~a1() called when scope exits
Medall_full 23-Mar-13 17:30pm
gracias colega!!
hor_313 24-Mar-13 3:48am

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.
Rate this: bad
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.
// 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
// statements if pa pointed to a2 object


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Mar 2013
Copyright © CodeProject, 1999-2017
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