Site banner
.
Home Forums Blogs Articles Photos Videos Contact FAQ                    
.
.
Wisdom Archive
Body Mind and Soul
Faith and Belief
God and Religion
Law of Attraction
Life and Beyond
Love and Happiness
Peace of Mind
Peace on Earth
Personal Faith
Spiritual Festivals
Spiritual Growth
Spiritual Guidance
Spiritual Inspiration
Spirituality and Science
Spiritual Retreats
More Wisdom
Buddhism Archives
Hinduism Archives
Sustainability
Theology Archives
Even more Wisdom
2012 - Year 2012
Affirmations
Aura
Ayurveda
Chakras
Consciousness
Cultural Creatives
Diksha (Deeksha)
Dream Dictionary
Dream Interpretation
Dream interpreter
Dreams
Enlightenment
Essential Oils
Feng Shui
Flower Essences
Gaia Hypothesis
Indigo Children
Kalki Bhagavan
Karma
Kundalini
Kundalini Yoga
Life after death
Mayan Calendar
Meaning of Dreams
Meditation
Morphogenetic Fields
Psychic Ability
Reincarnation
Spiritual Art, Music & Dance
Spiritual Awakening
Spiritual Enlightenment
Spiritual Healing
Spirituality and Health
Spiritual Jokes
Spiritual Parenting
Vastu Shastra
Womens Spirituality
Yoga Positions
Site map 2
Site map


Dream Sharing Forum

at Global Oneness Community.

Share your dreams and let others help you with the interpretation!
Dream Sharing Forum



.

Type conversion

Type conversion: Encyclopedia - Type conversion

In aviation, type conversion refers to the training pilots undertake in order to fly types of aircraft they have not operated before. Type conversion - Computing. In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting. Explicit type conversion c ...

Including:

Type conversion, Type conversion - Aviation, Type conversion - Computing, Type conversion - Explicit type conversion, Type conversion - Implicit type conversion, Type conversion - Two common casting styles, Type conversion - in Ada, Type conversion - in C/C++

Type conversion: Encyclopedia - Type conversion



Type conversion

Type conversion - Aviation

In aviation, type conversion refers to the training pilots undertake in order to fly types of aircraft they have not operated before.

Type conversion - Computing

In computer science, type conversion or typecasting refers to changing an entity of one datatype into another. There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. Explicit type conversion in some specific way is known as casting. Explicit type conversion can also be achieved with separately defined conversion routines such as an overloaded object constructor.

Type conversion - Implicit type conversion

Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. Some languages allow, or even require compilers to provide coercion.

In a mixed type expression, a subtype s will be converted to a supertype t or some subtypes s1, s2, ... will be converted to a supertype t (maybe none of the si is of type t) at runtime so that the program will run correctly. For example:

double  d;
long    l;
int     i;
if (d > i) d = i; if (i > l) l = i; if (d == l) d *= 2;

is legal in a C language program. Although d, l and i belong to different datatypes, they will be automatically converted to the same datatype each time a comparison or assignment is executed.

Type conversion - Explicit type conversion

There are several kinds of explicit conversion.

checked before the conversion is performed a runtime check is done to see if the destination type can actually hold the source value. If not an error condition is raised. unchecked no check is perfomed and when the destination type can not hold the source value the result is undefined. bit pattern The data is not interpreted at all and just the raw bit pattern is copied.

Each programming language has its own rules on how types can be converted. In general, both objects and fundamental data types can be converted.

Type conversion - in Ada

Ada supports all three conversion techniques and a few related techniques. On Wikibooks there is a full article on how to use them. It might be worth reading even if you plan to use another programming language.

Type conversion - in C/C++

A cast, or explicit type conversion, is special programming instruction which specifies what data type to treat a variable as (or an intermediate calculation result) in a given expression.

Casting will ignore "extra" information (but never adds information to the type being casted). The C/C++ cast is either "unchecked" or "bit pattern".

As an example with fundamental data types, a fixed-point float could be cast as an integer, where the data beyond the decimal (or binary) point is ignored. Alternatively, an integer could be cast as a float if, for example, a function call required a floating point type (but, as noted, no information is really added - 1 would become 1.0000000).

Object casting works in a similar way. A subclass can be cast as a parent type, where the "extra" information that makes it a subclass is ignored, and only the parts inherited from the parent are treated. For example, a triangle class derived from a shape class could be cast as a shape.

Type conversion - Two common casting styles

There are two common casting styles, each outlined below.

This style of casting is used in C and Java. It follows the form:

(type)expression

Several cast syntaxes are used in C++ (although C-style casting is supported as well). The function-call style follows the form:

type(expression)

This style of casting was adopted to force clarity when using casting. For example, the result of, and intention of, the C style cast

(type)firstVariable + secondVariable

may not be clear, while the same cast using C++-style casting allows more clarity:

type(firstVariable + secondVariable)

or

type(firstVariable) + secondVariable

Later in the evolution of C++, the following more explicit casts were added to the language to clarify the programmer's intent even further:

static_cast<type>(value_to_cast)
dynamic_cast<type>(value_to_cast)
const_cast<type>(value_to_cast)
reinterpret_cast<type>(value_to_cast)

Static casts converts type-compatible values. For instance the following:

double myDouble = 3.0;
int myInt = static_cast<int>(myDouble);

converts the double-precision floating point value myDouble (3.0) to the corresponding integer value (3). Static casts can be dangerous:

YourClass * pYour = GimmeAnObject();
void * pv = pYour;                                // no cast needed.
MyClass * p = static_cast<MyClass *>(pYour);      // MyClass had better be related to YourClass...
p->SomeMethod();                                  // ...or this might blow up in a nasty way.

Static casts on pointers or references don't verify that the pointed-to object is type-compatible to the new type.

A dynamic cast is safer than a static cast in this scenario: it is compiled by the compiler into a call to the C++ runtime library where a check is made to ensure legal casts. This is analogous to the casts in Java.

YourClass * pYour = GimmeAnObject();
void * pv = pYour;                                // no cast needed.
MyClass * p = dynamic_cast<MyClass *>(pYour);     // This won't blow up in the same way
if (p != 0)
    p->SomeMethod();                              // C++ guarantees p points to a MyClass

Dynamic casts on pointers return a null pointer if cast value is type incompatible. Dynamic casts on a reference throw a type exception.

A const cast casts away the constness of an object, returning a non-const reference to the same object. This allows modifications to objects that normally would be treated read-only by the compiler and allows passing to libraries that were not designed with const correctness in mind:

const MyClass * cantTouchThis = CreateConstObject();
cantTouchThis->constant_value = 41;                        // compile-time error.
const_cast<MyClass *>(cantTouchThis)->constant_value = 42; // compiles, but who knows what happens at runtime?

The reinterpret cast is the most notorious one in C++. It allows the reinterpretation of the raw bit pattern of the value to be cast, disregarding the type system completely. For example, it allows the casting of an arbitrary integer to a pointer to an object:

MyClass * pclass = reinterpret_cast<MyClass *>(0xDEADBEEF); // I know what I'm doing
pclass->some_field = 3.14159;                               // very unsafe indeed

Opinions were divided when these verbose casts were introduced into the language. Detractors argued the new syntax was 'ugly', while supporters claimed that since casting is such an 'ugly' activity to begin with, it should be highlit with an 'ugly' syntax to alert programmers. Another perceived advantage is the ease with which verbose casts can be located in source code using programming tools like grep.

Categories: Articles to be merged | Data types




Adapted from the Wikipedia article "Type conversion", under the G.N U Free Docmentation License. Please also see http://en.wikipedia.org/wiki

More material related to Type Conversion can be found here:
Main Page
for
Type Conversion
Index of Articles
related to
Type Conversion


« Back








Search the Global Oneness web site
Global Oneness is a huge, really huge, web site. Almost whatever you are searching for within health, spirituality, personal development and inspirationals - you will find it here!
Google
 
 

Rate this article!

Please rate this article with 10 as very good and 1 as very poor.

.








Sneak-Peek of Global Oneness Community

Hi friend! The Global Oneness Community, the place for information and sharing about Oneness is not really launched yet (you will see there is still some clean up to do) ...but it is now open for a sneak-peek! And if you wish - please register and become one of the very first members to do so! Jonas

Forum Home, Articles, Photo Gallery, Videos, News, Sitemap
...and much more!


Dream Sharing Forum

at Global Oneness Community.

Share your dreams and let others help you with the interpretation!
Dream Sharing Forum



Forum
Articles
Images Pictures
Videos
News
Sitemap




 

 

 

 

 


 








  » Home » » Home »