M
ma740988
Experimenting with smart pointers here but need a refresher here.
Consider the snippet:
# include <iostream>
# include <string>
using namespace std;
class ying {
public:
ying () {}
~ying() { cout << " deleted ying " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }
ying& operator=(const ying& o )
{ cout << " op= ying " << endl;
return *this; }
ying(const ying& o )
{cout << " cc ying " << endl; }
};
class yang
{
public:
yang () {}
~yang() { cout << " deleted yang " << endl; }
yang& operator=(const yang& o )
{ cout << " op= yang " << endl;
return *this; }
yang(const yang& o )
{cout << " cc yang " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }
};
class bar {
yang *ptr_ya;
ying *ptr_yi;
public:
bar ( yang* ptr1, ying* ptr2 )
: ptr_ya(ptr1)
, ptr_yi(ptr2)
{}
void test_yang() { ptr_ya->run_it(); }
void test_ying() { ptr_yi->run_it(); }
~bar() { delete ptr_ya; delete ptr_yi; }
};
int main()
{
yang *ptr_ya = new yang();
ying *ptr_yi = new ying();
bar b(ptr_ya, ptr_yi);
b.test_yang();
b.test_ying();
// no longer necessary. this is why we're better off with
// auto_ptrs (the smart 'pointer' stuff)
//delete ptr_ya;
//delete ptr_yi;
}
I've haven't retrofitted the source yet to use smart pointers,
nonetheless, simple question. The ying and yang objects are passed
(pass by pointer) to bar constructors. That said, it's bars
responsibility to delete ying and yang. Correct?
Now I'm off to retrofit this to use smart pointer and examine the
behavior.
Thanks
Consider the snippet:
# include <iostream>
# include <string>
using namespace std;
class ying {
public:
ying () {}
~ying() { cout << " deleted ying " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }
ying& operator=(const ying& o )
{ cout << " op= ying " << endl;
return *this; }
ying(const ying& o )
{cout << " cc ying " << endl; }
};
class yang
{
public:
yang () {}
~yang() { cout << " deleted yang " << endl; }
yang& operator=(const yang& o )
{ cout << " op= yang " << endl;
return *this; }
yang(const yang& o )
{cout << " cc yang " << endl; }
void run_it()
{ cout << typeid(*this).name() << endl; }
};
class bar {
yang *ptr_ya;
ying *ptr_yi;
public:
bar ( yang* ptr1, ying* ptr2 )
: ptr_ya(ptr1)
, ptr_yi(ptr2)
{}
void test_yang() { ptr_ya->run_it(); }
void test_ying() { ptr_yi->run_it(); }
~bar() { delete ptr_ya; delete ptr_yi; }
};
int main()
{
yang *ptr_ya = new yang();
ying *ptr_yi = new ying();
bar b(ptr_ya, ptr_yi);
b.test_yang();
b.test_ying();
// no longer necessary. this is why we're better off with
// auto_ptrs (the smart 'pointer' stuff)
//delete ptr_ya;
//delete ptr_yi;
}
I've haven't retrofitted the source yet to use smart pointers,
nonetheless, simple question. The ying and yang objects are passed
(pass by pointer) to bar constructors. That said, it's bars
responsibility to delete ying and yang. Correct?
Now I'm off to retrofit this to use smart pointer and examine the
behavior.
Thanks