Theoretical wondering about scope in CPP-files

Discussion in 'C++' started by The Cool Giraffe, Apr 18, 2007.

  1. I was told to use the following scheme.

    // Some.h
    class Some
    { bool doSome ();};

    // Some.cpp
    #include "Some.h"
    bool Some::doSome () {return true;}

    Why do i need to specify the returned type? The scope
    Some:: i get, the name of the method too, parameter
    type and number - sure thing. But why, oh why, does one
    need to specify that doSome returns a bool when
    implementing it in the cpp-file? It's unambiguous, isn't
    it? Please advise.

    --
    Vänligen Kerstin Viltersten
    (The Cool Giraffe)
     
    The Cool Giraffe, Apr 18, 2007
    #1
    1. Advertising

  2. The Cool Giraffe wrote:
    > I was told to use the following scheme.
    >
    > // Some.h
    > class Some
    > { bool doSome ();};
    >
    > // Some.cpp
    > #include "Some.h"
    > bool Some::doSome () {return true;}
    >
    > Why do i need to specify the returned type?


    The short answer is: because the Standard requires you to.

    > The scope
    > Some:: i get, the name of the method too, parameter
    > type and number - sure thing. But why, oh why, does one
    > need to specify that doSome returns a bool when
    > implementing it in the cpp-file? It's unambiguous, isn't
    > it? Please advise.


    A function definition begins with a declaration followed by
    a body. The declaration begins with some specifiers followed
    by a return value type. That's just a requirement of the
    language. The rule of its syntax. It makes it simple, yet
    efficient and reduces errors.

    Imagine that there would be no need to specify the return value
    type. How the hell should I know what type it returns when
    looking at the function definition in the 'cpp' file? I would
    have to look it up in the header, right? Extra work. Now I
    don't have to, the definition repeats it for me.

    What would happen if somebody changed the return value type in
    the class definition without ever looking at the implementation
    of the function? Suddenly all kinds of problems can arise due
    to some conversions or whatever. Now, when the types are not
    the same, I get a diagnostic message telling me that, and I
    have to go to the definition and change the type there as well.
    That's extra protection. That's A GOOD THING(tm).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #2
    1. Advertising

  3. The Cool Giraffe

    Zeppe Guest

    The Cool Giraffe wrote:

    > Why do i need to specify the returned type? The scope
    > Some:: i get, the name of the method too, parameter
    > type and number - sure thing. But why, oh why, does one
    > need to specify that doSome returns a bool when
    > implementing it in the cpp-file? It's unambiguous, isn't
    > it? Please advise.
    >


    In addition to what Victor wrote, please consider that at the beginning
    there was the C language, in which there where no classes but only
    functions. Any function needs to be defined - that is, have the
    declaration and the implementation - at some point in the program, and
    this declaration tells everything about the function, because it's the
    minimum requirement for a function to be used. Eventually, you can also
    spread a little bit of additional definition of the same function,
    reporting just the name, return type and argument, to enable other
    translation units to use it.

    Now, should the member functions in C++ introduce some sort of different
    syntax just because in that specific case the function definition
    inside of the class definition is mandatory? It would have been rather
    stupid, additionally complex, error prone, and so on. Just to save 8
    bytes in average? ^^

    Regards,

    Zeppe
     
    Zeppe, Apr 19, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    4
    Views:
    520
    Jon A. Cruz
    Feb 11, 2004
  2. R
    Replies:
    2
    Views:
    397
    Esmond Pitt
    Mar 15, 2005
  3. Richard

    theoretical "hidden images"

    Richard, Oct 8, 2003, in forum: HTML
    Replies:
    4
    Views:
    488
    Richard
    Oct 8, 2003
  4. DrUg13
    Replies:
    1
    Views:
    482
    DrUg13
    Feb 10, 2004
  5. Materialised

    Theoretical Problem

    Materialised, Nov 21, 2003, in forum: C Programming
    Replies:
    16
    Views:
    539
Loading...

Share This Page