Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

assigning an array

Posted on 1998-07-13
2
Medium Priority
?
257 Views
Last Modified: 2010-04-01
i have an array class and i want to be able to initialize the array with a string of values or variable names, much like:

array<int> AnArray(6) = {1, 2, 3, 4, 5, 6};

or

array<int> AnArray(6);
AnArray.AssignArray(1, 2, 3, 4, 5, 6);

i have tried to do this but have the following problem.  if i made an AssignArray function it would have to take a variable number of parameters because different arrays have different number of elements.  i know overloading would take care of this but it would get tedious to make the same function over and over changing only the number of arguments, especially with arrays of hundreds of elements.  
do any of you know a better way to do this?
0
Comment
Question by:Booth882
2 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 320 total points
ID: 1167620
There are several issues at work here.

First, a class can be initialized by an initialization list *only if* it does not have constructors, private or protected members, base classes, or virtual functions.

Second, a constructor (like any other function) can take a variable number of arguments.

For example:


    #include <stdarg.h>

    const int ArraySize = 5;

    class Array
    {
    public:
        Array(int, ...);

    private:
        int a[ArraySize];
    };


    Array::Array(int num, ...)
    {
        va_list args;
        va_start(args, num);
        for (int i = 0; i < num; ++i)
            a[i] = va_arg(args, int);
        va_end(args);
    }

    int main()
    {
        Array a(ArraySize, 1, 2, 3, 4, 5);

        return 0;
    }

Note however that no compile-time checking will be done on the arguments represented by the "..."
0
 
LVL 1

Author Comment

by:Booth882
ID: 1167621
thank you much!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question