Classes Query

closed account (3CXz8vqX)
Hi,

Just got to a bit that isn't explained in the tutorials and I was wondering if you wouldn't mind filling me in on why it is done this way.

Currently I'm looking at the initial class set up of Rectangle...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Rectangle
{
    int width;
    int length;

    public:
        void set_values ( int, int );
        int area ( void )
        {
            return ( width * length );
        }
} rect;

void Rectangle::set_values ( int a, int b )
{
    width = a;
    height = b;
}
...


Now...what was the point of setting a prototype? Why not just declare the constructor inside the class? (That 'is' a constructor right?). It would also make the code look neater since everything to do with the class is then inside the class instead of in a tangled mess all over the place?

What is the advantage of doing setting out a class as above? It's something I've seen to be an apparent standard and I haven't actually seen the constructor set up inside the class yet. >.>;

Thanks.
In some cases you need to have only the prototypes (such as the header of a library) but in most cases you will want to define all functions inline so that it makes it easier for your compiler to optimize the code.

It doesn't matter too much, usually it is just personal preference, but in some cases you have to declare the prototype only and provide the definition elsewhere. This usually comes into play with the PIMPL idiom and declaration order.
closed account (3CXz8vqX)
PIMPL?

Under what kind of circumstances might I have to declare only the prototype? Also, if inline is better (I assume you mean inside the class) then why isn't it done that way in the tutorials? Or are they trying to confuse us by just bringing in the scope identifier and not explaining why?
closed account (zb0S216C)
Ravenshade wrote:
"what was the point of setting a prototype?"

Prototypes are essential in some cases:

1) Consider this example:

1
2
3
4
5
6
7
8
9
int main( )
{
  X( );
}

void X( )
{
  // ...
}

This is an error. Since the compiler reads the file from the top to the bottom, the compiler cannot ensure that the call to "X( )" is correct until it sees the corresponding prototype. Therefore, the compiler will generate an error.

2) Prototypes allow a program to be divided into multiple files. In the traditional fashion, headers contain the prototypes (interfaces) while the translation unit contains the definitions.

Ravenshade wrote:
"Why not just declare the constructor inside the class?"

You could, but that would implicitly mark the function as "possibly in-line-able". In-line functions are not always respected by the compiler as functions marked "inline" serves only as a hint to the compiler that the function should be in-lined if the compiler agrees. Now, while in-lining is a good thing, it's also a bad thing. If all functions are in-lined by the compiler, you risk large performance loses and program size gains.

Ravenshade wrote:
"What is the advantage of doing setting out a class as above?"

None that I can think of. In larger classes, shoving all the definitions of member-functions inside the containing class only reduces readability.

Wazzak
Last edited on
Ravenshade wrote:
PIMPL?
First result on Google: http://c2.com/cgi/wiki?PimplIdiom

Ravenshade wrote:
Also, if inline is better (I assume you mean inside the class) then why isn't it done that way in the tutorials? Or are they trying to confuse us by just bringing in the scope identifier and not explaining why?
Again, it is just personal preference in many cases.

Ravenshade wrote:
inline [...] (I assume you mean inside the class)
Here is an example, both f and g are inline:
1
2
3
4
5
6
7
8
9
10
11
struct MyClass
{
    void f()
    {
    }
    void g();
};

inline void MyClass::g()
{
}
Last edited on
closed account (3CXz8vqX)
So the advantage for prototyping is really only there for larger programs so that they can be split up effectively and to increase readability.

Wazzak wrote:

You could, but that would implicitly mark the function as "possibly in-line-able". In-line functions are not always respected by the compiler as functions marked "inline" serves only as a hint to the compiler that the function should be in-lined if the compiler agrees. Now, while in-lining is a good thing, it's also a bad thing. If all functions are in-lined by the compiler, you risk large performance loses and program size gains.


Isn't that an advantage of doing it the other way with the prototype, since there's no risk doing it the other way. Why would I be risking the performance losses? I'm assuming you mean for larger files but I don't understand why it's any different.

@LB

Wait a second, why the... what's the point of the inline if you've actually prototyped it in the class anyway....O.o Why not just go the whole hog and display the function without the inline tag.
Last edited on
@Ravenhade because sometimes you need some definitions between MyClass and MyClass::g.

As for the problems with inlining, most compiler just completely ignore your request to inline or not inline something - they're really good at optimizing code better than you, so they often just make the best decision for you.
closed account (zb0S216C)
Ravenshade wrote:
"Why would I be risking the performance losses?"

People believe that in-lining increases performance because the function call overhead is removed. This is true to some degree. If a function is in-lined by the compiler and called very infrequently, there will be no significant performance increase; if a calling function's size is increased because of in-lined functions, the locality of reference is reduced, thereby reducing the effectiveness of the CPU's cache.

Ravenshade wrote:
"Why not just go the whole hog and display the function without the inline tag"

I know this was aimed at LB, but I couldn't help by respond. When a member function is defined outside the containing class, the compiler will not mark the function as in-line, but when the function is defined within the containing class, the compiler will mark the function as in-line. Though, LB's example shows that the programmer explicitly wanted the function to be in-lined if possible.

Wazzak
closed account (3CXz8vqX)
So much for, "Programmer knows best" hehehe.

Thank you both.
Topic archived. No new replies allowed.