K
Kevin Christopher
I've got a pile of code that won't compile for me, and can't figure out
why. I'm either making a template / friend / operator overloading
mistake (most likely - & why I'm asking here), or I'm using something
not supported by g++ 2.96 (less likely).
The big picture: I'm writing a wrapper class template that is supposed
to wrap around a primitive data type so that I can send myself
notifications whenever some code changes that data type (i.e. read from
or write to). My idea was a wrapper that overloads the operator=()
function. Works great, except for assigning with operator=(int,
wrapper) which doesn't compile. Demonstration code attached (somewhat
long, but all the relevant functions are there). Demonstration code
runs as is right now... there are three commented-out sections dealing
with the one friend function that I need to get compiling.
Can anyone tell me what I'm doing wrong? The g++ error message is:
operator=(base_t&, const notify_wrapper<base_t>)&) must be a
nonstatic member function
and I don't feel that it has to be - this is an external helper function
for when I want to do: (base_t) = wrapper<base_t>. And since base_t is
a primitive type, I can't add this as a member function.
(code follows as attachment)
-Kevin
#include <iostream>
class wrapper_listener
{
public:
virtual void written_to(void) {cout << "written" << endl;}
virtual void read_from(void) {cout << "read" << endl;}
};
template <typename base_t>
class notify_wrapper
{
public:
notify_wrapper() {}
~notify_wrapper() {}
public:
operator base_t ();
notify_wrapper<base_t>& operator=
(const notify_wrapper<base_t>& rhs);
notify_wrapper<base_t>& operator=
(const base_t& rhs);
/*
friend
void operator=<base_t>
(base_t& lhs, const notify_wrapper<base_t>& rhs);
*/
private:
base_t base;
public:
static wrapper_listener* listener;
};
/*
template <typename base_t>
void operator=
(base_t& lhs, const notify_wrapper<base_t>& rhs);
*/
template <typename base_t>
notify_wrapper<base_t>:perator base_t
()
{
return this->base;
}
template <typename base_t>
notify_wrapper<base_t>&
notify_wrapper<base_t>:perator=
(const notify_wrapper<base_t>& rhs)
{
base = (base_t) rhs.base;
if(rhs.listener) rhs.listener->read_from();
if(listener) listener->written_to();
}
template <typename base_t>
notify_wrapper<base_t>&
notify_wrapper<base_t>:perator=
(const base_t& rhs)
{
base = rhs;
if(listener) listener->written_to();
return *this;
}
/*
template <typename base_t>
void //disallow future assignments!
:perator=
(base_t& lhs, const notify_wrapper<base_t>& rhs)
{
lhs = (base_t) rhs.base;
if(listener) listener->read_from();
}
*/
wrapper_listener* notify_wrapper<int>::listener;
int main()
{
int i;
wrapper_listener wrapper;
notify_wrapper<int> tester;
notify_wrapper<int>::listener = &wrapper;
tester = 5;
i = tester;
return 0;
}
why. I'm either making a template / friend / operator overloading
mistake (most likely - & why I'm asking here), or I'm using something
not supported by g++ 2.96 (less likely).
The big picture: I'm writing a wrapper class template that is supposed
to wrap around a primitive data type so that I can send myself
notifications whenever some code changes that data type (i.e. read from
or write to). My idea was a wrapper that overloads the operator=()
function. Works great, except for assigning with operator=(int,
wrapper) which doesn't compile. Demonstration code attached (somewhat
long, but all the relevant functions are there). Demonstration code
runs as is right now... there are three commented-out sections dealing
with the one friend function that I need to get compiling.
Can anyone tell me what I'm doing wrong? The g++ error message is:
operator=(base_t&, const notify_wrapper<base_t>)&) must be a
nonstatic member function
and I don't feel that it has to be - this is an external helper function
for when I want to do: (base_t) = wrapper<base_t>. And since base_t is
a primitive type, I can't add this as a member function.
(code follows as attachment)
-Kevin
#include <iostream>
class wrapper_listener
{
public:
virtual void written_to(void) {cout << "written" << endl;}
virtual void read_from(void) {cout << "read" << endl;}
};
template <typename base_t>
class notify_wrapper
{
public:
notify_wrapper() {}
~notify_wrapper() {}
public:
operator base_t ();
notify_wrapper<base_t>& operator=
(const notify_wrapper<base_t>& rhs);
notify_wrapper<base_t>& operator=
(const base_t& rhs);
/*
friend
void operator=<base_t>
(base_t& lhs, const notify_wrapper<base_t>& rhs);
*/
private:
base_t base;
public:
static wrapper_listener* listener;
};
/*
template <typename base_t>
void operator=
(base_t& lhs, const notify_wrapper<base_t>& rhs);
*/
template <typename base_t>
notify_wrapper<base_t>:perator base_t
()
{
return this->base;
}
template <typename base_t>
notify_wrapper<base_t>&
notify_wrapper<base_t>:perator=
(const notify_wrapper<base_t>& rhs)
{
base = (base_t) rhs.base;
if(rhs.listener) rhs.listener->read_from();
if(listener) listener->written_to();
}
template <typename base_t>
notify_wrapper<base_t>&
notify_wrapper<base_t>:perator=
(const base_t& rhs)
{
base = rhs;
if(listener) listener->written_to();
return *this;
}
/*
template <typename base_t>
void //disallow future assignments!
:perator=
(base_t& lhs, const notify_wrapper<base_t>& rhs)
{
lhs = (base_t) rhs.base;
if(listener) listener->read_from();
}
*/
wrapper_listener* notify_wrapper<int>::listener;
int main()
{
int i;
wrapper_listener wrapper;
notify_wrapper<int> tester;
notify_wrapper<int>::listener = &wrapper;
tester = 5;
i = tester;
return 0;
}