![]() ![]() |
Tip #3 - Converting between commonly used types
When dealing with the various commonly used Power++ types such as
WString
orWLong
, there often arise situations in which there is a need to convert from one type to another. For example problems can arise when trying to pass aWString
to a method that accepts aWLong
. The following is meant to answer most common issues when dealing with various classes, and perhaps give the adept programmer an idea or two.
- Converting with Constructors
![]()
Consider the code snippit at right. In the last line of code, we are passing a
WChar *
object toMyMethod
,which accepts aWString
. This is not an illegal operation, contrary to first impressions, becauseWString
has a constructor defined which accepts aWChar *
type; this constructor is documented in the Component Library Reference.![]()
When C++ runs into this type of situation, it automatically looks for a constructor which accepts only one parameter, and if found will invoke it using the source object as the argument, which in this case is a
WChar *
. This is important because it makes the assumption that all one-parameter constructors are conversion constructors. This requires caution when creating a one-parameter constructor which is not intended to be used as a conversion constructor.Converting with Methods
![]()
When a constructor is not available, Power++ classes quite often provide methods to achieve the required conversion. For example, by using the
ConvertToInt
andConvertToLong
methods forWString
, aWString
object may be converted into either aWInt
orWLong
, respectively. To find the methods provided by an Power++ class, use the online Component Library Reference, and search for the class name. Listed are all of the methods provided by the class, in addition to a description and all of its constructors, properties and operators.
Click here to see Tip#4Return to Power++ Home Page.
to top of page
Copyright © 1999 Sybase, Inc. All Rights Reserved.
Privacy Policy
Legal