Skip to main content

Thread: C++ and Templates


i trying read data file , store structures. these structures quite similar, couple of fields. example:

code:
typedef struct {    int a;    int b;    char c; } struct_a;  typedef struct {    int a;    int b;    float d; } struct_b
i use struct_a make list of such objects, collected in structure (overstruct_a). same goes on struct_b , overstruct_b (i have 4 of such structures, 2 of used in 1 overstruct , other 2 in other, i'll keep example simple).

code:
typedef struct {    struct_a* pa; } overstruct_a;   typedef struct {    struct_b* pb; } overstruct_b;
everything similar, , since function fills lists of struct_a , struct_b contained in overstruct_a , overstruct_b respectively elaborated, keep templetized, managing code easier (or have type manually every modification twice).
function looks this:

code:
template <typename t, typename r> int func (t* struct, r& overstruct) { /* return irrelevant in example */ }
in end, function work list, struct head of 1 of lists (type either struct_a or struct_b) , overstruct struct containing head of list (type ovestruct_a or overstruct_b respectively).
need pass ovestruct because function needs conceal work done on head of list (which automatically ordered while data read file).
since have more 2 struct_x types, necessary pass struct separately overstruct, specify 1 of struct in overstruct want work with.

tried:

code:
if (typeid (t) == typeid (struct_a)) {overstruct.pa} if (typeid (t) == typeid (struct_b)) {overstruct.pb}
but error since when ovestruct == overstruct_a, compiler rightly argue there no field pb in overstruct_a.

aside doing this:
code:
template <typename t> t* func (t* struct) {/* head of list may change, scope of struct inside func, pass pointer new head returning value */}   int main () {   ...   struct_a.pa = func (pa);   struct_b.pb = func (pb);   ... }
how can solve problem alternatively?
tried explore option of passing pointer reference, people try discourage (i'm not sure why, though). other ideas?

there's no need typedef struct in c++. struct , class identical in every aspect except one: members in struct public, unless otherwise indicated. class, members private, unless otherwise indicated.

in mind, should consider augmenting structures provide friend method can accept reference istream object can read data file itself.

example (and code has not been compiled/tested):
code:
struct  {      int a;      int b;      char c;        friend std::istream& operator>>(std::istream& is, a& obja)      {          >> obja.a >> obja.b >> obja.c;          return is;      }  };    struct b  {      int a;      int b;      float d;        friend std::istream& operator>>(std::istream& is, b& objb)      {          >> objb.a >> objb.b >> objb.d;          return is;      }  };    int main()  {      a;      b b;        std::fstream file("datafile.txt", std::fstream::in);        if (file)      {          file >> a;          file >> b;      }  }
alternatively, if structures have common data, can create base structure, along specialized sub-classes (err, structures). example:
code:
struct base  {      int a;      int b;  };    struct : public base  {      char c;        ...  };    struct b: public base  {      float d;        ...  };
using templates may over-complicating require. or perhaps did not understand requirements.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C++ and Templates


Ubuntu

Comments

Popular posts from this blog

Some mp4 files not displaying correctly (CS6)

Thread: Samba is not authenticating with LDAP