last day (15 days later) » 

11:56 PM
1
A: How is inheritance implemented at the memory level?

cHaoIn your example here, there's no copying of anything. Generally an object doesn't know what class it's in at runtime -- what happens is, when the program is compiled, the compiler says "hey, this variable is of type C, let's see if there's a C::print(). No, ok, how about A::print()? Yes? Ok, ...

 
"Generally an object doesn't know what class it's in at runtime" an instance of a dynamic class certainly knows its type at runtime!
 
@curiousguy: In C++, classes don't even exist at runtime. It's all just bytes, pointers and smoke. And what are these "dynamic classes" you speak of, anyway?
 
"classes don't even exist at runtime" No true, typeid(*this).name() certainly does exist at runtime "And what are these "dynamic classes" you speak of, anyway?" classes with a vptr (either for virtual functions or for virtual base classes)
 
@curiousguy: Yeah, typeid's can exist at runtime. Classes, however, don't. The first C++ compiler started as a translator that turned C++ into C -- which, of course, doesn't contain any built-in notion of "classes". Objects became structs, member functions became global functions with slightly mangled names, and virtual functions became vtable entries. To a huge degree, though they now generate machine code directly, they still works this way -- CPUs rarely know or care about "classes" either. Like i said, it's all bytes and pointers...plus a little sleight of hand and misdirection.
As for how RTTI works when the classes no longer actually exist, i presume there's a pointer to a preexisting (compiled-in) type_info in the vtable (probably the first entry). Seems the simplest thing that'd work.
 
"The first C++ compiler started as a translator that turned C++ into C" yes, the cfront compiler - I know this stuff. "which, of course, doesn't contain any built-in notion of "classes"." of course. Most compilers compile to assembly, which don't have a notion of class either. That is the whole point of the compiler, or translator: to translate a programming language into another, simpler, lower level, with less concepts. C and assembly do not have any concept of vptr, of vtable either. And assembly does not know about C or C++ objects... obviously.
When you are saying that classes do not exist at runtime, are you also saying that objects do not exist at runtime? That anything that doesn't exist in the C language doesn't exist at runtime? It now seems to me that your previous statement "Classes don't exist at runtime" is empty (it says nothing). It is inherently tautological/trivial: you could replace "class" with anything, and even replace "C++" by any other language. If you can make the statement not inherently tautological, i.e. if you can name some C++ concept that still exists at runtime, please enlighten me!
 
11:56 PM
@curiousguy: THAT'S THE FREAKING POINT! Classes, objects, etc (anything other than bytes, certain-sized integers, pointers, floating-point numbers, and functions) are at a higher level of abstraction, and needs to be deconstructed into native things in order to translate it. And my point is that in the case of C++, any part of that abstraction that's not absolutely needed (like, say, anything saying A::function only works with As) is simply tossed out and not translated at all. RTTI is basically a pointer to a name, nothing more. Its only use is comparison with other names.
"Classes don't exist at runtime" is not tautological; in languages like C# and Java, a "class" is an actual thing -- it's there, it can be gotten, looked at, and even used to create new objects at will. You're basically running at a higher level of abstraction in those languages; the runtime's job there is to translate the higher-level stuff to lower-level stuff on the fly. You don't have that with C++ -- that higher-level stuff is tossed aside at compile time, and the runtime doesn't really translate anything -- it just calls a few functions and provides you some other functions.
 
You know, the JVM is not everything; Java can be compiled to assembly too. The dynamic type of an object can be "gotten, looked at" in C++ too with typeid. "that higher-level stuff is tossed aside at compile time" again, that is an empty statement "the runtime doesn't really translate anything" what about dynamic link? lazy dynamic link? explicit module loading (dlopen, dlclose, dlsym)?
@cHao "any part of that abstraction that's not absolutely needed (like, say, anything saying A::function only works with As) is simply tossed out and not translated at all." anything that is not needed is not translated, in any language. Again, this is an empty statement.
 
You can't get a "type" with typeid. At best, you get what amounts to the name of a type. And compare it to other names. The actual type itself is gone. There is no such thing. There's no metadata. There's no way to know anything about the type.
 
@cHao "RTTI is basically a pointer to a name, nothing more. Its only use is comparison with other names." The comparison between std::typeinfo with operator== is not about type names, it is about types.
 
In Java, that metadata is there. Same with C#.
 
Two types compare equal iff they are the same type.
@cHao You don't compare names.
 
11:59 PM
Yeah. And that's the only thing you can do with a type_info.
 
Two types may happen to have the same name, but still be distinct types.
 
I don't mean "name" as in textual name. I mean like an id.
 

  last day (15 days later) »