J
JKop
AnyClass Blah()
{
AnyClass poo;
return poo;
}
As we all know, in the above, the compiler is entitled to:
1) Create the object "poo"
2) Create a copy of "poo"
3) Destroy "poo"
4) Return the copy
or
1) Create the object "poo"
2) Return "poo"
On account of the first choice, whether a copy is made or not, the copy
constructor must be public.
First of all, I think it would be handy if we had a new type of "return"
statement in C++, one which makes it so that the actual object is returned
(no copy made) and as such the copy constructor wouldn't need to be
public... allowing the likes of the following:
std:stream Blah()
{
std:stream poo;
return_actual_object poo;
}
Anyway...
To achieve this, at the moment I'm using auto_ptr, as follows:
auto_ptr<AnyClass> Blah()
{
auto_ptr<AnyClass> poo( new AnyClass );
return poo;
}
int main()
{
auto_ptr<AnyClass> const &milk = Blah();
//now work with it as I please!
//and if I want reference syntax:
AnyClass const& cheese = *milk;
cheese.MemberFunction();
}
The compiler still has the choice of copying the auto_ptr... but it's a hell
of a lot better than copying the entire object! Plus, I can return an
"std:stream" by value... (kind of!).
AAnnyywwaayy... looking through a C++ reference today I was sort of shocked
to see:
basic_string::substr
basic_string substr(size_type off = 0,
size_type count = npos) const;
The member function returns an object whose controlled sequence is a copy of
up to count elements of the controlled sequence beginning at position off.
It returns by value! AAAAAAaaahhhhhhhh! throw Inefficiency(); !
-JKop
{
AnyClass poo;
return poo;
}
As we all know, in the above, the compiler is entitled to:
1) Create the object "poo"
2) Create a copy of "poo"
3) Destroy "poo"
4) Return the copy
or
1) Create the object "poo"
2) Return "poo"
On account of the first choice, whether a copy is made or not, the copy
constructor must be public.
First of all, I think it would be handy if we had a new type of "return"
statement in C++, one which makes it so that the actual object is returned
(no copy made) and as such the copy constructor wouldn't need to be
public... allowing the likes of the following:
std:stream Blah()
{
std:stream poo;
return_actual_object poo;
}
Anyway...
To achieve this, at the moment I'm using auto_ptr, as follows:
auto_ptr<AnyClass> Blah()
{
auto_ptr<AnyClass> poo( new AnyClass );
return poo;
}
int main()
{
auto_ptr<AnyClass> const &milk = Blah();
//now work with it as I please!
//and if I want reference syntax:
AnyClass const& cheese = *milk;
cheese.MemberFunction();
}
The compiler still has the choice of copying the auto_ptr... but it's a hell
of a lot better than copying the entire object! Plus, I can return an
"std:stream" by value... (kind of!).
AAnnyywwaayy... looking through a C++ reference today I was sort of shocked
to see:
basic_string::substr
basic_string substr(size_type off = 0,
size_type count = npos) const;
The member function returns an object whose controlled sequence is a copy of
up to count elements of the controlled sequence beginning at position off.
It returns by value! AAAAAAaaahhhhhhhh! throw Inefficiency(); !
-JKop