J
JHenstay
I have some questions regarding memory and C++ Objects. For these
questions, I'll use the following sample for explanation sake:
------------------------------------
class CBase
{
public:
int iGetA() { return iVarA; }
void vSetA(int iNewA) { iVarA = iNewA; }
private:
int iVarA;
};
class CTierOne : public CBase
{
public:
int iGetB() { return iVarB; }
void vSetB(int iNewB) { iVarB = iNewB; }
private:
int iVarB;
};
int main(void)
{
CBase* myBase = new CBase; // LINE 1
CTierOne* myTier = new CTierOne; // LINE 2
// Relevant code here
delete myBase;
delete myTier;
}
------------------------------------
Q1) In LINE 1 the 'new' keyword allocates memory and instantiates the
class CBase as an object, myBase. The memory that is allocated, what
is actually in there? Is it just allocating memory for the member
variable iVarA or is it allocating space for the executable code
contained in the iGetA() and iSetA methods? Or is it allocating memory
for the variable iVarA and 2 pointers to the executable code for the 2
methods, iGetA() and iSetA()?
Q2) In LINE 2 when the CTierOne class is instantiated as the myTier
object, how is the memory arranged as far as contiguous storage? Is a
block of memory set aside and the first part of the memory contains
all of the data for CBase and the second all of the data for CTier?
Q3) Is there an easy way to do a memcpy of all of the variable data in
an object into a byte array so that it can be "offloaded" (either
written to a file or a database) and at a later time, re-instantiate
the same class as another object and copy that data back into the
object?
If anyone knows the answer to any of these questions, I would greatly
appreciate it!
Thanks!
-JH
questions, I'll use the following sample for explanation sake:
------------------------------------
class CBase
{
public:
int iGetA() { return iVarA; }
void vSetA(int iNewA) { iVarA = iNewA; }
private:
int iVarA;
};
class CTierOne : public CBase
{
public:
int iGetB() { return iVarB; }
void vSetB(int iNewB) { iVarB = iNewB; }
private:
int iVarB;
};
int main(void)
{
CBase* myBase = new CBase; // LINE 1
CTierOne* myTier = new CTierOne; // LINE 2
// Relevant code here
delete myBase;
delete myTier;
}
------------------------------------
Q1) In LINE 1 the 'new' keyword allocates memory and instantiates the
class CBase as an object, myBase. The memory that is allocated, what
is actually in there? Is it just allocating memory for the member
variable iVarA or is it allocating space for the executable code
contained in the iGetA() and iSetA methods? Or is it allocating memory
for the variable iVarA and 2 pointers to the executable code for the 2
methods, iGetA() and iSetA()?
Q2) In LINE 2 when the CTierOne class is instantiated as the myTier
object, how is the memory arranged as far as contiguous storage? Is a
block of memory set aside and the first part of the memory contains
all of the data for CBase and the second all of the data for CTier?
Q3) Is there an easy way to do a memcpy of all of the variable data in
an object into a byte array so that it can be "offloaded" (either
written to a file or a database) and at a later time, re-instantiate
the same class as another object and copy that data back into the
object?
If anyone knows the answer to any of these questions, I would greatly
appreciate it!
Thanks!
-JH