Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

d000hg

Operator overloading : sizeof() ??

This topic is 5625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can you overload the sizeof() operator? This would be most useful for classes that may have to allocate memory - here sizeof() won''t catch that.

Share this post


Link to post
Share on other sites
Advertisement
He means it won''t detect when member pointers point to allocated memory and add that to the size. Although I really don''t see why you''d want a sizeof to do that.

Share this post


Link to post
Share on other sites
class myclass
{
int *pointer;
.
.
.
};

Say I have something like this, maybe as a wrapper for n image/sound file or whatever. It would be handy to be able to get sizeof() to act on the memory allocated. Though like you say it''s not hard to write a SizeOf() method or maybe even _sizeof(). It''s just a niggle that irritates me!

Share this post


Link to post
Share on other sites
it would be easy enough to have the class keep track of how much memory it allocates within itself, add that to the sizeof for itself, and you have (should be??) the total memory usage of an instance of that class.

I say should be because I''ve heard the actual memory hit of a class is not always accurately represented by the sizeof operator. Anyone have more details on this?

Share this post


Link to post
Share on other sites
this one is in stroustrops faq. He says it''s because sizeof is used internally by the compiler. When you have a pointer, and you do ptr++, it will add sizeof(type) to ptr, instead of just increasing the pointer with one. If you could overload the sizeof operator you would fuck up these things.

My Site

Share this post


Link to post
Share on other sites
Do member functions increase the size of a class? Ie
struct struct FVF_VERTEX1
{
float x,y,z;
DWORD diffuse;
};
class FVF_VERTEX2
{
float x,y,z;
DWORD diffuse;
public:
SetPos(float x,float y,float z);
};
Are these identical in terms of memory alignment? I assume so as some people use D3DX classes for xyz vectors in a FVF structure without screwing it up.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!