Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ classes

Name: scd 2009-01-21 16:54

What benefit does it have, if I write the prototypes of the functions my class will have and then later write the actual functions?

Like for example:

class foo
{
   public:
   int value;
   void getValue();
};

void foo::getValue()
{
  return value;
}


Couldn't I simply define the function inside the class?
Like:

class foo
{
  public:
  int value;
  void getValue()
  {
     return value;
  }
};

Name: Anonymous 2009-01-21 16:57

You separate the implementation from the specification. Trivial methods are best defined inside the class as they are implicitly inlined, otherwise no, because of that reason.

Name: Anonymous 2009-01-21 16:59

>>2

That does seem logical. But what difference does it actually make if I define the function outside of the class? If I want another implementation it's not a difference where I have to change the function, inside the class or some lines further down the code.
Or is it?

Name: Anonymous 2009-01-21 17:02

LOL C THREAD OVER

Name: Anonymous 2009-01-21 17:03

>>3
If you define the method inside the class, then it's inline, which means that whenever you use the method, the code is copied and inserted into the executable again. This is fine, in fact preferred, for small methods, because it avoids the overhead of truly calling the method, but if you use the method a lot, your executable will bloat.

Name: Anonymous 2009-01-21 17:03

It makes no difference really, it'll just be much clearer as there would be a lot of code in the class.

Name: scd 2009-01-21 17:07

>>5

Ah, sorry, I didn't read >>2 right. Somehow overlooked the inlined part. I understand now, thanks.

Name: Anonymous 2009-01-21 17:12

If foo::getValue() used bar::getValue() and bar::getValue() used foo::getValue(), then you could do

class foo
{
   public:
   int value;
   void getValue();
};

class bar
{
   public:
   int value;
   void getValue();
};

And then define the functions.

Name: scd 2009-01-21 17:14

>>8

True. Only works if the variable names are the same, though.

Name: Anonymous 2009-01-21 18:06

GAY

Name: Anonymous 2009-01-22 1:27

>>8
How would one define the functions then?

void foo::getValue()

would define it only for foo, wouldn't it? while bar::getValue() would define it for bar only.

Name: Anonymous 2009-01-22 1:34

>>11
I'm rusty on my sepples, so correct me if I'm wrong, but I meant that you could prototype them in the class so when you define the foo::getValue() you could use the bar::getValue() and when define the bar::getValue() you could use the foo::getValue() for compilers that don't look ahead.

Name: Anonymous 2009-01-22 1:40

>>12
You mean something along the lines of

void foo::getValue()
{
  return value;
}

void bar::getValue()
{
  foo::getValue();
}


?

Name: Anonymous 2009-01-22 1:44

>>3
If you want another implementation, you subclass. If your subclass can't get by using just the public, published interface, you're doing it wrong.

Another big, practical issue is that by putting all the code in the header all the clients of that class (ie. everyone who includes the header) must be recompiled if you make any changes in the method implementations. Compiling C++ is slow enough without introducing fuckwittery like that.

Name: C3PO 2009-01-22 1:45

>>10
GAYNESS

Name: Anonymous 2009-01-22 1:47

>>13
Yeah, that's what I was referring to.

Name: Anonymous 2010-12-06 9:39

Back to /b/, ``GNAA Faggot''

Don't change these.
Name: Email:
Entire Thread Thread List