template function

hey lads,

Need your opinion over here,

for example we've got a structure with some elements in it

struct
{
char a[20];
char b[30];
int ab;
int abc;
}A[10];

if I want to write function which will edit the structure's elements, it is possible to make it like a template "edit" function for all the elements or should I write a function for each element of the structure in order to edit it?
Last edited on
Ultimately, an edit function is editing a string. The only thing that's different when editing a char array or an integer is how you validate the string and how you copy it to its final destination. So you might create an Editor class that includes a virtual isValid() method. When the user tries to save the result, you call isValid() to make sure what they type is valid and make them continue editing or cancel if it's invalid. When the data is valid, the caller can copy the resulting string to it's final destination, translating it to the right format along the way.
is this C or C++?

1
2
3
4
5
6
7
8
9
10
11
in c++ I would say

struct x //why not, we are going random one letter variables here for names
{
  char a[20];
  char b[30];
  int ab;
  int abc;
};
...  //later, in main or somewhere sensible out of global scope:
x A[10]; 

that moves the global variable, names the struct, and generally makes it a bit more modern approach.

that aside... c++ template? Not really appropriate IMHO.
first, its a struct, so directly editing the values may be fine:
A[0].a[0] = 1;
This violates OOP design, but, it is OK if you are not in the middle of an OOP designed part of the code and its just a simple piece of data that you want to store in a structured way for a bit. If you are ok with globals, this should also be ok

Or you can do a getter/setter member function for each item. Do you NEED something more complex than this?
Last edited on
thank you guys for help.
@jonnin- basically its C(I finished the basics of programming language), and that's a tiny part of my final lab.

One of My structures represents all personal information( name, surname, phone, dob, email and so on) of the future Students which are going to be inserted later through an interactive MENU(switch cases).

I was thinking of writing a function (which will be called using an interactive MENU) that will permit administrators and users( which represent other structure) to EDIT all the personal information(8 elements) of the structure Student and I was looking for a possibility of writing something like a template EDIT function instead of 8 different edit functions for each element of the structure...
I don't think C has embraced OOP; the only way I know to do it is function pointers inside the struct, which works great if you want to do it. But in C, I would go with the direct access approach. That can be inside a function or whatever --
if admin
function_edit_a(A, index, value)
else
print "access denied"
is doable

or if you had function pointers
if admin
A[index].function_pointer_call

something like this?

you can of course use a union or pointer casting to make a meta all in one editor for it. The structs have the bytes in a specific way, and you can hack right on into them. But its ugly, gains you nothing but ugliness and risks to portability and readability and debugging. I wouldn't.
Last edited on
@jonnin, thank you ! really helped
Topic archived. No new replies allowed.