pass array directly to new object

This is a discussion on pass array directly to new object within the C++ Programming forums, part of the General Programming Boards category; Hi, I have an object that takes an array as a parameter of the constructor: Code: class Foo { public: ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    pass array directly to new object

    Hi, I have an object that takes an array as a parameter of the constructor:
    Code:
    class Foo {
    public:
    Foo(int array[]);
    
    private:
    int _array[];
    }
    If I want an array of the above type, and I want to pass in the array in one go:
    Code:
    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:
    Code:
    Foo otherArray[5];
    otherArray[0] = Foo({1,2,3,4,5}, 5;
    but that didnt work either? It doesnt compile...

    Thanks

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    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.
    Code:
    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.

    Jim

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

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

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I like this one: SourceForge.net: 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, 12:27 PM
  5. Replies: 3
    Last Post: 04-02-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21