0

why pointers must have specific type?

if polymorphism means that an object of a class A who inherent from class B amd class C, can be saved on pointers of all those three types A B and C, what is the word type for pointers even mean? i understand that when one declares an variable he has to say what type of memory he needs, but if polymorphism combine types, why the pointer need a type anymore?

3rd May 2021, 2:15 PM
‎Mmk
‎Mmk - avatar
9 Respuestas
+ 2
In this code you can see that it is possible in C++ to bypass the class structure and for example assign private member variables where that should not be possible. It is of course not recommended to do that but if you really want you can more ore less completely ignore the types. So types are really just for the people writing code/to make it easier and less error prone to access data and call methods. https://code.sololearn.com/ca2596a5a126/?ref=app
3rd May 2021, 7:57 PM
Hape
Hape - avatar
+ 1
if that so, then why when i try to specify a int variable to a char pointer, it doesn't work, but for classes A B and C one same pointer does work?
3rd May 2021, 4:22 PM
‎Mmk
‎Mmk - avatar
+ 1
1. so, what is the answer then. 2. C is more basic than C++? because i want the fundamentals of the programing.
3rd May 2021, 4:37 PM
‎Mmk
‎Mmk - avatar
+ 1
‎Mmk I don't know anything about C++, so I'm not sure of the answer. And yeah, C is much more basic than C++. You can actually do stuff like: int k = 88; char* a = &k; putchar(*a); in C and get the output without an error (just a warning, because pointer-types are only necessary for de-referencing them). Here's a more brief explanation: Consider that you declare an int (which takes 4 bytes in the stack) and set a pointer of an unknown type to the variable declared previously. Now, what if you had to display or use the value pointed by it? The pointer just holds the memory address now, as the type isn't specified. De-referencing a pointer actually reads an 'n' number of consecutive bytes from the memory pointed by it, and won't work properly if it doesn't know how many bytes to read. So basically, pointer-types help in de-referencing them. Like, an int* declaration reads 4 bytes from the memory location pointed by it while char* reads just a single byte. I hope this helps.
3rd May 2021, 5:01 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
so i guess about the classes that, it doesn't read the bytes anyway, it just go to the page with the class and read that code, hence it doesn't need to really know what exact type it is. right?
3rd May 2021, 5:08 PM
‎Mmk
‎Mmk - avatar
0
You are right. Pointers theoretically do not need an explicit type. Basically they store just a number which indicates a memory address. The types they have are just for the programmers writing the code so that you do not accidentally try to do something which makes no sense. This is also not really related to polymorphism. In normal C it is possible to convert for example an int pointer to a float pointer. You can do that if you really need it. But most of the times you want your pointers to remember what sort of data they point to.
3rd May 2021, 2:53 PM
Hape
Hape - avatar
0
‎Mmk Also, you'd need to know the type of the pointer while de-referencing it.
3rd May 2021, 4:10 PM
Calvin Thomas
Calvin Thomas - avatar
0
‎Mmk Oh, sorry, I was talking about C. I guess that C++ was designed to be more "secure". In C, only a warning is raised.
3rd May 2021, 4:28 PM
Calvin Thomas
Calvin Thomas - avatar
0
i didn't fully understand the code because is too advanced for me. but i see the point, and it helped me a bit to more understand the idea of pointers. thank you.
3rd May 2021, 8:23 PM
‎Mmk
‎Mmk - avatar