R
Roger Leigh
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<typename Row>
class row : public row_base
{
public:
typedef std::auto_ptr<Row> row_ptr;
row(): row_base() {}
row(row_state status, bool modified=false):
row_base(status, modified) {}
virtual ~row() {}
static row_ptr create(pqxx::result::const_iterator row,
pqxxobject::transaction& tran)
{
row_ptr p(new Row);
p->convert_impl(row, tran); // protected
p->row_base::m_state = STATE_INITIALISED; // private
return p;
}
virtual void convert_impl(pqxx::result::const_iterator row,
pqxxobject::transaction& tran) = 0;
}; // class row
Here, in the static create() method, row_base::m_state 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(pqxx::result::const_iterator,
pqxxobject::transaction&) [with Row = Place]':
.../pqxx-object/table.h:172: instantiated from `std::auto_ptr<std::list<Row, std::allocator<_CharT> > > pqxxobject::table<Row>::find_many(const std::string&) [with Row = Place]'
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_impl(pqxx::result::const_iterator, pqxxobject::transaction&)'
is protected
.../pqxx-object/row.h:97: error: within this context
row.h:97 is the "p->convert_impl(row, 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
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<typename Row>
class row : public row_base
{
public:
typedef std::auto_ptr<Row> row_ptr;
row(): row_base() {}
row(row_state status, bool modified=false):
row_base(status, modified) {}
virtual ~row() {}
static row_ptr create(pqxx::result::const_iterator row,
pqxxobject::transaction& tran)
{
row_ptr p(new Row);
p->convert_impl(row, tran); // protected
p->row_base::m_state = STATE_INITIALISED; // private
return p;
}
virtual void convert_impl(pqxx::result::const_iterator row,
pqxxobject::transaction& tran) = 0;
}; // class row
Here, in the static create() method, row_base::m_state 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(pqxx::result::const_iterator,
pqxxobject::transaction&) [with Row = Place]':
.../pqxx-object/table.h:172: instantiated from `std::auto_ptr<std::list<Row, std::allocator<_CharT> > > pqxxobject::table<Row>::find_many(const std::string&) [with Row = Place]'
places.cc:207: instantiated from here
places.cc:175: error: `virtual void
Place::convert_impl(pqxx::result::const_iterator, pqxxobject::transaction&)'
is protected
.../pqxx-object/row.h:97: error: within this context
row.h:97 is the "p->convert_impl(row, 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