In message <11**********************@g49g2000cwa.googlegroups .com>,
vivekian <vi********@gmail.com> writes
The arguments passed will be a set of integers ( the size of the set is
variable ). Each argument will be assigned to a node in a linked list (
this is what the function does basically ). So the size of the linked
list depends on the number of arguments passed. I think the method
specified by Meager sounds good, though if there is a better way do let
know.
If you're adding elements one at a time, you might consider overloading
operator<<:
// warning - incomplete skeleton code for illustration only
class MyType {
public:
void add(int i);
// etc...
};
MyType & operator<<(MyType & obj, int arg)
{
obj.add(arg);
return obj;
}
MyType x;
x << arg1 << arg2 << arg3; // etc
If your set of arguments constitute some kind of a sequence, you could
have a templated function that takes two iterators representing the
first and one-beyond-the end elements. This is similar to meagar's
solution, but more flexible, since you aren't constrained to pass a
vector, but can use any kind of representation that behaves like an
input iterator - for example you could use an istream_iterator to read
the data from a file.
template <typename Iterator>
void AddElements(MyType & obj, Iterator p, Iterator end)
{
for (; p!=end; ++p)
obj.add(*p);
}
MyType x;
AddElements(x, istream_iterator<int>(cin), istream_iterator<int>());
In this case you could alternatively make this a member function of
MyType and omit the first argument.
--
Richard Herring