C++: prototype declarations

Discussion in 'C++' started by Web Developer, Aug 11, 2003.

  1. Hi,

    Comming from Java, it seems that prototype declarations are like abstract
    methods. I have not read classes in C++ yet, but prototype declarations are
    a strange concept. Any comments appreciated.


    WD



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
     
    Web Developer, Aug 11, 2003
    #1
    1. Advertising

  2. Re: prototype declarations


    > > Hi,
    > >
    > > Comming from Java, it seems that prototype declarations are like

    abstract
    > > methods. I have not read classes in C++ yet, but prototype declarations

    > are
    > > a strange concept. Any comments appreciated.
    > >
    > >
    > > WD
    > >

    >
    > Prototypes are not like abstract methods in any way I can see.


    I was referring to the fact that abstract methods in Java do not have a
    method body, just like a prototype declaration in C++.


    > Prototypes are just a way to tell the compiler what a function or method
    > signature is without actually defining the function or method.


    Right. But why you need to do this? reading on...


    > Java has a completely different compilation model from C++. In Java the
    > compiler looks up a method signature when a method is called. It can do

    this
    > because Java defines rules on where a method can be defined.


    But in more detail, hows does the compiler in Java look up a method that is
    called? I know the use of the import statement in Java allows us to avoid
    fully qualifying data fields and methods of a class that is imported. This
    is becuase it puts the file in a local folder? and thats how it knows where
    to find it??


    > C++ on the other hand uses a model of separate compilation. This requires

    the
    > programmer to provide a declaration of any method before any call of that
    > method.


    Separate from what? I know the linker theory does in here somewhere as well.
    More detail perhaps?


    > void f(); // prototype
    >
    > int main()
    > {
    > f();
    > }
    >
    > void f()
    > {
    > }
    >
    > Without seeing the prototype the compiler would not know anything about f
    > when it compiled main, since its definitions occurs after main. Without

    the
    > prototype you would get an 'undefined f' error message.



    Nice example.


    Regards,
    WD


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
     
    Web Developer, Aug 11, 2003
    #2
    1. Advertising

  3. Re: prototype declarations

    >
    >
    > > C++ on the other hand uses a model of separate compilation. This

    requires
    > the
    > > programmer to provide a declaration of any method before any call of

    that
    > > method.

    >
    > Separate from what? I know the linker theory does in here somewhere as

    well.
    > More detail perhaps?


    Seperate from other compilations. So when you compile a.cpp, the compiler
    know nothing at all about the compilation of b.cpp. If a.cpp calls a
    function in b.cpp you have to tell the compiler that function is defined
    elsewhere, hence function prototypes.

    On the other hand with Java and import statement I believe the compiler will
    actually go and look up the imported classes and compile them if necessary
    (not sure about this, not an expert in Java). Certainly nothing like that
    happens in C++.

    Linking is the stage when the seperate compilations are brought together.
    The linker checks that everything that needs to be defined is defined (and
    defined only once).

    john
     
    John Harrison, Aug 11, 2003
    #3
  4. Re: prototype declarations

    On Mon, 11 Aug 2003 17:09:05 +0930, Web Developer wrote:

    > I was referring to the fact that abstract methods in Java do not have a
    > method body, just like a prototype declaration in C++.


    It would look like:


    // foo.h
    class Foo {
    virtual int bar() = 0; // bar() is abstract
    };


    // baz.h
    class Baz {
    int bar(); // meaning Baz Implements bar()
    };

    // baz.cpp
    int Baz::bar() {
    do_something();
    return value_of_something();
    }


    Actually in this case Foo would be an interface since it's only abstract
    methods.


    hth
    NPV
     
    Nils Petter Vaskinn, Aug 11, 2003
    #4
  5. Re: prototype declarations

    Web Developer wrote:
    >
    > I was referring to the fact that abstract methods in Java do not have a
    > method body, just like a prototype declaration in C++.


    There is no such thing as a "prototype declaration". There are
    prototypes. A prototype is also a declaration of a function. The
    function still must have a body. The prototype allows you to call a
    function that has not been defined in the compilation unit.

    >
    >>Prototypes are just a way to tell the compiler what a function or method
    >>signature is without actually defining the function or method.

    >
    >
    > Right. But why you need to do this? reading on...
    >


    The prototype is how the compiler knows how to call the function.
    Without it, the compiler would not know things like how many arguments,
    what their types are, and what the return type is.

    >
    > But in more detail, hows does the compiler in Java look up a method that is
    > called? I know the use of the import statement in Java allows us to avoid
    > fully qualifying data fields and methods of a class that is imported. This
    > is becuase it puts the file in a local folder? and thats how it knows where
    > to find it??


    Off topic and I have no idea.

    >
    > Separate from what? I know the linker theory does in here somewhere as well.
    > More detail perhaps?


    Separate compilation means that you can compile different source files
    at different times and leave it up to the linker to figure out how to
    generate the final executable. So if I have a huge project, and I modify
    one source file, I recompile only that one source file then link. This
    saves a lot of time. But it also means that the compiler may not have
    access to function definitions for functions that my source code calls.
    Therefore it needs prototypes for those functions in order to know how
    to generate code that calls them.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 11, 2003
    #5
  6. On Mon, 11 Aug 2003 15:56:10 +0930, "Web Developer" <>
    wrote:

    >Hi,
    >
    >Comming from Java, it seems that prototype declarations are like abstract
    >methods. I have not read classes in C++ yet, but prototype declarations are
    >a strange concept. Any comments appreciated.


    Unlike Java, C++ allows you to split the specification of a class into two
    pieces: its prototype declaration, and its implementation. i.e.:

    class A
    {
    public:
    void foo() {
    frobnicate();
    }

    void bar() {
    refrobnicate();
    }

    protected:
    int baz() {
    return alakazam();
    }
    };

    is equivalent to:

    // Interface description -- normally goes in .h file
    class A
    {
    public:
    void foo();
    void bar();
    protected:
    int baz();
    };

    // Implementation -- normally goes in .cpp file
    void A::foo() {
    frobnicate();
    }

    void A::bar() {
    refrobnicate();
    }

    int A::baz() {
    return alakazam();
    }

    Although the preceding two blocks of code are essentially equivalent, there is
    a significant advantage of splitting the two blocks of code into its prototype
    declaration (typically saved in a "header" (normally *.h) file) and its
    implementation (typically saved in a *.cpp file)...

    Unlike C++, Java independently compiles each class (*.java) into its own
    "object" file (*.class)--other classes that need to refer to a class will
    examine the .class file to obtain the interface information, including the
    names and types of each method. (Please correct me if I'm wrong--I am not a
    Java expert by any means).

    C++, however, relies on textual processing to evaluate the interface to a
    class. Since the interface to each class must be declared before it is used,
    each translation unit (.cpp file) that uses class A must describe the
    interface to class A before using it. This is typically done by #include-ing a
    common file to avoid replication of code.

    However, it is an error to declare the _implementation_ of a method more than
    once (your linker will complain about this), so the first code example cannot
    be the file that other .cpp files include.

    Instead, each file must include just enough information to completely describe
    the class interface without supplying its implementation. Thus the interface
    prototype goes into the .h file (which other .cpp files can freely include),
    while the implementation is placed in a .cpp file, to be compiled only once
    during the build.

    Hope that helps. My apologies for the various inaccuracies that I have no
    doubt inserted into the text above.
     
    Dave Rahardja, Aug 12, 2003
    #6
    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. deep
    Replies:
    0
    Views:
    484
  2. June Lee
    Replies:
    2
    Views:
    809
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    198
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    271
  5. javascript fish
    Replies:
    0
    Views:
    175
    javascript fish
    Oct 11, 2008
Loading...

Share This Page