M
mathemagic
Hello All,
I apologise if this has been covered by an earlier post.
I just wanted to know how the functions of a class are tied to the
class.
ie, in C structs, since u can have only data members,
sizeof(some_C_struct) gives u the cumulative size of each of the
individual members plus whatever bytes were used for alignment padding
(the rules for which of course are different for Windows, Linux, 32-
bit, and 64-bit machines, but thats a different story).
Now say I have a class with some 10 instances, and the behaviour of
the class is defined by some functions.
Obviously, there is only one copy of the functions used by all the
objects.
But, whenever a function is called for a particular instance of a
class, the magical 'this' pointer is inserted as the first argument
and the function is hence called for that particular object, so,
voila, u get per-instance behaviour, but where is the 'this' pointer
stored in the first place? Why is it not part of the object? I first
thought that each object would at least store the function pointer for
the functions, but sizeof doesnt show this to be true. So, how are the
functions tied to each of the objects?
To help u understand where am coming from and where am getting at:
struct some_C_struct {
int i;
char c;
}struc;
so sizeof(struc) = 8 because of the first 4 bytes for the int, the
next byte for the char and the remaning 3 bytes of empty padding to
make it 8byte aligned.
Now,
class some_Cpp_class {
int i;
char c;
public:
some_Cpp_class() {
/*do something to initialize*/
}
some_other_member_function() {
}
}obj1;
Now, sizeof(obj1) for this too gives 8, assuming the same rules as
earlier. So where is the this pointer stored? How is it then tied to
this object? What about the function pointers? And anything else that
I might have missed out?
Thanks,
--Aditya.
I apologise if this has been covered by an earlier post.
I just wanted to know how the functions of a class are tied to the
class.
ie, in C structs, since u can have only data members,
sizeof(some_C_struct) gives u the cumulative size of each of the
individual members plus whatever bytes were used for alignment padding
(the rules for which of course are different for Windows, Linux, 32-
bit, and 64-bit machines, but thats a different story).
Now say I have a class with some 10 instances, and the behaviour of
the class is defined by some functions.
Obviously, there is only one copy of the functions used by all the
objects.
But, whenever a function is called for a particular instance of a
class, the magical 'this' pointer is inserted as the first argument
and the function is hence called for that particular object, so,
voila, u get per-instance behaviour, but where is the 'this' pointer
stored in the first place? Why is it not part of the object? I first
thought that each object would at least store the function pointer for
the functions, but sizeof doesnt show this to be true. So, how are the
functions tied to each of the objects?
To help u understand where am coming from and where am getting at:
struct some_C_struct {
int i;
char c;
}struc;
so sizeof(struc) = 8 because of the first 4 bytes for the int, the
next byte for the char and the remaning 3 bytes of empty padding to
make it 8byte aligned.
Now,
class some_Cpp_class {
int i;
char c;
public:
some_Cpp_class() {
/*do something to initialize*/
}
some_other_member_function() {
}
}obj1;
Now, sizeof(obj1) for this too gives 8, assuming the same rules as
earlier. So where is the this pointer stored? How is it then tied to
this object? What about the function pointers? And anything else that
I might have missed out?
Thanks,
--Aditya.