The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn't seem entirely correct. It also doesn't mention
whether static member functions can access protected and private
member data and methods (and I couldn't spot this in the FAQ).
I have a class row<Row> which derives from row_base:
template<typena me Row>
class row : public row_base
{
public:
typedef std::auto_ptr<R ow> row_ptr;
row(): row_base() {}
row(row_state status, bool modified=false) :
row_base(status , modified) {}
virtual ~row() {}
static row_ptr create(pqxx::re sult::const_ite rator row,
pqxxobject::tra nsaction& tran)
{
row_ptr p(new Row);
p->convert_impl(r ow, tran); // protected
p->row_base::m_st ate = STATE_INITIALIS ED; // private
return p;
}
virtual void convert_impl(pq xx::result::con st_iterator row,
pqxxobject::tra nsaction& tran) = 0;
}; // class row
Here, in the static create() method, row_base::m_sta te is a private
(enum) data member of the base class, yet I can assign it directly.
However, convert_impl() is a pure virtual function which is public
here and protected in the deriving class, but I get an error when I
compile:
places.cc: In static member function `static std::auto_ptr<_ Tp1>
pqxxobject::row <Row>::create(p qxx::result::co nst_iterator,
pqxxobject::tra nsaction&) [with Row = Place]':
.../pqxx-object/table.h:172: instantiated from `std::auto_ptr< std::list<Row, std::allocator< _CharT> > > pqxxobject::tab le<Row>::find_m any(const std::string&) [with Row = Place]'
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_ impl(pqxx::resu lt::const_itera tor, pqxxobject::tra nsaction&)'
is protected
.../pqxx-object/row.h:97: error: within this context
row.h:97 is the "p->convert_impl(r ow, tran);" line, above.
I can't see the rationale between being able to access private data
members, but not protected methods (which are actually public in the
row<> class)!
I would probably be better making create() call a specialised
protected constructor, but I'm interested in learning why the above
doesn't work.
Thanks,
Roger
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member variables".
However, this doesn't seem entirely correct. It also doesn't mention
whether static member functions can access protected and private
member data and methods (and I couldn't spot this in the FAQ).
I have a class row<Row> which derives from row_base:
template<typena me Row>
class row : public row_base
{
public:
typedef std::auto_ptr<R ow> row_ptr;
row(): row_base() {}
row(row_state status, bool modified=false) :
row_base(status , modified) {}
virtual ~row() {}
static row_ptr create(pqxx::re sult::const_ite rator row,
pqxxobject::tra nsaction& tran)
{
row_ptr p(new Row);
p->convert_impl(r ow, tran); // protected
p->row_base::m_st ate = STATE_INITIALIS ED; // private
return p;
}
virtual void convert_impl(pq xx::result::con st_iterator row,
pqxxobject::tra nsaction& tran) = 0;
}; // class row
Here, in the static create() method, row_base::m_sta te is a private
(enum) data member of the base class, yet I can assign it directly.
However, convert_impl() is a pure virtual function which is public
here and protected in the deriving class, but I get an error when I
compile:
places.cc: In static member function `static std::auto_ptr<_ Tp1>
pqxxobject::row <Row>::create(p qxx::result::co nst_iterator,
pqxxobject::tra nsaction&) [with Row = Place]':
.../pqxx-object/table.h:172: instantiated from `std::auto_ptr< std::list<Row, std::allocator< _CharT> > > pqxxobject::tab le<Row>::find_m any(const std::string&) [with Row = Place]'
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_ impl(pqxx::resu lt::const_itera tor, pqxxobject::tra nsaction&)'
is protected
.../pqxx-object/row.h:97: error: within this context
row.h:97 is the "p->convert_impl(r ow, tran);" line, above.
I can't see the rationale between being able to access private data
members, but not protected methods (which are actually public in the
row<> class)!
I would probably be better making create() call a specialised
protected constructor, but I'm interested in learning why the above
doesn't work.
Thanks,
Roger
--
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Comment