S
Steven T. Hatton
I've run into a situation where the conventional thinking doesn't seem
satisfactory. I have a table of data where each row maps into a struct. I
want to put these into a vector. My overall design uses interfaces for
everything accessed from outside of the namespace. I also have a class
that acts as a wrapper for the structs. For example:
struct Row { long _col0; long _col1; };
class TableRow_IF {
public:
virtual long getCol0() const = 0;
virtual long getCol1() const = 0;
virtual ~TableRow_IF(){}
};
using std::istream;
class TableRow: public TableRow_IF {
bool _foreignEndian;
Row _row;
public:
TableRow(istream& in, istream:
os_type pos, bool foreignEndian_){
/*read the row data*/
}
virtual long getCol0() const {
return _foreignEndian? reendian(_row._col0): _row._col0;
}
virtual long getCol1() const {
return _foreignEndian? reendian(_row._col1): _row._col1;
}
virtual ~TableRow_IF(){}
};
That works. All the user needs to know about is TableRow_IF. But now I
want a collection of rows. I can't make a std::vector<TableRow_IF> because
that would require the instantiation of an abstract class. I can make a
std::vector<TableRow_IF*>, but that is less safe. Even if I use a smart
pointer, I might still inadvertently attempt to dereference a null pointer.
I can create a std::vector<TableRow>, but then I no longer have
polymorphism when I access the elements.
What I want is this:
class Table_IF {
// acts like a std::vector<TableRow_IF>, except it copmiles.
virtual TableInfo() const = 0;// information about the table
};
class Table: public Table_IF {
// derive from std::vector<TableRow> ??????
virtual TableInfo() const { return _info; }
//...
};
Is there a standard C++ pattern for addressing this kind of situation?
This may prove to be the best approach for the entire project:
http://www.research.att.com/~bs/matrix.c
I believe it's what I will try now for the situation described above.
satisfactory. I have a table of data where each row maps into a struct. I
want to put these into a vector. My overall design uses interfaces for
everything accessed from outside of the namespace. I also have a class
that acts as a wrapper for the structs. For example:
struct Row { long _col0; long _col1; };
class TableRow_IF {
public:
virtual long getCol0() const = 0;
virtual long getCol1() const = 0;
virtual ~TableRow_IF(){}
};
using std::istream;
class TableRow: public TableRow_IF {
bool _foreignEndian;
Row _row;
public:
TableRow(istream& in, istream:
/*read the row data*/
}
virtual long getCol0() const {
return _foreignEndian? reendian(_row._col0): _row._col0;
}
virtual long getCol1() const {
return _foreignEndian? reendian(_row._col1): _row._col1;
}
virtual ~TableRow_IF(){}
};
That works. All the user needs to know about is TableRow_IF. But now I
want a collection of rows. I can't make a std::vector<TableRow_IF> because
that would require the instantiation of an abstract class. I can make a
std::vector<TableRow_IF*>, but that is less safe. Even if I use a smart
pointer, I might still inadvertently attempt to dereference a null pointer.
I can create a std::vector<TableRow>, but then I no longer have
polymorphism when I access the elements.
What I want is this:
class Table_IF {
// acts like a std::vector<TableRow_IF>, except it copmiles.
virtual TableInfo() const = 0;// information about the table
};
class Table: public Table_IF {
// derive from std::vector<TableRow> ??????
virtual TableInfo() const { return _info; }
//...
};
Is there a standard C++ pattern for addressing this kind of situation?
This may prove to be the best approach for the entire project:
http://www.research.att.com/~bs/matrix.c
I believe it's what I will try now for the situation described above.