Solved

assigning an array

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now