Click here to Skip to main content
15,949,686 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
CPallini21-May-24 20:41
mveCPallini21-May-24 20:41 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan21-May-24 22:37
mveRichard MacCutchan21-May-24 22:37 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
jschell23-May-24 13:17
jschell23-May-24 13:17 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard Andrew x6423-May-24 14:09
professionalRichard Andrew x6423-May-24 14:09 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan23-May-24 22:13
mveRichard MacCutchan23-May-24 22:13 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
k505424-May-24 8:55
mvek505424-May-24 8:55 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan24-May-24 22:43
mveRichard MacCutchan24-May-24 22:43 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
jschell27-May-24 12:10
jschell27-May-24 12:10 
Richard MacCutchan wrote:
If you have something like the following:


For background I have 10 years of C and 15 of C++ after that so I do understand a bit of how it works. Not to mention wild forays into assembler, interpreters, compilers, compiler theory and spelunking through compiler libraries. I have written my own heaps (memory management), my own virtual memory driver, device drivers and hardware interfaces. So I do understand quite a bit about how computer languages work and how the language is processed.

I have used char arrays as ints. I have used char arrays as functions. I have used void* to hide underlying data types. I have used void* in C to simulate C++ functionality.

Richard MacCutchan wrote:
When you use a cast the compiler does what can be done at machine level, but the object itself is still a char*, and any attempt to use it in any other way will raise a compiler error.


That specifically is not true.

Once a char* is cast to an int (or int*) then the compiler specifically and exclusively treats it as that new type.

The question is not how it is used but rather how it is defined to the compiler.

Richard MacCutchan wrote:
And the result of calling that function may, or may not, make sense.


All of those worked because the compiler did what it was told. The cast changed the type. The compiler respected the type and it did not and does not maintain information about previous types.

A human understands that the underlying data originated from a character array.

However the compiler does what it is told. And once it is cast to a different type it is in fact a different type to the compiler. By definition. You, the human, can use it incorrectly but you (again the human) can use the original type incorrectly as well. That has nothing to do with the cast but rather how the human uses it.

The easiest way, perhaps only way, for a language to preserve type is to not allow the type to be changed at all. Java and C# do that.

Going back to what was originally said by you.

"pretend it points to an int"

The compiler is not doing that. To the compiler once the cast occurs the data is now the new type. Whether that is a problem or not is a human problem, not a compiler problem.

For the compiler to be involved in this at all the underlying data would need to keep track of the type. And it does not do that.
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan27-May-24 21:57
mveRichard MacCutchan27-May-24 21:57 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
trønderen28-May-24 5:19
trønderen28-May-24 5:19 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
jschell29-May-24 5:26
jschell29-May-24 5:26 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
trønderen29-May-24 6:53
trønderen29-May-24 6:53 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg21-May-24 8:02
charlieg21-May-24 8:02 
AnswerRe: Okay, real C++ question - who makes use of the auto keyword? Pin
11917640 Member 21-May-24 22:09
11917640 Member 21-May-24 22:09 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg21-May-24 23:47
charlieg21-May-24 23:47 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
11917640 Member 22-May-24 0:00
11917640 Member 22-May-24 0:00 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg22-May-24 0:05
charlieg22-May-24 0:05 
AnswerRe: Okay, real C++ question - who makes use of the auto keyword? Pin
honey the codewitch13-Jun-24 12:43
mvahoney the codewitch13-Jun-24 12:43 
Questionmfc, vs2022 compatible? Pin
charlieg17-May-24 7:21
charlieg17-May-24 7:21 
AnswerRe: mfc, vs2022 compatible? Pin
Victor Nijegorodov17-May-24 7:39
Victor Nijegorodov17-May-24 7:39 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg17-May-24 7:56
charlieg17-May-24 7:56 
AnswerRe: mfc, vs2022 compatible? Pin
RedDk17-May-24 9:39
RedDk17-May-24 9:39 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg17-May-24 10:51
charlieg17-May-24 10:51 
GeneralRe: mfc, vs2022 compatible? Pin
RedDk17-May-24 11:11
RedDk17-May-24 11:11 
AnswerRe: mfc, vs2022 compatible? Pin
Maximilien21-May-24 5:31
Maximilien21-May-24 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.