|
|
Nicolas Alvarez wrote:
> Darren New wrote:
>> I can pass you a void* and
>
> That's a C feature. Real C++ programmers don't use void*. So no, of course
> you have no RTTI on a void*.
OK. What am I doing wrong here, that I'm not getting the size of the actual
instance passed in?
#include <iostream>
class Alpha {
public:
int i;
Alpha() { i = 9; }
virtual ~Alpha() { }
virtual int yidda() { return 0; }
virtual int mysize() { return sizeof(*this); }
};
class Beta : public Alpha {
public:
long l0, l1, l2, l3, l4, l5, l6, l7;
Beta() { i = 10; }
virtual ~Beta() { }
virtual int yidda() { return 1; }
};
void show_size(Alpha & xyz) {
std::cout << sizeof(xyz) << std::endl;
std::cout << xyz.mysize << std::endl;
}
int main() {
Alpha alpha;
Beta beta;
show_size(alpha);
show_size(beta);
return 0;
}
Why does this print "8 8 8 8"? If there's RTTI, shouldn't I at least be able
to figure out how big my structure is without having to code a function to
calculate it for every function? Seriously, if you wanted to write whatever
structure "show_size" got passed out to a file, how would you do it? Would
you have to write a "return sizeof(*this)" in each and every class as a
virtual function? A function inherited by the parent into the child doesn't
know that "this" is pointing to a child type? (I tried adding more longs to
alpha and I started getting 32's, so it's not giving me the size of the
reference itself.)
If I copy the text of mysize() into Beta's class, mysize() returns 40 for
betas. That seems kind of ... not right, to me. I duplicate a virtual
function into a child class without changing it, and it gives different
answers than if I inherited it?
>> The traceback in your exceptions
>> can tell you what the values passed as arguments to the function on each
>> frame...
>
> Yes they can.
I wasn't aware C++ even defined tracebacks on exceptions. How does one
access that feature? The googles with the obvious words mostly talk about
python exceptions passing through C++, and the g++ exception.h file declares
exception as having a constructor, destructor, and a "what" field, but no
traceback. What should I google on to find the right invocation to get the
traceback out of an exception?
>> Hell, you can't even tell me how big an array is
> No, but you'll see me using std::vector, not arrays, in C++.
Yes, because you can't see how big an array is.
>> I know how it *won't* be implemented. It won't be implemented such that
>> references to local variables survive the end of the stack frame those
>> local variables are allocated in. That's enough.
>
> They never do,
Well, no, not in C++. That's kind of my point. In other languages with
lambdas, yes, they do. That's why it's called a closure. That's why I was
asking stupid questions about how C++ manages to do that. :-)
> And in my mind it wouldn't be a stack if it kept
> alive after 'popped' :D
Well, yes. That's exactly why I'm asking "Wow, how does C++ do that??"
Because, like, that's what "lambda" means, and C++ can't do it, so I'm
trying to figure out what amazing mechanism they used.
--
Darren New, San Diego CA, USA (PST)
My fortune cookie said, "You will soon be
unable to read this, even at arm's length."
Post a reply to this message
|
|