Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

assigning an array

Posted on 1998-07-13
2
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

715 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