B
Bit byte
I have the following methods:
static void Foo::setBar(const Bar*) ; //store a copy of Bar
static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr
to our internal copy
In another part of my code , I retrieved and used Bar as follows:
....
const Bar* temp = NULL ;
....
//retrieve and use
temp = Foo::getBar();
.....
delete temp ; // <- compiler did not complain (what's happend ? - has
Foo::Bar really been deleted?)
Basicaly, this is what I want to do:
1). store a copy of Bar 'safely' in Foo
2). Be able to request a ptr to Bar and use it without being able to
modify it
3). I want Foo::setBar() to be able to replace (i.e. destroy first, then
replace) the original Foo::Bar* variable
Questions:
1). How do I do that?
I was doing this:
class Foo
{
public:
Foo(){ m_bar = NULL ;}
/* I need these static so I can use Bar* in some 'stand alone' C
callback functions*/
static void setBar(const Bar* b) {
if (m_bar) delete m_bar ;
m_bar = CopyOf(b) ;
}
static const B* getBar(void)const { return m_bar ; }
private:
Bar* m_bar ;
}
whats wrong with the above?
static void Foo::setBar(const Bar*) ; //store a copy of Bar
static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr
to our internal copy
In another part of my code , I retrieved and used Bar as follows:
....
const Bar* temp = NULL ;
....
//retrieve and use
temp = Foo::getBar();
.....
delete temp ; // <- compiler did not complain (what's happend ? - has
Foo::Bar really been deleted?)
Basicaly, this is what I want to do:
1). store a copy of Bar 'safely' in Foo
2). Be able to request a ptr to Bar and use it without being able to
modify it
3). I want Foo::setBar() to be able to replace (i.e. destroy first, then
replace) the original Foo::Bar* variable
Questions:
1). How do I do that?
I was doing this:
class Foo
{
public:
Foo(){ m_bar = NULL ;}
/* I need these static so I can use Bar* in some 'stand alone' C
callback functions*/
static void setBar(const Bar* b) {
if (m_bar) delete m_bar ;
m_bar = CopyOf(b) ;
}
static const B* getBar(void)const { return m_bar ; }
private:
Bar* m_bar ;
}
whats wrong with the above?