How can I do the following? I read up on Variable Arguments last time I asked this question (Mike pointed me in the right direction).. but it still does not let me define any number of them.. I Do not want to specify the first parameter and if I do, it has to be a custom type.

Class M
{
	private:
	  vector<MyTypes> P;
	  
	//Constructor..
	//Destructor..
	  
	M& Insert(...)//or M& Insert(MyTypes, ...)   //where ... can ONLY be "MyTypes"
	{
	   for (int I = 0; I < #ofArgs; I++)
		P.push_back(Args[I]);
		
	   return *this
	}
}

I looked at doing it via templates but how can I even do that when I'm trying to do it in a class and access it without having to put template<.....> in front of it every single time.

Recommended Answers

All 2 Replies

I don't have the context of your previous conversation, but why would something like a vector not work for you?

M& Insert(std::vector< MyTypes >& args) {
   for (int I = 0; I < args.size(); ++I)
      P.push_back(Args[I]);
   return *this;
}

Because when I do something like:

int main()
{  
   M Arr;
   Arr.Insert(MyTypes, MyTypes, MyTypes......... up to any amount);
}

it gives an error saying function only takes 1 argument. Too many arguments in function call.. initial value of reference to non-const must be an l-value..

now if I do:

template<typename T, typename... Args>
		M& Insert(const T& first, const Args&... args)
		{
			for (unsigned I = 0; I < Args; I++)
				P.push_back(Args[I]);
			return *this;
		}

It gives me errors about missing comma's before ... and missing comma before &.. etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.