Hi All,
Consider a following snippet
class exm
{
int *i;
public:
exm()
{
}
};
void main()
{
eam obj;
cout<<sizeof(obj); // o/p will be 4 bytes (ofcourse according to compiler.
Mine is MSVC)
};
similarly..
I understood that this pointer will be assigned with the address of the
object which comes as parameter in copy constructor. But to assign , "this"
pointer has to exist. That means , it must be declared (probably hidden) . So
the size of the class should carry the size of this hidden pointer also. Please
correct me. I understood that I am in wrong way. But Didn't understand where I
missed the track.
Thanks&Regards,
Gopi.k
"Victor A. Wagner Jr." <[EMAIL PROTECTED]> wrote:
Gopi Krishna Komanduri wrote:
> Hi Tamas Marki,
> I agreed for your explanation. But , consider a code for
> copy constructor or assignment operator overloading methods. Those
> will be provided irrespective of whether we create objects or not. Am
> I correct? If so , in side the code , the implementation wil be with
> "this" pointer itself na! .. Even if we think like the assignment
> for this pointer will be done by creation of object(passing &obj as
> hidden parameter and catching this address into this pointer and
> using it , ) even in this xcase also .. some memory will be occupied
> for formal parameters and that included the size of this pointer also na!
> Please correct me where I missed the track!
the object doesn't have a "hidden this pointer" calls to methods are
provided "this" as an argument
> Thanks&Regards,
> Gopi.k
>
> Tamas Marki <[EMAIL PROTECTED]> wrote:
> On 5/8/07, Gopi Krishna Komanduri <[EMAIL PROTECTED]>
> wrote:
>> Hi,
>> I know that size of empty class is 1 byte. But when we write a class
>> compiler will provide , default constructor , default destructor ,
>> copy constructor (shallow) , and one assignment operator overloading
>> functions whether the class is empty or not. So , in copy constructor
>> , and in assigment operator methods , the implementation wil use
>> "this" pointer. So the empty class should have one hidden this
>> pointer. and when we create object , the this pointer will start
>> pointing to the current object. So the size of empty class shuld be
>> atleast size of a pinter (2 bytes , but depends on compiler). Could
>> you please clarify!
>
> A pointer might be 2 bytes on your compiler (something tells me it's
> Turbo C++), but on modern systems it is 4 or 8 bytes.
> However, the size of the pointer is irrelevant in this case: the class
> does not have a pointer to itself, because you supply the this pointer
> implicitly when you call a member of an object.
> The size of the empty class is one bytes because the pointer needs to
> point somewhere, and if it would be 0 then one pointer could point to
> multiple instances which is not good.
> I hope it's more clear now.
>
GopiKrishna Komanduri
Software engineer
Covansys,
Chennai,
9884965748.
[EMAIL PROTECTED]
---------------------------------
Don't be flakey. Get Yahoo! Mail for Mobile and
always stay connected to friends.
[Non-text portions of this message have been removed]