473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 12156
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.