Board index » cppbuilder » Scope of object created within function call

Scope of object created within function call


2004-02-09 07:55:29 PM
cppbuilder10
I have 2 classes:
template <class Return, class Take>
FPTemp1
{
virtual
Return
operator () (Take) = 0;
};
template <class Type1, class Type2>
AUTO_SCAST :
public FPTemp1 <Type1*, Type2*>
{
Type1
*operator () (Type2 *aArg)
//Only member function
{
return static_cast <Type1*>(aArg);
}
};
I also have a function similar to this:
template <class Type1, class Type2>
void
Count(Type1*, Type2*,
FPTemp1 <Type1*, Type2*>* = &AUTO_SCAST <Type1, Type2>());
Note that the default argument is a pointer to the result of a constructor. I
have been using this for a long time, and it has never given me any problems.
So does this work similar to initialization in a control statement?:
bool Conf1 = true;
if (bool Conf2 = Conf1)
{
//I can use Conf2 here
};
//But not here
I should also note that 'AUTO_SCAST' has no data members: only member functions,
therefore even if it was a bad pointer, no members are accessed by the functions
that are called, therefore no AV's occur. Thanks.
ta0kira
 
 

Re:Scope of object created within function call

"ta0kira" < XXXX@XXXXX.COM >writes:
Quote
template <class Type1, class Type2>
void
Count(Type1*, Type2*,
FPTemp1 <Type1*, Type2*>* = &AUTO_SCAST <Type1, Type2>());


Note that the default argument is a pointer to the result of a constructor.
I have been using this for a long time, and it has never given me any
problems.

So does this work similar to initialization in a control statement?:
The expression
Count<int,double>()
is by (definition of "default function argument") equivalent to
Count<int,double>(&AUTO_SCAST<int,double>())
The lifetime of the temporary object which is the return value of the
sub-expression AUTO_SCAST<int,double>() extends to the evaluation of the
"full expression" that the Count expression is part of.
I.e. if you have a statement
Count<int,double>();
, the Count expression is the full expression. The temporary AUTO_CAST object
is destructed after the Count function returns.
 

Re:Scope of object created within function call

XXXX@XXXXX.COM (Thomas Maeder [TeamB]) wrote:
Quote
Count<int,double>();

, the Count expression is the full expression. The temporary AUTO_CAST object
is destructed after the Count function returns.
Outstanding. Good thing for me...
Thanks.
ta0kira
 

{smallsort}