Thread: pass array directly to new object

  1. #1
    Registered User
    Join Date
    Apr 2008

    pass array directly to new object

    Hi, I have an object that takes an array as a parameter of the constructor:
    class Foo {
    Foo(int array[]);
    int _array[];
    If I want an array of the above type, and I want to pass in the array in one go:
    Foo otherArray[5];
    otherArray[0] = Foo({1,2,3,4,5});
    How can I do that? It isnt happy?
    I tried changing the array in the class Foo to an int* and then also passing the size, i.e:
    Foo otherArray[5];
    otherArray[0] = Foo({1,2,3,4,5}, 5;
    but that didnt work either? It doesnt compile...


  2. #2
    Registered User
    Join Date
    May 2010
    A couple of things, first you can't define a zero length array, you need to specify the size of the array. Second since you defined a constructor that takes a parameter you no longer have a default no argument constructor so the following line is incorrect because it requires the default constructor.
    Foo otherArray[5];
    Have you considered using a std::vector instead of the array?

    I suggest you post a small complete program that illustrates your problem.


  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Zurich, Switzerland, Switzerland
    I believe this might help you:

    There you can learn that an array is passed as a parameter by passing its address.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    I like this one: Safer arrays in Cpp - cpwiki
    It may be a little tricky to understand, though, but it is essential to know when it comes to arrays in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to pass an array of object
    By kdun in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2013, 06:19 AM
  2. How do I pass a whole number directly to print
    By sharris in forum C++ Programming
    Replies: 7
    Last Post: 04-17-2011, 01:51 PM
  3. Pass object to function
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2008, 07:57 AM
  4. pass all the array as blob object in one field
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 11-12-2006, 01:27 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM