On Monday 07 April 2003 11:38 am, Anders Johansson wrote:
On Monday 07 April 2003 09:34, Steven T. Hatton wrote:
The question remains. How big is an object? What does sizeof really mean?
An object is the size of its members. Plus all objects of a class share the methods of that class. Plus possible padding bytes (don't know about that one, but C sometimes does that for structs and unions, to align the data on word boundaries. I guess c++ does the same, but I don't know).
sizeof returns the size of the parameter. I think sizeof for an object returns the dynamic size of that instance. i.e. not counting the methods it shares with other objects of that class.
The reason I asked the question is that I'm not sure that every instance of a class will have the same size. If they don't, then how would the result of incrementing a pointer (ptr++ ) be calculated?
Also, what's the lifetime of a member variable of derived type when created as a side effect of implicit (automatic?) instantiation in a function call variable declaration?
If I understand the question, I would say from the time the function is called to the time the function returns.
The following code is kind of messy. It's intended to illustrate some of the
pointer issuees I'm dealing with. Notice that I never explicitly instantiate
tc.mc, neither in the constructor nor in any function. Nonetheless, I can
call mc.output(), thereby demonstrating the constructor had been called. I
intentionally omitted the initialization of int someNum to demonstrate that
it is not automatically initialized.
I do, however, initialize tc.mcPtr using:
this->mcPtr = new MemberClass(this);
That is, to demonstrate that I can use /this/ within a constructor. It also
demonstrates the design issue I was mulling over when I started posting to
the list. I'm not sure there is a way to accomplish a similar instantiation
of a member. IOW, I'm not sure I can explicitly call a constructor and pass
/this/ as an argument. I am sure that I don't know how to do so.
There are five files represented in the following.
testclass.h
testclass.cpp
memberclass.h
memberclass.cpp
main.cpp
Two classes are defined, TestClass and MemberClass.
/*
testclass.h
*/
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <iostream>
#include <string>
#include "memberclass.h"
using namespace std;
class TestClass {
public:
TestClass();
~TestClass();
void setOwnerMessage(char * ownerMessage="Owner Message");
void output();
void ownerOutput();
private:
string message;
string ownerMessage;
int someNum;
MemberClass mc;
MemberClass * mcPtr;
};
#endif
/********************************/
/*
testclass.cpp
*/
#include <iostream>
#include <string>
#include "testclass.h"
#include "memberclass.h"
using namespace std;
TestClass::TestClass(){
this->message = "I am of TestClass. Assigned in the constructor";
this->ownerMessage = "";
this->mcPtr = new MemberClass(this);
}
TestClass::~TestClass(){
}
void TestClass::setOwnerMessage(char * ownerMessage) {
this->ownerMessage = ownerMessage;
}
void TestClass::output() {
cout << this->message << endl;
cout << "TestClass.someNum=" << this->someNum << endl;
this->mc.output();
this->mcPtr->output();
}
void TestClass::ownerOutput() {
cout << "Owner Message=" << this->ownerMessage << endl;
}
/*******************************/
/*
memberclass.h
*/
#ifndef MEMBERCLASS_H
#define MEMBERCLASS_H
#include <iostream>
#include <string>
using namespace std;
class TestClass;
class MemberClass {
public:
MemberClass(TestClass * owner = 0);
~MemberClass();
void output();
private:
string message;
TestClass * owner;
};
#endif
/********************************/
/*
memberclass.cpp
*/
#include <iostream>
#include <string>
#include "memberclass.h"
#include "testclass.h"
using namespace std;
MemberClass::MemberClass(TestClass * owner){
this->message = "I am of MemberClass. Assigned in the constructor";
this->owner = owner;
}
MemberClass::~MemberClass(){
}
void MemberClass::output(){
cout << this->message << endl;
if(this->owner) {
this->owner->setOwnerMessage("I am the owner of a MemberClass");
this->owner->ownerOutput();
}
}
/***********************************/
/*
main.cpp
*/
#ifdef HAVE_CONFIG_H
#include