why define functions outside a class
hi,
i''m going through my c++ book and have come to where the author defines a member function outside a class and refers to it through the scope resolution...
may be i have to read further but could i know why i''d do something like this i.e advantages and disadvantages.
thanx for helping out.
b
remember what you see to see what you rememberb
sounds sort of like a static function.
from what i understand (no guru here), you can call that method even when no class of the type it's associated with is instantiated.
ie: (i think, correct me if this is wrong)
hopefully that gives you some idea about static stuff
[edited by - Irrelevant on March 20, 2003 8:17:09 AM]
from what i understand (no guru here), you can call that method even when no class of the type it's associated with is instantiated.
ie: (i think, correct me if this is wrong)
classCThingy{ static unsigned int iInstances; /* only one copy of this var for all objects of this type */ CThingy(){} /* make these private so no one can create instances outside the Create and Delete methods below */ ~CThingy(){}public: static int CreateInstance(CThingy *pThingy); static int DeleteInstance(CThingy *pThingy);};in source:unsigned int CThingy::iInstances = 0; /* this value is for all CThingy's remember */int CThingy::CreateInstance(CThingy *pThingy){ if(iInstances > 0) // already an instance? return 0; else{ pThingy = new Thingy; iInstances++; // increment instance counter return 1; }}int CThingy::DeleteInstance(CThingy *pThingy){ if(pThingy && iInstances > 0){ /* valid pointer and there is an instance to delete* delete pThingy; // delete it iInstances--; // decrement instance count return 1; } else return 0;}
hopefully that gives you some idea about static stuff
[edited by - Irrelevant on March 20, 2003 8:17:09 AM]
quote: Original post by boomji
may be i have to read further but could i know why i''d do something like this i.e advantages and disadvantages.
It allows you to separate the implementation from the interface.
Oh, and please don''t cross-post.
Like SabreMan said, it allows you to separate the declaration from the implementation. Also, I believe that a function declared inside the class body is implicitly declared inline, so separating them would appear to be the only way to not declare a member function inline ...
saber man:sorry for cross posting.and thanks for the answer.
irreleveant:thanx but my head started spinning when i read your reply ...i still have to reach at what you were trying to explain.thanx for your time.I appreciate.
miserable:Ahhhhh... like i''m a 2 year old.Just what i needed today.So i cant afford to put every thing inline yes yes i c .
again thanx ya all.With diffrent points of view i learn more and get a better understaning of whats happenning.
thanx
b
remember what you see to see what you rememberb
I heard about it too:
Is it right that a method defined in the class body is always inline?
Is it right that a method defined in the class body is always inline?
quote: Original post by boomji
miserable:Ahhhhh... like i''m a 2 year old.Just what i needed today.So i cant afford to put every thing inline yes yes i c .
Sure you can. It''s a bad idea, though, because it bloats your code, performance gains are negligible except for short functions that are called very frequently, and it may make compile time longer when you put a lot of code in the headers (and modify it).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement