templates - passing the same template value to two different template functions

I have been trying to get a hang on templates, if you have actually a better suggestion in terms of design I'm more than happy to hear:

I have the two following functions that that could be consolidated in a single template function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

void Attractor::updateFamilies(FamiliesController *_tmp, int _counter){
    
    center.x = ofGetWidth()/2;
    center.y = ofGetHeight()/3;
    attractorCounter = _counter;
    
    if(attractorCounter == 1){
        
        attractor1(_tmp); 
        
    } else if (attractorCounter == 2){ 
        
        attractor2(_tmp); 
    } else if (attractorCounter == 3){
        
        attractor3(_tmp); 
        
    } else if (attractorCounter == 4){
        
        attractor4(_tmp); 
        
    }
    
}


void Attractor::updateNotes(NotesController *_tmp, int _counter){
    
    center.x = ofGetWidth()/2;
    center.y = ofGetHeight()/3;
    attractorCounter = _counter;
    
    if(attractorCounter == 1){
        
        attractor1(_tmp); 
        
    } else if (attractorCounter == 2){ 
        
        attractor2(_tmp); 
    } else if (attractorCounter == 3){
        
        attractor3(_tmp); 
        
    } else if (attractorCounter == 4){
        
        attractor4(_tmp); 
        
    }
    
}


NotesController and FamiliesController have the same parent. The thing that I'm trying to grasp with templates is that is could something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template<class TYPE>
void Attractor::updateData(TYPE* *_tmp, int _counter){
    
    center.x = ofGetWidth()/2;
    center.y = ofGetHeight()/3;
    attractorCounter = _counter;
    
    if(attractorCounter == 1){
        
        attractor1(_tmp); 
        
    } else if (attractorCounter == 2){ 
        
        attractor2(_tmp); 
    } else if (attractorCounter == 3){
        
        attractor3(_tmp); 
        
    } else if (attractorCounter == 4){
        
        attractor4(_tmp);         
    }    
}


And then have another template function declaration for all the attractor functions where I pass the same template value as in the first one.

As you can see, I'm calling another functions inside called attractors(_tmp). I know that one way around it could be to get rid of that function and just do all the logic inside of each if statement. Is there any way to pass the same template function parameter within a template function parameter?
Last edited on
> NotesController and FamiliesController have the same parent.
¿why don't you ask for the parent then?
¿what do the attractor{1..4}() functions receive as parameter? (you could use polymorphism there)


1
2
3
4
void Attractor::updateNotes(NotesController *_tmp, int _counter){

template<class TYPE>
void Attractor::updateData(TYPE* *_tmp, int _counter){
¿don't you see an extra asterisk? (replace TYPE)


The template parameter may be deduced by the types of the arguments of the function. So you wouldn't need to specify it.
If you must, then you could simply do attractor4<TYPE>(_tmp);
@ne555 they receive the same parameter that I'm passing in the updateNotes family and function. so either NotesController or FamiliesController.

And yes it worked!!! I didn't know I had to pass the parent, but that fixed it! I guess I don't need to use a template yet I figured. I did this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

void Attractor::update(ParticleController *_tmp, int _counter){ // PArticleController is the parent class for both NotesController and FamiliesController
    
    center.x = ofGetWidth()/2;
    center.y = ofGetHeight()/3;
    attractorCounter = _counter;
    
    if(attractorCounter == 1){
        
        attractor1(_tmp); // circle (2 Attractor moving same direction in same circle)

    } else if (attractorCounter == 2){ // PROBLEM HERE
        
        attractor2(_tmp); // figure-8 (2 Attractor passing boids back n forth)
        
    } else if (attractorCounter == 3){
        
        attractor3(_tmp); // flower (like circle Attractor but radius flux'es on a sine wave)
        
    } else if (attractorCounter == 4){
        
        attractor4(_tmp); // flower (like circle Attractor but radius flux'es on a sine wave)
        
    }

}


I have a feeling that passing the same value to the attractor functions is bad design. I don't come from a CS background so I could be wrong.

Thanks anyway!

Topic archived. No new replies allowed.