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

Started by
6 comments, last by ankhd 9 years, 11 months ago

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

Advertisement

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.

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);

}

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;}
}



Thanks all.

Looks Like this is the only way

struct PNT

{

vector3 Pos;

//others

inline vector3& accessPos() { return Pos;}

} ;

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.

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.

Sean Middleditch – Game Systems Engineer – Join my team!

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.

This topic is closed to new replies.

Advertisement