C++: prototype declarations

W

Web Developer

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
 
W

Web Developer

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
 
J

John Harrison

requires

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
 
N

Nils Petter Vaskinn

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
 
K

Kevin Goodsell

Web said:
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.
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
 
D

Dave Rahardja

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top