4 Replies - 437 Views - Last Post: 26 March 2010 - 04:58 AM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Returning A Templated Object

Posted 25 March 2010 - 02:39 PM

Here is the situation, I want to write a function that would return a templated object. What syntax should I use to declare and define such a function? The object i will be returning is defined below:

template <class Type>
class vertSet
{
   private:

          Type Set[3];

   public:

          Type return_x(void){ return Set[0]; }
          Type return_y(void){ return Set[1]; }
          Type return_z(void){ return Set[2]; }
          Type return_w(void){ return Set[3]; }

          Type set_x(Type x){Set[0] = x;}
          Type set_y(Type y){Set[1] = y;}
          Type set_z(Type z){Set[2] = z;}
          Type set_w(Type w){Set[3] = w;}

          vertSet(Type x,y,z,w)
          {
            set_x(x);
            set_y(y);
            set_z(z);
            set_w(w);
          }
};



Thanks all

Is This A Good Question/Topic? 0
  • +

Replies To: Returning A Templated Object

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,562
  • Joined: 09-June 09

Re: Returning A Templated Object

Posted 25 March 2010 - 03:26 PM

take a look at this example, it should help
#include <iostream>

using namespace std;

enum Point {X,Y,Z,W};
template <class T>
class vertSet
{
	T Set[4]; //you need 4 

public:
	T returnPoint(Point index){ return Set[index]; }
	void setValue(T value, Point index){ Set[index] = value; }
	vertSet(T x, T y, T z, T w)
	{
		Set[0] = x;
		Set[1] = y;
		Set[2] = z;
		Set[3] = w;
	}

	friend ostream &operator<<(ostream &stream, vertSet &obj)
	{
		stream<<"X : "<<obj.Set[0]<<endl
			  <<"Y : "<<obj.Set[1]<<endl
			  <<"Z : "<<obj.Set[2]<<endl
			  <<"W : "<<obj.Set[3]<<endl;
		return stream;
	}

};

int main()
{
	vertSet<int> test(1,2,3,4);
	cout<<test<<endl;

	test.setValue(3,X);
	test.setValue(4,Y);
	test.setValue(5,Z);
	test.setValue(6,W);
	cout<<test<<endl;

	cin.ignore();
	cin.get();



	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Returning A Templated Object

Posted 25 March 2010 - 03:42 PM

Can I return the entire object? I intend to use vertSet in another class that will have a vector of vertSet objects.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,562
  • Joined: 09-June 09

Re: Returning A Templated Object

Posted 25 March 2010 - 03:56 PM

yeah just return "this" (a pointer to the current class), or just return another created one
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,758
  • Joined: 16-October 07

Re: Returning A Templated Object

Posted 26 March 2010 - 04:58 AM

Ok, this is bugging the crap out of my. Why an array?
template <class T>
class VertSet {
private:
	// why?
	// Type Set[3];
	T x,y,z,w;

public:
	VertSet(T x, T y, T z, T w) { set_x(x); set_y(y); set_z(z); set_w(w); }

	T get_x() const { return x; }
	T get_y() const { return y; }
	T get_z() const { return z; }
	T get_w() const { return w; }

	void set_x(T value) { x = value; }
	void set_y(T value) { y = value; }
	void set_z(T value) { z = value; }
	void set_w(T value) { w = value; }
};



Hell, for that matter, why all the getters and setters:
template <class T> struct VertSet { T x,y,z,w; }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1