Inheritance and pointers

Started by
7 comments, last by chairthrower 16 years, 2 months ago
Hey I have a question for you :) I think I'll explain this in the code

class Base{
int data;
};
class Child : Base{
int childdata;
};

// I declared these and as far as I know child costs a memory of (base + child) //bytes and the first (base) of bytes contain the data from the mother class and
//the rest is the child class I have..

void myfunction(Base& foo)
{
foo->data = 50;
};


//When I send a child class to this function, it takes the first (base) bytes of
//the child class and there are no problems at all..
// but what if I ....

class Base{
int data;
};
class Base2{
int data2;
};

class child2 : Base , Base2{
int childdata2;
}

void myfunction(Base2& foo)
{
foo->data2 = 50;
};

//I don't know if this code works but I think it does, my question is; How does 
// c++ finds the part of the child2 that is actually Base2, if it took first 
// Base2 bytes, it would come out Base. Because in the memory they stay 
// like (Base+Base2+child2) so is there some kind of search algorithm? or what


if you won't understand something, dont hesitate to ask and thanks
Advertisement
The child2 data is in memory as:
Base1 data
Base2 data
Child2 data

When you pass a Child2 type to a function that expects a Base2, the compiler takes the address of the Base2 type from the Child2 type (Which it knows as address + size of Base1 data), and passes that to the function.

If you have a pointer (or reference) to a Child2, the pointer actually points to the start of the Base1 data, but the compiler knows the variable is a Child2 type, so it can access its data because it knows the size of Base1 and Base2.

Sorry if that doesn't make sense, I'm rubbish at explaining things [smile]
Quote:Original post by Eralp
Hey I have a question for you :) I think I'll explain this in the code

*** Source Snippet Removed ***

if you won't understand something, dont hesitate to ask and thanks


First, that should be foo.data = 50, not foo->data = 50, based on the fact that you passed by reference. Secondly and more important is I think you're expecting polymorphic behavior, but it doesn't work like that for data members. You need a virtual function that is overridden in the derived class if you want polymorphic behavior.
No no it made sense, I got it.I just can predict what is going on between the ram cpu and c++ and can't know it without the help of others :).

And two more questions.. How much bytes take a function in the memory?(for example an int function will take as much as an int variable? if so what's with a void function)*

as last :) how much does the "class header" take(I think the pointers to the "mother classes" and more data that I dont know is stored there)

I am not struggling for using the lowest memory I can, I just want to know how the data is actually stored "beyond" our view :)


*no variables will be initialiazed in the functions
Quote:Original post by Rydinare
Quote:Original post by Eralp
Hey I have a question for you :) I think I'll explain this in the code

*** Source Snippet Removed ***

if you won't understand something, dont hesitate to ask and thanks


First, that should be foo.data = 50, not foo->data = 50, based on the fact that you passed by reference. Secondly and more important is I think you're expecting polymorphic behavior, but it doesn't work like that for data members. You need a virtual function that is overridden in the derived class if you want polymorphic behavior.


I'm inexperienced at seperating the * and & marks, my bad.
I know about the virtual functions but I didn't expect that I just want the basic inheritance for asking this question(did I do something wrong? huh).But thanks anyway
This might answer some of your questions.
Quote:Original post by Eralp
Quote:Original post by Rydinare
Quote:Original post by Eralp
Hey I have a question for you :) I think I'll explain this in the code

*** Source Snippet Removed ***

if you won't understand something, dont hesitate to ask and thanks


First, that should be foo.data = 50, not foo->data = 50, based on the fact that you passed by reference. Secondly and more important is I think you're expecting polymorphic behavior, but it doesn't work like that for data members. You need a virtual function that is overridden in the derived class if you want polymorphic behavior.


I'm inexperienced at seperating the * and & marks, my bad.
I know about the virtual functions but I didn't expect that I just want the basic inheritance for asking this question(did I do something wrong? huh).But thanks anyway


You question wasn't very clear. Perhaps I should've asked you to elaborate. Please elaborate?

Edit: Upon further review, I think you might be asking about memory layouts. Frankly, I took a quick glance over your code and quickly skimmed the comments, so I missed the fact that your question was somewhat hidden in there. My apologies.
Quote:Original post by SiCrane
This might answer some of your questions.


This is a great resource!

I got my "curiosy" fed for now thanks to all that wanna help

Quote:How much bytes take a function in the memory


It really depends - but its safe to say that in any stack based language it requires at least the callers return address so that the function can return. this also has the consequence that infinite recursion will eventually run out of memory.

note that even if there are no arguments in the function signature and you dont declare variables in the function the compiler is free to 'spill' temporary results of calculations to the stack frame. also when the function is calling another function the registers will be spilled to stack to protect them and free them up to the callee.

you can get some sense of the size of the stack frame like this by just taking
the addresses of variables on the stack.

void func2( unsigned char *x) {        unsigned char x0;         cout << (x - &x0) << endl;      }void func1(){        unsigned char x;         func2( &x); };main(){        func1(); }


for me it says 16 bytes for which i think i need to subtract 4 bytes for the variable (including padding) to get the simple stack set up size.

note though with optimisation and inlining it only uses 1 byte on my compiler since the second function is never called by using the stack.

This topic is closed to new replies.

Advertisement