472,782 Members | 1,156 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

C++: 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

---
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
Jul 19 '05 #1
5 12132
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
Jul 19 '05 #2
>
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
Jul 19 '05 #3
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
Jul 19 '05 #4
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.

Jul 19 '05 #5
On Mon, 11 Aug 2003 15:56:10 +0930, "Web Developer" <no****@hotmail.com>
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.

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Harrison | last post by:
I've seen cases before where it was intended to write a variable declaration but a function prototype was written instead (e.g. X x();) but I've come accross a new version of this (new to me at...
8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
14
by: Mantorok Redgormor | last post by:
Would this by any chance invoke undefined behavior? extern int printf(const char *, ...); int main(void) { printf("Hello\n"); return 0; } That is, providing my own printf prototype would...
21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
23
by: Allin Cottrell | last post by:
Thomas Heinz wrote (in re. gcc compilation of this erroneous program): $ cat test.c int f(int); int f(); int f() {return 0;} int main (void) { return 0; }
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
2
by: jonnys5k | last post by:
I wanted to add an object as a prototype to separate my methods more nicely, however, I ran into a couple of problems. Apart from the obvious "scope" issues I found that any instances of my class...
2
by: jaysome | last post by:
While looking at the source code for gcc today, I noticed that a prototype for main() was declared. From gcc.c: extern int main (int, const char **); int main (int argc, const char **argv) {...
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.