V
V
Vitaly Yakovenko2011-12-04 17:51:16
C++ / C#
Vitaly Yakovenko, 2011-12-04 17:51:16

Handling slightly different structures with one function in C++

Help a newbie. There are two structures:

struct valuenode {
    char elements [5];
    int power;
};
struct indexnode {
    char elements [5];
    int elementindex [10];
};

There is also a function that generates some array of structures of the first type (valuenode), here is its declaration:
int pushlevelnodes (valuenode * valuesarray,int beginatindex=0,char toplevel=1) {

In this function, only the elements[5] subarray is used, power is not touched. Whether it is possible to use the same function at generation of an array of structures of the second type? The difference between the structures is in the further processing of values ​​by other functions, the initial array is generated in the same way. I wouldn't want to duplicate quite a lot of generation code. If you can, poke your nose like. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Sidorov, 2011-12-04
@Xazzzi

template method? Or inherit these two structures from a common ancestor?

D
Dmitry Dedukhin, 2011-12-04
@Demetros

union?

C
Cthutq66a, 2011-12-04
@Cthutq66a

Why wouldn't you do that?
You can give the function an array of char* elements as an input.
And call from valuenode.elements and indexnode.elements.

Q
QTsik, 2015-02-27
@QTsik

Create a separate function foo to work with elements [5]. Then create two functions to work with different structures and use foo in them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question