bahoo wrote:
Quote:
On Dec 21, 3:28 am, Salt_Peter <pj_h...@yahoo.comwrote:
Quote:
>On Dec 21, 1:47 am, bahoo <b83503...@yahoo.comwrote:
>>
>>
>>
>>
Quote:
>>I have an array that I need to define inside a function, and later
>>use it in another function.
>>
Quote:
>>main()
>>{
>>Object* arr = NULL;
>>
Quote:
>>func1( arr );
>>func2 (arr );
>>
>>
>>
Quote:
>>void func1(Object* arr)
>>{
>>arr = new ...
>>
>>
Quote:
>>But this doesn't seem to work. Can anyone tell me what's wrong with
>>it?
>>
>>
>pass the array by reference instead:
>>
>#include <iostream>
>>
>template < typename T, const size_t SIZE >
>void func( T(& array)[SIZE])
>{
> for(size_t i = 0; i < SIZE; ++i)
> {
> std::cout << "array[" << i << "] ";
> std::cout << array[i] << std::endl;
> }
>>
>}
>>
>int main()
>{
> int myarray[] = { 11, 22 };
> func(myarray);
>>
>}
>>
>/*
>array[0] 11
>array[1] 22
>*/
>
Sorry but this doesn't directly address my original question.
I have three functions, main + func1 + func2, and the object is NOT
created in the main function.
Also, I don't know the "SIZE", so I cannot make it a function argument
as you suggested by " void func( T(& array)[SIZE])".
Are you storing the size of the image in the object? First off if you are
using pointers in your class, you should make your copy constructor and
assignment operators private so that they can't be called for right now.
When you find you need to actually copy construct or assign your object
you'll need to write correct copy constructor and assignment operators (rule
of three).
Now, you are using a pointer to create this object using new (which may or
may not be a good thing.). As long as you make sure to work on that pointer
itself, and not it's value, then you should be looking at the object that
was created with new. Passing a reference to the pointer is one of the best
ways to do this, although you may also want to look at usages of smart
pointers.
You say that the size seems to change from one call to another, it sounds to
me like you may not be looking at the same instance in one of your calls
then. At this point to determine what is going wrong, we need to see some
code. Otherwise we can just guess that the error is on line 42 of your
program.
--
Jim Langston
tazmaster@rocketmail.com