Solved

assigning an array

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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…
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 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…
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.

739 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