[ Pobierz całość w formacie PDF ]
temporary object is destroyed when the initialization is complete.
v A reference is bound to a temporary object: the temporary object is destroyed at
the end of the reference s lifetime.
If a temporary object is created for a class with constructors, the compiler calls the
appropriate (matching) constructor to create the temporary object.
When a temporary object is destroyed and a destructor exists, the compiler calls
the destructor to destroy the temporary object. When you exit from the scope in
which the temporary object was created, it is destroyed. If a reference is bound to a
temporary object, the temporary object is destroyed when the reference passes out
of scope unless it is destroyed earlier by a break in the flow of control. For
example, a temporary object created by a constructor initializer for a reference
member is destroyed on leaving the constructor.
The ANSI/ISO C++ definition permits an implementation that eliminates the
construction of such temporary objects in cases in which they are redundant.
The VisualAge C++ compiler takes advantage of this fact to create more
efficient optimized code. Take this into consideration when debugging your
programs, especially for memory problems.
v Arguments of catch Blocks on page 324
v Initializing References on page 69
v Cast Expressions on page 108
v Function Return Values on page 140
User-Defined Conversions
User-defined conversions allow you to specify object conversions with
constructors or with conversion functions. User-defined conversions are implicitly
used in addition to standard conversions for conversion of initializers, functions
arguments, function return values, expression operands, expressions controlling
iteration, selection statements, and explicit type conversions.
There are two types of user-defined conversions:
v Conversion by constructor
v Conversion functions
The compiler can use only one user-defined conversion (either a conversion
constructor or a conversion function) when implicitly converting a single value.
The following example demonstrates this:
class A {
int x;
public:
operator int() { return x; };
};
274 C/C++ Language Reference
class B {
A y;
public:
operator A() { return y; };
};
int main () {
B b_obj;
// int i = b_obj;
int j = A(b_obj);
}
The compiler would not allow the statementinti=b_obj. The compiler would
have to implicitly convertb_objinto an object of typeA(withB::operatorA()),
then implicitly convert that object to an integer (withA::operatorint()). The
statementintj=A(b_obj)explicitly convertsb_objinto an object of typeA, then
implicitly converts that object to an integer.
User-defined conversions must be unambiguous, or they are not called. A
conversion function in a derived class does not hide another conversion function in
a base class unless both conversion functions convert to the same type. Function
overload resolution selects the most appropriate conversion function. The following
example demonstrates this:
class A {
int a_int;
char* a_carp;
public:
operator int() { return a_int; }
operator char*() { return a_carp; }
};
class B : public A {
float b_float;
char* b_carp;
public:
operator float() { return b_float; }
operator char*() { return b_carp; }
};
int main () {
B b_obj;
// long a = b_obj;
char* c_p = b_obj;
}
The compiler would not allow the statementlonga=b_obj. The compiler could
either useA::operatorint()orB::operatorfloat()to convertb_objinto a long.
The statementchar*c_p=b_objusesB::operatorchar*()to convertb_objinto
a char* becauseB::operatorchar*()hidesA::operatorchar*().
When you call a constructor with an argument and you have not defined a
constructor accepting that argument type, only standard conversions are used to
convert the argument to another argument type acceptable to a constructor for that
class. No other constructors or conversions functions are called to convert the
argument to a type acceptable to a constructor defined for that class. The following
example demonstrates this:
class A {
public:
A() { }
A(int) { }
};
Chapter 15. Special Member Functions 275
int main() {
A a1 = 1.234;
// A moocow = "text string";
}
The compiler allows the statementAa1=1.234. The compiler uses the standard
conversion of converting1.234into an int, then implicitly calls the converting
constructorA(int). The compiler would not allow the statementAmoocow="text
string"; converting a text string to an integer is not a standard conversion.
v Standard Type Conversions on page 116
Conversion by Constructor
A converting constructor is a constructor that can be called with one
parameter, but is not declared without the function specifier explicit. The compiler
uses converting constructors to convert objects from the type of the first parameter
to the type of the converting constructor s class. The following example
demonstrates this:
class Y {
int a, b;
char* name;
public:
Y(int i) { };
Y(const char* n, int j = 0) { };
};
void add(Y) { };
int main() {
// equivalent to
// obj1 = Y(2)
Y obj1 = 2;
// equivalent to
// obj2 = Y("somestring",0)
Y obj2 = "somestring";
// equivalent to
// obj1 = Y(10)
obj1 = 10;
// equivalent to
// add(Y(5))
add(5);
}
The above example has the following two converting constructors:
v Y(inti)which is used to convert integers to objects of class Y.
v Y(constchar*n,intj=0)which is used to convert pointers to strings to
objects of class Y.
The compiler will not implicitly convert types as demonstrated above with
constructors declared with the explicit keyword. The compiler will only use
explicitly declared constructors in new expressions, the static_cast expressions and
explicit casts, and the initialization of bases and members. The following example
demonstrates this:
276 C/C++ Language Reference
class A {
public:
explicit A() { };
explicit A(int) { };
};
int main() {
A z;
// A y = 1;
A x = A(1);
A w(1);
A* v = new A(1);
A u = (A)1;
A t = static_cast(1);
}
The compiler would not allow the statementAy=1because this is an implicit
conversion; classAhas no conversion constructors.
A copy constructor is a converting constructor.
v The explicit Keyword on page 121
v C++ new Operator on page 92
v static_cast Operator on page 82
v Cast Expressions on page 108
Conversion Functions
You can define a member function of a class, called a conversion function,
that converts from the type of its class to another specified type.
operator conversion_type
class :: const
volatile
( )
{ function_body }
pointer_operator
A conversion function that belongs to a classXspecifies a conversion from the class
typeXto the type specified by the conversion_type. The following code fragment
shows a conversion function calledoperatorint():
class Y {
int b;
public:
operator int();
};
Y::operator int() {
return b;
}
void f(Y obj) {
int i = int(obj);
int j = (int)obj;
int k = i + obj;
}
Chapter 15. Special Member Functions 277
All three statements in functionf(Y)use the conversion functionY::operator
int().
Classes, enumerations, typedef names, function types, or array types cannot be
declared or defined in the conversion_type. You cannot use a conversion function to
convert an object of typeAto typeA, a base class ofA, or void.
Conversion functions have no arguments, and the return type is implicitly the
conversion type. Conversion functions can be inherited. You can have virtual
conversion functions but not static ones.
Copy Constructors
The copy constructor lets you create a new object from an existing one by
[ Pobierz całość w formacie PDF ]
Podobne
- Start
- AQA GCSE polish second language specyfikacja 2005
- J. SZTUMSKI WstćÂp do metod i technik badaśÂ spośÂecznych
- Elliott Pickart Joan Posluchaj glosu serca
- Hohl Joan A jednak
- Jade Phoenix Falcon Guard
- Rennison Louise Zwierzenia Georgii Nicolson 08 MiśÂośÂć ci wszystko wypaczy
- DeSoto Lewis Malarz nadziei
- Janez Jalen Bobri 3, Vrh
- Tadeusz Boy śąeleśÂski SśÂówka
- Conan at the Demon's Gate Roland J. Green
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- aceton.keep.pl