Class pointer?

Aug 11, 2017 at 8:05pm
I'm sure it's a dumb question but I don't see the difference.


1
2
3
4
5
6
7
8
9
10
11
  class person{
     public:
        int Age;
  }

   int main(){
      person a = new person;
      person* b = new person;

      return 0;
   }


What's the difference between a and b objects? I know a is a person and b is a person pointer but I don't understand what's the point of creating a person pointer.
Aug 11, 2017 at 8:14pm
does this compile?

I think you don't see the difference because it isnt legal.

you can only use new with a pointer. the legal code might look like


person a; //a class is just a user defined type, like int or double. And creating one looks the same.
person *b = new person; //and a pointer to one looks the same as int* etc.


Modern c++ is not big into having tons of pointers. The containers and language changes make them less necessary for most tasks.
Last edited on Aug 11, 2017 at 8:18pm
Aug 11, 2017 at 8:17pm
And what's the difference between

person a;
person *b = new person;
Aug 11, 2017 at 8:33pm
person a; -- this is a variable of type person. you can use it like this:

a.Age = 11;


person *b = new person; -- this is a pointer. A pointer is just an integer that is an index into the computer's ram (in simplistic terms). Calling new allocates a chunk of memory based off this location to store the data for the class. This moves the storage of the object from the program stack to the 'heap' which are basically operating system terms for areas of memory, but it boils down (again, oversimplified) to the stack having a relatively small size, and the heap being the size of your ram. Say the stack is 10MB (making this up) and your computers memory might be 8GB. So if you needed a LOT of this class object, you might do this, however vector and similar containers do this for you by using pointers internally.

you call this by

p->Age = 11; //go to the location in memory where the object is using -> operator to access the data indirectly. the same result from other syntax:
p[0].Age = 11; //same thing, alternative syntax
*p.Age = 11; //same again, another version

the [] notation is telling, if you understand what an array is in memory and what this operator does (?).

I can go into pointers more carefully if you are still lost. Maybe your question is "what is a pointer, and what is it good for" ?

Last edited on Aug 11, 2017 at 8:35pm
Aug 11, 2017 at 8:43pm
You should first ask what is the difference between
1
2
int  a;
int* b;

- they are both variables with automatic storage duration
http://en.cppreference.com/w/cpp/language/storage_duration
That is not a difference.

- both are stored in stack memory. That is not a difference.

- they are both uninitialized. That is not a difference.

- both store a value. The type of the stored value differs.


The value that has a pointer type is an address of some point in memory.
For example of a dynamically allocated piece of the free store area.
http://www.gotw.ca/gotw/009.htm

Aug 12, 2017 at 3:45am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class person{
     public:
        int Age;
  }; // <--

   int main(){
      person* a = new person; // <-- must be a pointer because of new
      person* b = new person;

      delete a; // <--
      delete b; // <--

      return 0;
   }


http://en.cppreference.com/w/cpp/language/new
Aug 12, 2017 at 3:57pm
Hi,

Unless you are writing a library or trying to simulate an STL feature, avoid using new altogether. Use an STL container (like std::vector for example) instead, they store their data on the heap and do their own memory management.

There is lots written why new / delete are bad news.

good luck !!
Topic archived. No new replies allowed.