473,385 Members | 1,925 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,385 software developers and data experts.

is C++ dynamically or statically typed language or both?

I have seen these terms used in Gang of 4 but could never thoroughly
understand what it meant I perceive that C++ is both, but apparently
it is only a latter.

Can someone explain it?

thx

Dec 25 '05 #1
4 10419
Well, you need to have clear definitions of both terms before you can
evaluate to what extent they do or do not apply to C++. Others may
correct me, but I'll rough out some definitions here:

Statically typed language -- a language which requires compile-time
enforcement of a type system.

Dynamically typed language -- a language which provides information
about types at runtime, and may permit operations based on that
information.

By these definitions, C++ is both. However, there is room for a lot of
gray here, and it has a great deal to do with programming practice, not
just the language standard. I'll use Java as a point of comparison.
Both C++ and Java have static type checking. However, C++'s static
typing system is much stronger, especially w.r.t. templates. C++
provides runtime facilities for typing as well (RTTI, typeid,
dynamic_cast), but these are language/library features which one can do
entirely without, often with good justification. C++ generally abides
by the philosophy that if you can perform the necessary determinations
statically (i.e. at compile time), you're better off doing that than
paying the runtime cost of determining them dynamically. By contrast,
in Java you pretty much agree up front to pay the cost for dynamic
typing, so it finds its way into standard Java idioms pretty
extensively. It's handy to have this available in Java, because of the
diminished static typing as compared to C++.

Does this help?

Luke

Dec 25 '05 #2
puzzlecracker wrote:
I have seen these terms used in Gang of 4 but could never thoroughly
understand what it meant I perceive that C++ is both, but apparently
it is only a latter.

Can someone explain it?


C++ is strictly a statically typed language, however, you can create
classes that do dynamic typing, but from a C++ standpoint, this is an
application implementation detail, not somthing provided as a standard
language facility.
Dec 25 '05 #3

Luke Meyers wrote:
Well, you need to have clear definitions of both terms before you can
evaluate to what extent they do or do not apply to C++. Others may
correct me, but I'll rough out some definitions here:

Statically typed language -- a language which requires compile-time
enforcement of a type system.

Dynamically typed language -- a language which provides information
about types at runtime, and may permit operations based on that
information.

By these definitions, C++ is both. However, there is room for a lot of
gray here, and it has a great deal to do with programming practice, not
just the language standard. I'll use Java as a point of comparison.
Both C++ and Java have static type checking. However, C++'s static
typing system is much stronger, especially w.r.t. templates. C++
provides runtime facilities for typing as well (RTTI, typeid,
dynamic_cast), but these are language/library features which one can do
entirely without, often with good justification. C++ generally abides
by the philosophy that if you can perform the necessary determinations
statically (i.e. at compile time), you're better off doing that than
paying the runtime cost of determining them dynamically. By contrast,
in Java you pretty much agree up front to pay the cost for dynamic
typing, so it finds its way into standard Java idioms pretty
extensively. It's handy to have this available in Java, because of the
diminished static typing as compared to C++.

Does this help?

Luke
Thanks Luke. Indeed helpful!However, C++'s static typing system is much stronger, especially w.r.t. templates
I am sure you missed the introduction of templates in jdk 5.0 (1.5).requires compile-time enforcement of a type system.

How does c++ enforce that?
I thought run-time polymorphism, that is, through virtual functions,
make language of a dynamic type.

Dec 25 '05 #4
puzzlecracker wrote:
I am sure you missed the introduction of templates in jdk 5.0 (1.5).


I'm sure I didn't, though you should be more careful than to refer to
Java generics as "templates." They're superficially similar, but
fundamentally different. A specialization of a generic does not really
create a new type, unlike a template specialization. Generics also
don't support specializing on constants, like templates do (e.g.
template <int x> identity() { return x; }). The types are erased after
compilation, which is pretty limiting. You can't overload on different
specializations of a generic type, because the types are erased after
compilation, meaning that at runtime all specializations revert to be
the same type as if you had never specialized them. Generics support
the "container of T" paradigm well, but for templates that's only the
barest beginning.

To get back to C++, and your question about run-time polymorphism and
virtual functions: remember that the only thing that makes a virtual
function different is their dispatch -- that is, when I have this code:
Parent * p = new Child();
p->doStuff();
The call to doStuff is dispatched to the Child implementation of that
function, if Parent declared doStuff as virtual. This is accomplished
by means of a virtual function table, or "vtable." The vtable is just
a lookup table, computed at compilation time. I'm not up on the
details of how the dispatch is accomplished, but you can look that up
on your own if you're interested.

A bit more material on static and dynamic typing:
http://en.wikipedia.org/wiki/Data_ty...dynamic_typing
Reading this, I find that my original definitions could use some
improvement. But most of what I've said about C++ still stands. The
gist of it is that in a dynamically-typed language, the type of a
variable may depend on the execution path on which it occurs. For
example:
if (cond) {
x = 5;
} else {
x = "foo";
}
To do something like this in C++, you'd have to write appropriate
conversion operators, assignment operators, or constructors, and you
still wouldn't be changing the type of x, just setting the value of x
based on a conversion from some other type.

Anyway, the Wikipedia article knows more than me, so you should
probably just read that.

Luke

Dec 25 '05 #5

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

Similar topics

22
by: Ajay | last post by:
hi! is there an authoritative source on the performance of scripting languages such as python vs. something like java, c, c++. its for a report, so it would be awesome if i could quote some...
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
2
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: munna | last post by:
Why scripting languages are dynamically and weakly typed? and why none of the scripting languages is compiler based? why cant a scripting language be use like java or c++ where we can catch type...
5
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before...
36
by: Martin Larsen | last post by:
Hi, When a PHP program links to a library using include or require (or their _once variations), is the library then linked dynamically or statically? While it might seem irrelevant from a...
0
by: swapnil1987 | last post by:
hello friends i am trying to find solution for this problem if u know something then please tell i am creating web application that runs on black berry. here i need to create selection list...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...
0
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,...
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...

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.