Author Topic: C++ newbie needs help  (Read 1624 times)

0 Members and 1 Guest are viewing this topic.

Offline Goober5000

  • HLP Loremaster
  • 214
    • Goober5000 Productions
Well, neither does that.  I edited my post to display the entire function; it's part of a header file I wrote in high school, when we had to use the apstring class, but it's valid for regular strings also.

 

Offline Kazan

  • PCS2 Wizard
  • 212
  • Soul lives in the Mountains
    • http://alliance.sourceforge.net
lberator.. nobody told you to do anything with pointers
PCS2 2.0.3 | POF CS2 wiki page | Important PCS2 Threads | PCS2 Mantis

"The Mountains are calling, and I must go" - John Muir

 
Quote
Originally posted by mikhael
Yeah, method declaration inside the class declaration is the same as declaring them outside the class declaration with an inline keyword.

As Kaz says, this results in your method's code getting duplicated EVERY SINGLE PLACE the method is called. For anything more than a one liner (like the simplest accessor or maybe a incrementor), this is insanity: you'll end up with ridiculousl bloated executables. Its just not an acceptable trade off, since you're only saving on a vtable lookup.


Could sound like nitpicking, but to be accurate, function inlinements are just hints to the compiler. It doesn't force any behaviour, any half-decent compiler doesn't even try to inline to any of the functions beside CheckBal() since the other functions surely get bounced back by cost/benefit analysis.

But anyway, it's true that method declaring should be done outside, even if only because it being clearer.

 

Offline mikhael

  • Back to skool
  • 211
  • Fnord!
    • http://www.google.com/search?q=404error.com
It is nitpicking, yes, but accurate nitpicking.

So far as I know, most compilers will follow an 'inline' keyword unless told not to for one reason or another. I'd have to go hit up the gcc man page (and reinstall VC++ to read its docs) to see for sure, mind you.

Either way, there's way too many benefits for defining methods outside the class description. I shouldn't have used the word 'declaring' before, since method declaration still has to take place within the class definition. Blah blah blah. Semantics.
[I am not really here. This post is entirely a figment of your imagination.]

 

Offline Kazan

  • PCS2 Wizard
  • 212
  • Soul lives in the Mountains
    • http://alliance.sourceforge.net
there are appropriate times to both declair and define a function in the class header though liberator - don't think we're espousing a universal rule

one line accessors, etc are appropriate like

Code: [Select]

class SomeClass
{
      private:
           int SomeData;
           ...

      public:
          SomeClass(...);
          ~SomeClass();

          void SomeOperation(...);

           int GetSomeData()
                { return SomeData; }

}
PCS2 2.0.3 | POF CS2 wiki page | Important PCS2 Threads | PCS2 Mantis

"The Mountains are calling, and I must go" - John Muir