3 Replies - 1756 Views - Last Post: 04 April 2010 - 01:08 PM Rate Topic: -----

#1 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Insert template class object in vector

Posted 04 April 2010 - 12:45 PM

How to insert template class object in a vector? This is my class definition :

namespace Stat {
	template <typename DataType1, typename DataType2, typename DataType3>
	class IntervalClass {
		public :
			DataType1 LowerBound;
			DataType2 UpperBound;
			DataType3 Frequency;
			IntervalClass() { }
			IntervalClass(DataType1 _LowerBound, DataType2 _UpperBound, DataType3 _Frequency) {
				LowerBound = _LowerBound;
				UpperBound = _UpperBound;
				Frequency = _Frequency;
			}
	};
}

In main() I want to insert the above class object into a vector. For doing this I am following this method :
#include "statistics.hpp"
#include <iostream>

using namespace std;

int main() {	
	vector<Stat::IntervalClass<double, double, int> > container;
	
	container.push_back(Stat :: IntervalClass<double, double, int> (12.5, 17.5, 2));
	
	Stat :: IntervalClass<double, double, int> Object(17.5, 22.5, 22);
	container.push_back(Object);
	
	return 0;
}

Which one is better :
container.push_back(Stat :: IntervalClass<double, double, int> (12.5, 17.5, 2));

Or
Stat :: IntervalClass<double, double, int> Object(17.5, 22.5, 22);
container.push_back(Object);


If I have a function in a namespace, how can I define it? This way?
namespace ex {
	void fun();
}
void ex :: fun() {
}



If I have a function in a class in a namespace, how can I define it? This way?

namespace ex {
	class cl {
		void fun();
	};
}

void ex :: cl :: fun() {
}



Is This A Good Question/Topic? 0
  • +

Replies To: Insert template class object in vector

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Insert template class object in vector

Posted 04 April 2010 - 01:00 PM

In your case, I'd go with the first method. The second method provides access to public member functions of your class, so you can modify the class before adding it to a vector. The first method doesn't provide access the public member functions, but it's not the case for that here, as you don't want to modify the class before adding it to the vector.
About the next questions, the answer would be yes.

This post has been edited by sarmanu: 04 April 2010 - 01:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Insert template class object in vector

Posted 04 April 2010 - 01:02 PM

Thank you sir.
Was This Post Helpful? 0
  • +
  • -

#4 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Insert template class object in vector

Posted 04 April 2010 - 01:08 PM

Is initializing some public data member by constructor valid? Valid in the sense does it follow OOP? I have another template function within same namespace but not within that class. So I can not make these variable private.
Can we declare template function as friend, static or virtual?

This post has been edited by Tapas Bose: 04 April 2010 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1