Solved

assigning an array

Posted on 1998-07-13
2
222 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 80 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 72
TCP/IP Socket - connection close results in data lost 14 132
Unresolved External Symbols 3 80
How do i run a c++ file? 15 24
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 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