passing array to a constructor

This is a discussion on passing array to a constructor within the C++ Programming forums, part of the General Programming Boards category; Suppose I have a class like this: Code: class Polygon { public: Polygon(unsigned int, Vertex*, float, float, float); ~Polygon(); void ...

  1. #1
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552

    passing array to a constructor

    Suppose I have a class like this:
    Code:
    class Polygon
    {
        public:
        Polygon(unsigned int, Vertex*, float, float, float);
        ~Polygon();
        void Render();
        protected:
        Vertex* vertices;
        unsigned int VertexCount;
    };
    In my constructor the second parameter is for an array of a structure called Vertex. Now I got to coding it, and I got to a crossroads. Do I need to call new on vertices, and then set it to equal the parameter, or do I just need to set it to be equal to the parameter. I think that if I just set it to equal the parameter, that when the array I passed to it went out of scope, it won't work right, because vertices won't be pointing to anything; however, I don't know if I dynamically allocate vertices, and then set it to equal the parameter, that it would work right.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    I'd say make a copy to be safe.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I just did a test. If the variable goes out of scope, then it fails to work correctly, because the pointer has lost the variable.

    The same thing happens even if I dynamically allocate the array.

    Anyone got any ideas on how to get this to work?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    As long as you create the copy *before* the array goes out of scope, it will work. Care to post some code?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    how I usually do it:
    Code:
    Polygon::Polygon(Vertex* verts, int count) {
        if (count <= 0) {
            verts = NULL;
            return;
        }
        mVerts = new Vertex[count];
        for (int i = 0; i < count; i++) {
            mVerts[i] = verts[i];
        }
    }
    
    Polygon::~Polygon(void) {
        if (mVerts) {
            delete [] mVerts;
        }
    }
    Always copy the passed pointer (verts), once I've written a similar class, I thought that I could make sure not to pass something on the stack to the constructor, it went fine for a while, then my program (a game engine) started to give error messages on exit, after lots of debugging nightmares I figured out that I was passing a static variable to the constructor.

  6. #6
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I was thinking of doing that, but something compelled me not to for some reason, lol. Thx!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use a std::vector...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help passing nxn array to 2d array.
    By beglaryanh in forum C Programming
    Replies: 2
    Last Post: 06-06-2009, 05:23 PM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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