why do we want to point to a class?

Jul 15, 2018 at 4:39pm
Hi guys,

1
2
3
4
5
6
7
  class A{
}
int main(){
A a;
A*aa;
return 0;
}


I want to know what the difference is between A a et A*aa?
Thank you for any help you can give.
Jul 15, 2018 at 5:28pm
The same as
1
2
int a;
int* a;

When you point to an object of a class, you have the ability to initialize it on the heap instead of initializing it on the stack. You basically have the benefits of pointers.
Jul 15, 2018 at 5:33pm
It's nothing to do with heap vs. stack in and of itself. You can have
1
2
int a = 3;
int* p = &a;

Everything is still stack-allocated here.

But yes, pointers tend towards being used with heap memory objects with extended lifetimes.

@heros
I would look up tutorials on C++ pointers if you want to know how to use them correctly.
http://www.cplusplus.com/doc/tutorial/pointers/
https://gist.github.com/ericandrewlewis/720c374c29bbafadedc9

Note that pointers can be avoided a lot in modern C++ by using references, standard library data structures like std::vector, and only dynamically allocating memory when necessary.
Last edited on Jul 15, 2018 at 5:36pm
Jul 15, 2018 at 6:58pm
Thanks Uk Marine.

Thanks Ganado for your tutorial.
Topic archived. No new replies allowed.