小霜 撰寫於文章 <3S6UUl$UPa@bbs.ncku.edu.tw>...
:深深的感謝Paladin提供資料並再附上一個問題
: 在這本書中說明c++是weakly typing
:那麼有沒有其他的說法支持c++是strong typing 的論點嗎??
:還是請各位看官列出書名--and--作者
:大大的感謝說!!
:--
1.
在 "The C++ Programming Language 3/e" p.6 section 1.2 的第二段有寫
.... All are based on strong static type checking, ...
2.
另外在 "The C Programming Language 2/e"
的 Introduction 中 p.3 第四段有提到
C is not a strongly-typed language, ...
而在 "The C++ Programming Language 3/e"
的 Preface to the First Edition (就是第一版的序啦)中有提到
C++ provides much better facilities for type checking ...
所以了,至少有一點可以確定的是 C++ 比 C 更 strong type checking
至於其它書上的說法我是不知啦,不過這二本書是可以參考一下的啦
> -------------------------------------------------------------------------- <
發信人: garfi.bbs@vlsi1.iie.ncku.edu.tw (小霜), 看板: Programming
標 題: Re: R:strong typing
發信站: 成大資訊所_BBS (Thu Dec 17 13:53:56 1998)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!netnews.csie.nctu!netnews2.csie.nctu!n
==> jyhuang@csie.nctu.edu.tw (Jiun-Yao Huang) 在 programming 版提到:
: // 深深的感謝Paladin提供資料並再附上一個問題
: // 在這本書中說明c++是weakly typing
: // 那麼有沒有其他的說法支持c++是strong typing 的論點嗎??
: Booch 書上並沒有說 C++ 是 weakly typing 啊!在書中 67 第一段。
: C++ 的設計是朝 strong typing 的方向走,但一方面又允許忽略 typing。
可是在Paladin所提供的bbs.ncku.edu-->program程式語言討論區的提到
以下文章從Grady Booch的 "Object-oriented analysys and design with
applications" 節取, 第65~67頁. 希望能對你有幫助 :)
"...strong typed, meaning that type conformance is strictly enforced:
operations cannot be called upon an object unless the exact signature
of that operation is defined in the object's class or superclasses."
"A type is a precise characterization of structural or behavioral
properties which a collection of entities all share"
"Typing is the enforcement of the class of an object, such that
objects of different types may not be interchanged, or at most,
they may be interchanged inly in very restricted ways."
"Violations of type conformance may not be known until execution,
and usually manifest themselves as execution errors. Languages
such as C++ are hybrid: they have tendencied toward strong typing,
but it is possible to ignore or suppress the typnig rules."
"In C++, typedefs do not introduce new types. ..., In this aspect,
C++ is weakly typed."
所以我猜想c++是weakly typed的language說
各位看倌,若有其它的想法請再po文章給我--小女子感激不盡說!!
還是希望指出書名及作者喔!!
--
Origin: 成大資工BBS站 (vlsi1.csie.ncku.edu.tw) From: 210.70.147.108
> -------------------------------------------------------------------------- <
發信人: Paladin.bbs@bbs.ccu.edu.tw ([積極創新修德澤人]), 看板: Programming
標 題: Re: R:strong typing
發信站: 中正大學寂寞芳心小站 (Thu Dec 17 18:04:47 1998)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!suncc.ccu!Feeling
(Booch的第66頁:)
"In strongly typed languages, violation of type conformance can
be detected at the time of compilation. Smalltalk, on the other
hand, is an untyped language: a client can send any message to
any class(although a class may not know how to respond to the
message). Violations of type conformance may not be known until
execution, and usually manifest themselves as execution errors.
Languages such as C++ are hybrid: they have tendencied toward
strong typing, but it is possible to ignore or suppress the typing
rules."
我想這一段除了說明strong/weak typing的不同外,
另外也提到 C++ 是介於strong和weak之間,
它傾向於strong typing, 卻又允許某些type interchange.
: "In C++, typedefs do not introduce new types. ..., In this aspect,
: C++ is weakly typed."
這一段我想我可能有點斷章取義...原文是:
" typedef float Concentration // 第48頁
We introduce another typedef:
typedef float Level // 第67頁
In C++, typedefs do not introduce new types. In particular, the
typedefs "Level" and "Concentration" are both floating-point numbers,
and can be intermixed. In this aspect, C++ is weakly typed: values
of primitive types such as "int" and "float" are indistinguishable
within that particular type. "
"Level"和"Concentration"兩個資料型別, 在strong-typing是不可以互換的,
但在C++中因為兩者都屬於float, 所以C++允許互換. 就這一點來講C++是 weak-
typing的.
如有任何錯誤, 歡迎指教 :)
--
朋友與其"知心", 不如"貼心".
所謂貼心, 就是體貼的心, 一種主動關懷對方的心和
被動傾聽對方心聲的心.
遊俠心 平凡人
Ξ Origin: 中正大學寂寞芳心小站 <bbs.ccu.edu.tw> [FROM: 140.123.2.28]
> -------------------------------------------------------------------------- <
發信人: PaladinMu.bbs@cis.nctu.edu.tw (蕃茄怎麼不見了...), 看板: Programming
標 題: Re: R:strong typing
發信站: 交大資科_BBS (Wed Dec 30 12:35:27 1998)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!newsfeed.nthu!news.cs.nthu!netnews.csi
==> 在 Paladin.bbs@bbs.ccu.edu.tw ([積極創新修德澤人 的文章中提到:
> (Booch的第66頁:)
> "In strongly typed languages, violation of type conformance can
> be detected at the time of compilation. Smalltalk, on the other
> hand, is an untyped language: a client can send any message to
> any class(although a class may not know how to respond to the
> message). Violations of type conformance may not be known until
> execution, and usually manifest themselves as execution errors.
> Languages such as C++ are hybrid: they have tendencied toward
> strong typing, but it is possible to ignore or suppress the typing
> rules."
> 我想這一段除了說明strong/weak typing的不同外,
> 另外也提到 C++ 是介於strong和weak之間,
> 它傾向於strong typing, 卻又允許某些type interchange.
隔了幾天才看到這個 thread, 就自己所知說一說。
Booch 的這段說法著重在 type error 是何時被找出來,認為在 compile
time 找出來的就是 strongly typed, 否則是 weakly typed. 但也有人
不這麼說。至少研究 type 的一些人似乎認為這樣的定義不夠細緻,無法
反映出 type system 的多樣性。
Luca Cardelli 在一篇 paper (http://www.luca.demon.co.uk
/Papers.html#TypeSystems) 中用了下列的定義,我不知這樣的分類在哪
些領域類廣被接受,不過 Cardelli 是個挺重要的人物,所以應該有些份
量。 :)
照他的說法,type/untype, static/dynamic, strong/weak
都是不相關的特性。如果一個 type system 在 compile time 抓出 type
error, 則是 static, 反之則為 dynamic.
程式執行時會發生的 error 有兩種,錯誤發生當場可以發現的(如除零)
稱做 trapped error,要隔一陣子才會突然當掉的則叫做 untrapped error,
例如把 pointer 當數字用等等。如果一個 type system 能保證不會有
untrapped error 發生,就是 strongly typed. 否則是 weak typed.
這樣一來,LISP會被歸類為 dynamic, strongly typed 的語言。而 C 顯然是
weakly typed. 但 C++ 呢?
--
Paladin Mu
怎麼又一個 paladin 呀? :)
--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>
> -------------------------------------------------------------------------- <
發信人: garfi.bbs@bbs.ncku.edu.tw (小霜), 看板: Programming
標 題: Re: R:strong typing
發信站: 成大計中BBS (Wed Jan 6 12:45:15 1999)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!ccnews.nsysu!news.civil.ncku!news.ncku
首先:大大ㄉ感謝PaladinMuㄉ大力幫助;
我覺得會回我的問題的人都叫做Paladin,所以我想我也該加入Palaidin的行列
叫做PaladinShuang了~~呵~~
現在我對strong typing 的想法整理如下:和大家交流
一、 Strong typing 的定義:
1-1 在Borert W.Sebesta所寫的Concepts of Programming
Languages 的page166到167中有提到:
"strong typing:One of the new ideas in language design
that became prominent in the so-called structured programming
revolution of the 1970s is strong typing. Strong typing
is widely acknowledged as being a highly valuable concept.
Unfortunately, it is often loosely defined, and it is
sometimes used in computing literature without being
defined at all."
"The following is one simple but incomplete definition
of a strongly typed language: A strongly typed language
is one in which each name in a program in the language
has a single type associated with it, and that type is
known at compile time. The essence of this definition is
that all types are statically bound. The weakness of
this definition is that it ignores the possibility that,
although a variable's type may be known, the storage
location to which it is bound may store values of
different types at different times. To take this
possibility into account, we define a programming
language to be strongly typed if type errors are always
detected. This requires that the types of all operands
can be determined, either at compile time or
At run time. The importance of strong typing lies in its
ability to detect all misuses of variables that result
in type errors. A strongly typed language also allows the
detection, at run time, of uses of the incorrect type
values in variables that can store values of more than one
type."
1-2 在Grady Booch所寫的Object-oriented analysis and
design with applications第65~67頁中有提到:
"strong typed, meaning that type conformance is strictly
enforced:
operations cannot be called upon an object unless the
exact signature of that operation is defined in the
object's class or superclasses."
"A type is a precise characterization of structural or
behavioral properties which a collection of entities all
share"
"Typing is the enforcement of the class of an object,
such that objects of different types may not be
interchanged, or at most, they may be interchanged in
very restricted ways."
1-3 在David R C Hill所寫的Object-Oriented Analysis and
simulation的page39中有提到:
"Grady Booch gives the following definition of typing:
Typing is the enforcement of the class of an object,
such that objects of different types may not be
interchanged, or at most they may be interchanged in
very restricted ways. (Booch, 1991)"
"Booch considers that typing is only a minor element
of object modelling. As far as implementation is
concerned, a strongly typed language, a slightly typed
language or language which is not typed at all may still
be an object-oriented language."
"In strongly typed languages, assignments or operations
which might not be consistent with the types of the
objects concerned are detected at compile time. This
function is desirable for reasons of software validity.
However, it is possible with numerous typed languages to
make varying records in order to handle variables which
mayhave different types. This feature, included in most
major languages, shows a real need for flexible typing."
1-4 在Bertand Meyer所寫的An Object-Oriented environment
principles and Application 的page47到49中有提到:
"Strong typing: In the typed approach to object-oriented
software construction, every object has a will-defined
type, which results form the declaration of the
corresponding entity in the software text. In such
declarations the specification of the type is a required
component."
"Indeed an object-oriented environment will benefit form
strong typing. For environments too strong typing will be
beneficial. Like execution objects and the corresponding
entities, development objects and the corresponding tools
will have clearly defined types. The advantages are the
counterparts of T1 and T2 above: safety (helping users
avoid errors) and readability (which here means ease of
use).
T1. Making the software safer by enabling compilers to
detect many errors that, with other approaches, would be
detected much later or not at all.
T2. Improving the readability of the software: by
declaring every entity with the corresponding type you
give a strong and immediate indication to the software
reader about what the entity is intended for and how it
will be used."
"Strong Typing Principle:
An object-oriented environment should associate a type
with every development object, ensure that each object's
type is immediately apparent from the object's visual
representation, define simple rules as to what operations
and enforce these rules though interaction techniques
that favor error avoidance over a posteriori detection and
correction."
1-5 在Arthur E. Anderson william J. Heinze所寫的C++
Programming and Fundamental Concepts 的page5中有提到:
"Strong typing checking:
In languages which do not support strong typing checking,
it is possible to pass the wrong type of argument of a
function call, e.g., passing a double when an int should
have been passed. These programs will often compile and
produce an executable file but generate runtime errors.
To prevent runtime errors(i.e., catch the errors during
compilation)strong type checking must used."
1-6 在Kip R. Irvine所寫的C++ and Object-Oriented
Programming 的page2中有提到:
"A strongly typed language is one in which the compiler
performs strict type checking on variables and
expressions, helping to catch some errors that would
otherwise go unnoticed and wind up being execution
errors."
1-7 在蔡明志所寫的物件導向程式設計-使用Borland C++
的page7-6中有提到:
"在一個純粹的靜態型別(static typing)程式語言來說,
"值(value) "本身是沒有什麼意義的,必須要靠 "識別字
(identifier)" 來解釋"值"有什麼含意,記憶體中的某位址上
記錄了一堆0或1的值,這些值如果用整數型別的識別字來解釋
可能是65,如果用字元型別的識別字來解釋則可能是'A',同
樣一堆0或1的值套上不同的型別的識別字,將有不同的意義。
如果某程式語言恪守以上規則,則稱之為強勢型態(strong
typing)的語言。"
二、 C++為strong typing的說法:
2-1 在Arthur E. Anderson william J. Heinze所寫的C++
Programming and Fundamental Concepts 的page5中有提到:
"Strong type checking is a feature of Pascal, Modula2,
Ada, and C++. In C++ all functions must be declared with
the types of their return values and function argument.
This declaration is called the function prototype. When
a function is called, the type of arguments that are
passed are checked with the type of arguments in the
function prototype and any non-coercible differences
will invoke a compiler error."
2-2 在Kip R. Irvine所寫的C++ and Object-Oriented
Programming 的page2中有提到:
"C++ is sometimes called "a better C" because it has
strong type checking and better protection against
runtime errors. It is possible to use C++ to write
better C programs, but one would be missing out on
the advantages of object-oriented programming. C++
has also been called a "hybrid language" because it
allows one to mix object-oriented programming with
traditional procedural programming.
A strongly typed language is one in which the
compiler performs strict type checking on variables and
expressions, helping to catch some errors that would
otherwise go unnoticed and wind up being execution errors.
This was a major goal in the design of C++--for the
language to help programmers create robust, reliable
programs."
2-3 在Grady Booch的 Object-oriented analysis and
design with applications第66頁中有提到:
"In strongly typed languages, violation of type
conformance can be detected at the time of compilation.
Smalltalk, on the other hand, is an untyped language: a
client can send any message to any class (although a
class may not know how to respond to the message).
Violations of type conformance may not be known until
execution, and usually manifest themselves as execution
errors. Languages such as C++ are hybrid: they have
tendencied toward strong typing, but it is possible to
ignore or suppress the typing rules."
三、 C++非為strong typing的說法:
3-1 在Borert W.Sebesta所寫的Concepts of Programming
Languages 的page167中有提到:
"C and C++ are not strongly typed languages because
both allow functions for which parameters are not type
checked. Furthermore, the union types of these languages
are not type checked."
3-2 在蔡明志所寫的物件導向程式設計-使用Borland C++
的page7-6中有提到:
"但本書所使用的C++和C並不屬於強勢型態
(strong typing)的語言,因為它們如果利用union機制,則同
一個記憶位址將可以有兩種以上不同型別的識別字,換句話說,
在編譯時期,編譯器對該位址之值應如何解釋並無法確定,因而
它們是屬於弱勢型態(weak typing)的語言。強勢語言的好處
是:編譯器可以靠著型別的檢查,找出任何不合法之設定,而這
類設定很可能就是程式師疏忽所造成的錯誤,但相反地,如果一
位程式師很高明地運用某些技巧,必須故意使用這類不合法的設
定時,他會採用弱勢型態的語言。"
3-3 在Grady Booch的 Object-oriented analysis and design
with applications第67頁中有提到:
"In C++, typedefs do not introduce new types. In
particular, the typedefs "Level" and "Concentration"
are both floating-point numbers, and can be intermixed.
In this aspect, C++ is weakly typed: values of primitive
types such as "int" and "float" are indistinguishable
within that particular type."
PS. typedef float Concentration和typedef float Level
在本書中的第48頁。
四、 我的想法:
由於C++是C語言引進了Object-oriented的觀念,所以有許多
對C++有興趣的人,愈來愈重視C++的各種探討,而strong typing就
是一項值得研究的課題。
C++在某些情況下,是以非strong typing的模式存在的,例
如:union、typedef float Concentration和typedef float
Level。但是Strong typing有 "編譯器可以靠著型別的檢查,找
出任何不合法之設定,而這類設定很可能就是程式師疏忽所造成
的錯誤"的優點,所以C++的設計是朝著Strong typing的方向走,
但一方面又允許忽略typing,"Level"和"Concentration"兩個資
料型別,在strong typing的情況下,是不可以互換的,但在C++
中因為兩者都屬於float,所以C++允許互換,就這一點來講C++
是weak typing的。要說C++介於strong typing 和weak typing
之間也不為過,因為它傾向於strong typing,卻又允許某些type
interchange。
如果您的所設計的程式希望:編譯器可以靠著型別的檢查,
找出任何不合法之設定,就可以取用C++為strong typing的部份
,同樣道理的,如果您想用到可以型態轉換的程式或想很高明地
運用某些技巧,必須故意使用這類不合法的設定時,您可以採用
C++弱勢型態的部份。所以說寫C++就是視您的需要,選擇最適合
的方法來做。
PS.若有錯誤,請多多指教!!
--
您正如風般
輕輕得飄來~~
--
㊣ Origin: 成大計中 BBS 站 ◆ From: sun1.im.cju.edu.tw
> -------------------------------------------------------------------------- <
發信人: PaladinMu.bbs@cis.nctu.edu.tw (蕃茄怎麼不見了...), 看板: Programming
標 題: Re: R:strong typing
發信站: 交大資科_BBS (Wed Dec 30 12:35:27 1998)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!newsfeed.nthu!news.cs.nthu!netnews.csi
==> 在 Paladin.bbs@bbs.ccu.edu.tw ([積極創新修德澤人 的文章中提到:
> (Booch的第66頁:)
> "In strongly typed languages, violation of type conformance can
> be detected at the time of compilation. Smalltalk, on the other
> hand, is an untyped language: a client can send any message to
> any class(although a class may not know how to respond to the
> message). Violations of type conformance may not be known until
> execution, and usually manifest themselves as execution errors.
> Languages such as C++ are hybrid: they have tendencied toward
> strong typing, but it is possible to ignore or suppress the typing
> rules."
> 我想這一段除了說明strong/weak typing的不同外,
> 另外也提到 C++ 是介於strong和weak之間,
> 它傾向於strong typing, 卻又允許某些type interchange.
隔了幾天才看到這個 thread, 就自己所知說一說。
Booch 的這段說法著重在 type error 是何時被找出來,認為在 compile
time 找出來的就是 strongly typed, 否則是 weakly typed. 但也有人
不這麼說。至少研究 type 的一些人似乎認為這樣的定義不夠細緻,無法
反映出 type system 的多樣性。
Luca Cardelli 在一篇 paper (http://www.luca.demon.co.uk
/Papers.html#TypeSystems) 中用了下列的定義,我不知這樣的分類在哪
些領域類廣被接受,不過 Cardelli 是個挺重要的人物,所以應該有些份
量。 :)
照他的說法,type/untype, static/dynamic, strong/weak
都是不相關的特性。如果一個 type system 在 compile time 抓出 type
error, 則是 static, 反之則為 dynamic.
程式執行時會發生的 error 有兩種,錯誤發生當場可以發現的(如除零)
稱做 trapped error,要隔一陣子才會突然當掉的則叫做 untrapped error,
例如把 pointer 當數字用等等。如果一個 type system 能保證不會有
untrapped error 發生,就是 strongly typed. 否則是 weak typed.
這樣一來,LISP會被歸類為 dynamic, strongly typed 的語言。而 C 顯然是
weakly typed. 但 C++ 呢?
--
Paladin Mu
怎麼又一個 paladin 呀? :)
--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>
> -------------------------------------------------------------------------- <
發信人: garfi.bbs@bbs.ncku.edu.tw (小霜), 看板: Programming
標 題: Re: R:strong typing
發信站: 成大計中BBS (Wed Jan 6 12:45:15 1999)
轉信站: Ptt!news.ntu!ctu-gate!news.nctu!ccnews.nsysu!news.civil.ncku!news.ncku
首先:大大ㄉ感謝PaladinMuㄉ大力幫助;
我覺得會回我的問題的人都叫做Paladin,所以我想我也該加入Palaidin的行列
叫做PaladinShuang了~~呵~~
現在我對strong typing 的想法整理如下:和大家交流
一、 Strong typing 的定義:
1-1 在Borert W.Sebesta所寫的Concepts of Programming
Languages 的page166到167中有提到:
"strong typing:One of the new ideas in language design
that became prominent in the so-called structured programming
revolution of the 1970s is strong typing. Strong typing
is widely acknowledged as being a highly valuable concept.
Unfortunately, it is often loosely defined, and it is
sometimes used in computing literature without being
defined at all."
"The following is one simple but incomplete definition
of a strongly typed language: A strongly typed language
is one in which each name in a program in the language
has a single type associated with it, and that type is
known at compile time. The essence of this definition is
that all types are statically bound. The weakness of
this definition is that it ignores the possibility that,
although a variable's type may be known, the storage
location to which it is bound may store values of
different types at different times. To take this
possibility into account, we define a programming
language to be strongly typed if type errors are always
detected. This requires that the types of all operands
can be determined, either at compile time or
At run time. The importance of strong typing lies in its
ability to detect all misuses of variables that result
in type errors. A strongly typed language also allows the
detection, at run time, of uses of the incorrect type
values in variables that can store values of more than one
type."
1-2 在Grady Booch所寫的Object-oriented analysis and
design with applications第65~67頁中有提到:
"strong typed, meaning that type conformance is strictly
enforced:
operations cannot be called upon an object unless the
exact signature of that operation is defined in the
object's class or superclasses."
"A type is a precise characterization of structural or
behavioral properties which a collection of entities all
share"
"Typing is the enforcement of the class of an object,
such that objects of different types may not be
interchanged, or at most, they may be interchanged in
very restricted ways."
1-3 在David R C Hill所寫的Object-Oriented Analysis and
simulation的page39中有提到:
"Grady Booch gives the following definition of typing:
Typing is the enforcement of the class of an object,
such that objects of different types may not be
interchanged, or at most they may be interchanged in
very restricted ways. (Booch, 1991)"
"Booch considers that typing is only a minor element
of object modelling. As far as implementation is
concerned, a strongly typed language, a slightly typed
language or language which is not typed at all may still
be an object-oriented language."
"In strongly typed languages, assignments or operations
which might not be consistent with the types of the
objects concerned are detected at compile time. This
function is desirable for reasons of software validity.
However, it is possible with numerous typed languages to
make varying records in order to handle variables which
mayhave different types. This feature, included in most
major languages, shows a real need for flexible typing."
1-4 在Bertand Meyer所寫的An Object-Oriented environment
principles and Application 的page47到49中有提到:
"Strong typing: In the typed approach to object-oriented
software construction, every object has a will-defined
type, which results form the declaration of the
corresponding entity in the software text. In such
declarations the specification of the type is a required
component."
"Indeed an object-oriented environment will benefit form
strong typing. For environments too strong typing will be
beneficial. Like execution objects and the corresponding
entities, development objects and the corresponding tools
will have clearly defined types. The advantages are the
counterparts of T1 and T2 above: safety (helping users
avoid errors) and readability (which here means ease of
use).
T1. Making the software safer by enabling compilers to
detect many errors that, with other approaches, would be
detected much later or not at all.
T2. Improving the readability of the software: by
declaring every entity with the corresponding type you
give a strong and immediate indication to the software
reader about what the entity is intended for and how it
will be used."
"Strong Typing Principle:
An object-oriented environment should associate a type
with every development object, ensure that each object's
type is immediately apparent from the object's visual
representation, define simple rules as to what operations
and enforce these rules though interaction techniques
that favor error avoidance over a posteriori detection and
correction."
1-5 在Arthur E. Anderson william J. Heinze所寫的C++
Programming and Fundamental Concepts 的page5中有提到:
"Strong typing checking:
In languages which do not support strong typing checking,
it is possible to pass the wrong type of argument of a
function call, e.g., passing a double when an int should
have been passed. These programs will often compile and
produce an executable file but generate runtime errors.
To prevent runtime errors(i.e., catch the errors during
compilation)strong type checking must used."
1-6 在Kip R. Irvine所寫的C++ and Object-Oriented
Programming 的page2中有提到:
"A strongly typed language is one in which the compiler
performs strict type checking on variables and
expressions, helping to catch some errors that would
otherwise go unnoticed and wind up being execution
errors."
1-7 在蔡明志所寫的物件導向程式設計-使用Borland C++
的page7-6中有提到:
"在一個純粹的靜態型別(static typing)程式語言來說,
"值(value) "本身是沒有什麼意義的,必須要靠 "識別字
(identifier)" 來解釋"值"有什麼含意,記憶體中的某位址上
記錄了一堆0或1的值,這些值如果用整數型別的識別字來解釋
可能是65,如果用字元型別的識別字來解釋則可能是'A',同
樣一堆0或1的值套上不同的型別的識別字,將有不同的意義。
如果某程式語言恪守以上規則,則稱之為強勢型態(strong
typing)的語言。"
二、 C++為strong typing的說法:
2-1 在Arthur E. Anderson william J. Heinze所寫的C++
Programming and Fundamental Concepts 的page5中有提到:
"Strong type checking is a feature of Pascal, Modula2,
Ada, and C++. In C++ all functions must be declared with
the types of their return values and function argument.
This declaration is called the function prototype. When
a function is called, the type of arguments that are
passed are checked with the type of arguments in the
function prototype and any non-coercible differences
will invoke a compiler error."
2-2 在Kip R. Irvine所寫的C++ and Object-Oriented
Programming 的page2中有提到:
"C++ is sometimes called "a better C" because it has
strong type checking and better protection against
runtime errors. It is possible to use C++ to write
better C programs, but one would be missing out on
the advantages of object-oriented programming. C++
has also been called a "hybrid language" because it
allows one to mix object-oriented programming with
traditional procedural programming.
A strongly typed language is one in which the
compiler performs strict type checking on variables and
expressions, helping to catch some errors that would
otherwise go unnoticed and wind up being execution errors.
This was a major goal in the design of C++--for the
language to help programmers create robust, reliable
programs."
2-3 在Grady Booch的 Object-oriented analysis and
design with applications第66頁中有提到:
"In strongly typed languages, violation of type
conformance can be detected at the time of compilation.
Smalltalk, on the other hand, is an untyped language: a
client can send any message to any class (although a
class may not know how to respond to the message).
Violations of type conformance may not be known until
execution, and usually manifest themselves as execution
errors. Languages such as C++ are hybrid: they have
tendencied toward strong typing, but it is possible to
ignore or suppress the typing rules."
三、 C++非為strong typing的說法:
3-1 在Borert W.Sebesta所寫的Concepts of Programming
Languages 的page167中有提到:
"C and C++ are not strongly typed languages because
both allow functions for which parameters are not type
checked. Furthermore, the union types of these languages
are not type checked."
3-2 在蔡明志所寫的物件導向程式設計-使用Borland C++
的page7-6中有提到:
"但本書所使用的C++和C並不屬於強勢型態
(strong typing)的語言,因為它們如果利用union機制,則同
一個記憶位址將可以有兩種以上不同型別的識別字,換句話說,
在編譯時期,編譯器對該位址之值應如何解釋並無法確定,因而
它們是屬於弱勢型態(weak typing)的語言。強勢語言的好處
是:編譯器可以靠著型別的檢查,找出任何不合法之設定,而這
類設定很可能就是程式師疏忽所造成的錯誤,但相反地,如果一
位程式師很高明地運用某些技巧,必須故意使用這類不合法的設
定時,他會採用弱勢型態的語言。"
3-3 在Grady Booch的 Object-oriented analysis and design
with applications第67頁中有提到:
"In C++, typedefs do not introduce new types. In
particular, the typedefs "Level" and "Concentration"
are both floating-point numbers, and can be intermixed.
In this aspect, C++ is weakly typed: values of primitive
types such as "int" and "float" are indistinguishable
within that particular type."
PS. typedef float Concentration和typedef float Level
在本書中的第48頁。
四、 我的想法:
由於C++是C語言引進了Object-oriented的觀念,所以有許多
對C++有興趣的人,愈來愈重視C++的各種探討,而strong typing就
是一項值得研究的課題。
C++在某些情況下,是以非strong typing的模式存在的,例
如:union、typedef float Concentration和typedef float
Level。但是Strong typing有 "編譯器可以靠著型別的檢查,找
出任何不合法之設定,而這類設定很可能就是程式師疏忽所造成
的錯誤"的優點,所以C++的設計是朝著Strong typing的方向走,
但一方面又允許忽略typing,"Level"和"Concentration"兩個資
料型別,在strong typing的情況下,是不可以互換的,但在C++
中因為兩者都屬於float,所以C++允許互換,就這一點來講C++
是weak typing的。要說C++介於strong typing 和weak typing
之間也不為過,因為它傾向於strong typing,卻又允許某些type
interchange。
如果您的所設計的程式希望:編譯器可以靠著型別的檢查,
找出任何不合法之設定,就可以取用C++為strong typing的部份
,同樣道理的,如果您想用到可以型態轉換的程式或想很高明地
運用某些技巧,必須故意使用這類不合法的設定時,您可以採用
C++弱勢型態的部份。所以說寫C++就是視您的需要,選擇最適合
的方法來做。
PS.若有錯誤,請多多指教!!
--
您正如風般
輕輕得飄來~~
--
㊣ Origin: 成大計中 BBS 站 ◆ From: sun1.im.cju.edu.tw