Jump to content

  • Log In with Google      Sign In   
  • Create Account

Is it Possible to have a function operate on 2 different input structure


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 ankhd   Members   -  Reputation: 1284

Like
0Likes
Like

Posted 21 May 2014 - 03:32 AM

Hi.

 

I have 2 structures like this

 

struct PNT

{

vector3 Pos;

//others

}

 

and

 

struct PNTWB

{

vector3 pos;//the p is lower case

}

 

 

if I use a template function like this

template<class type>

void dostuf()

{    type.Pos = vector3(0,0,0);

}

 

but the second structure has a lower case pos

does it still work.

 

 

 

GetExtentsX



Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2623

Like
1Likes
Like

Posted 21 May 2014 - 03:36 AM

May I refer you to a thread I started a week ago, where basically the same question is asked and answered.

 

http://www.gamedev.net/topic/656753-member-variable-as-template-paramter/

 

Basically, you have to add another special template paramter, look at the answers in the thread, should be the first one.



#3 RnaodmBiT   Members   -  Reputation: 643

Like
2Likes
Like

Posted 21 May 2014 - 05:49 AM

The simpler version instead of using templates is to just define the function twice with two different parameters. Then when the compiler link the program, it picks the function that matches the correct parameter template.

 

Ie.

 

void dostuff(PNT var)

{

    var.Pos = vector3(0, 0, 0);

}

 

void dostuff(PNTWB var)

{

    var.pos = vector3(1, 2, 3);

}



#4 Ashaman73   Crossbones+   -  Reputation: 7517

Like
1Likes
Like

Posted 21 May 2014 - 07:13 AM

The first question is, can you change the structure code ? Or is it some thirdparty code you dont have access to ?

 

Some options:

1. Define an access method, if you have access to the code.
 

struct PNT
{
  vector3 Pos;
  //others

 inline vector3& accessPos() { return Pos;}
}
struct PNTWB
{
vector3 pos;//the p is lower case
 inline vector3& accessPos() { return pos;}
}
 

2. Use a wrapper instead of the structures.

class PNT_Wrapper
{
PNT& target;
PNT_Wrapper(PNT& _target) {...}

inline vector3& accessPos() { return target.Pos;}
}
class PNTWB_Wrapper
{
PNTWB& target;
PNTWB_Wrapper(PNTWB& _target) {...}

inline vector3& accessPos() { return target.Pos;}
}





#5 ankhd   Members   -  Reputation: 1284

Like
-2Likes
Like

Posted 21 May 2014 - 07:33 AM

Thanks all.

 

Looks Like this is the only way

 

struct PNT

{

vector3 Pos;

//others

inline vector3& accessPos() { return Pos;}

} ;



#6 Aardvajk   Crossbones+   -  Reputation: 5982

Like
4Likes
Like

Posted 21 May 2014 - 08:17 AM

If you have access to the code, then why are you using different capitalization formats for your member variables? Pick a system and stick to it.



#7 SeanMiddleditch   Members   -  Reputation: 5872

Like
4Likes
Like

Posted 21 May 2014 - 09:25 AM

You have a ton of options depending on what you're doing.

For one, you could just standardize the code a bit more. Use the same naming convention everywhere. Then your templates will just work (assuming you write them correctly):
 
template <typename T>
void stuff (T& t) {
  t.pos = vec3(0,0,0);
}

You could also use inheritance, though this is almost certainly not what you want:
 
struct base {
  vec3 pos;
};
struct foo : base {
};
struct bar : base {
};

void stuff(base& b) {
  b.pos = vec3(0,0,0);
}

A far more natural approach may be to just pass in the member values you need, assuming you're only using a small number of them. If you have a lot, use composition/aggregation.
 
struct transform {
  vec3 pos;
  vec3 scale;
};

struct foo {
  transform trans;
};

struct bar {
  transform TransSpelledDifferently;
};

void stuff(transform& t) {
  t.pos = vec3(0,0,0);
}

foo f;
bar b;
stuff(f.trans);
stuff(b.TransSpelledDifferently);

Failing that, you can use accessor functions outside of your class, which plays well with ADL.
 
vec3& get_pos(PINT& p) { return p.pos; }
vec3& get_pos(PNTWB& p) { return p.Pos; }

template <typename T>
void stuff1(T& t) {
  get_pos(t) = vec3(0,0,0);
}

void stuff2(vec3& pos) {
  pos = vec3(0,0,0);
}
stuff2(get_pos(whatever));

There's more ways, too, though some get particularly advanced/complex without much benefit over the above approaches.

C++ is a multi-paradigm language. There are many ways to solve any problem. So long as you consistently apply the particular paradigms in your code (mix them as needed, only when needed), this doesn't get too confusing.

#8 ankhd   Members   -  Reputation: 1284

Like
-2Likes
Like

Posted 21 May 2014 - 05:51 PM

Thanks for the unfair down vote. the choice I selected was the only option for me the code base is 8 years old and them structure are used in other programs and if I

Change them to much if I need to go back to any old app Ill be having a lot of work.

 

If you have access to the code, then why are you using different capitalization formats for your member variables? Pick a system and stick to it. It's Old code

Forgive me for not rememering what I wrote in a structure 4 years ago.

 

I do now have a system and I keep it all the same nameing.

 

 

 

A far more natural approach may be to just pass in the member values you need, assuming you're only using a small number of them. If you have a lot, use composition/aggregation.
 
I can't It locks a mesh and gets the min and max vertex elements of the mesh. See I made a template locking function that take a mesh and locks and maps it.

 

 

 

I like this way

 
vec3& get_pos(PINT& p) { return p.pos; }
vec3& get_pos(PNTWB& p) { return p.Pos; }

template <typename T>
void stuff1(T& t) {
  get_pos(t) = vec3(0,0,0);
}

void stuff2(vec3& pos) {
  pos = vec3(0,0,0);
}
stuff2(get_pos(whatever));

Thank All.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS