[xsd-users] handling auto_ptr?
Boris Kolpackov
boris at codesynthesis.com
Sun Dec 28 06:58:17 EST 2008
Hi Azadeh,
Azadeh Omrani <a.omrani at gmail.com> writes:
> class A
> {
> auto_ptr<b::B> p;
> public:
> void parseData(string); //fills the memory pointed by p using xsd.
>
> void?? getParsedData(auto_ptr<b::B>??);
>
> A();
> ~A();
> }
>
> void main()
> {
> A * a =new A();
> a->parseData("inst.xml");
>
> // How can I have the parsed data here without nullifying the private
> // parameter p? (coz I will need p locally later)
Note that this is not a question about XSD but rather about std::auto_ptr
and it is answered in your favorite C++ book.
It is not possible to have two instances of std::auto_ptr pointing to the
same object. So what you can do is return a normal pointer or reference,
for example:
b::B& A::getParsedData()
{
return *p;
}
This way, however, you need to make sure that the 'a' instance is around
for as long as you have pointers/references to the object model.
Alternatively, you can use a smart pointer that support the sharing
semantics, e.g., Boost shared_ptr:
class A
{
shared_ptr<b::B> p;
public:
void parseData (string s)
{
auto_ptr<b::B> tmp = ... // parse s
p = shared_ptr<b::B> (tmp.release ());
}
shared_ptr<b::B> getParsedData ()
{
return p;
}
};
> Actually I want to know how to make a whole copy of the memory
> pointed by an auto_ptr without loosing the source and without
> double parsing.
You can use _clone() for this:
auto_ptr<b::B> A::getParsedData()
{
return auto_ptr<b::B> (p->_clone ());
}
Boris
More information about the xsd-users
mailing list