473,569 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10436
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
2406
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 well-known authority on this. thanks
20
12873
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 obviously, I put the alert("s") part in for debugging purposes, just to make sure the error wasn't in any code I was going to be running. This line...
4
2250
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 source:
2
2367
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, Int, Float, etc...) 2. VB 6 and VBScript were not strongly typed because both allowed you to ... dim blah .... which creates a variant.
6
3359
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display...
4
1381
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 errors e.g. (string being assigned to an integer), parameter passing at compiled time. what is the significance of multiple dollar sign...
5
1954
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 some textboxes (dynamically added). I know I could move it around with CSS but I want to move towards an accessible site that will display forms in...
36
3159
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 technical point of view, the linking method is important when it comes to licencing issues as some licences, like GPL, differ between those kinds of...
0
2681
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 which has three radio buttons in it. but these are created dynamically. and i want to add event "onselectedindexchanged" for this list. so how...
0
7703
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...
0
7618
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...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6286
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...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.