473,895 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12192
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_someth ing();
}
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
4952
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 least) and I want to check I've understood it correctly. The code I wrote was. std::istringstream buf("abc"); std::vector<char> vec(std::istream_iterator<char>(buf), std::istream_iterator<char>());
8
3761
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 bind function. The problem with Javascript is that the "this" operator is poorly overloaded and it is often hard to understand in the context of object-oriented javascript So, let's start with the definition:
14
5573
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 invoke undefined behavior in anyway? Assuming the implementation is c89 of course.
21
3869
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 writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
23
2452
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
2582
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(){}, my_meth2: function(){} }); to define new methods on the MyObj prototype object. Object.extend
2
1109
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 shared the objects methods and properties. I realise (now) that this is actually how prototypes work, they share functions and objects rather than create new instances of them for every "class", but is there any way around it? (or shouldn't I be...
2
9726
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
8097
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 difference between a function prototype and a function declaration? If you get this right, you must have done fair amount of research on C
0
9990
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9835
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11249
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10473
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9649
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7177
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5863
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4285
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3294
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.