![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
custom array class: operator[] not working for new dynamic allocationHello,
I created an array class called floatArrayFrank that replaces the C float array. I initialize it with the quantity of floats it containts. My floatArrayFrank class works fine when I allocate it on the stack, but if I do an allocation on the stack with new the operator[] does not compile properly. The rest of my program requires that I can reallocate as needed. CPP / C++ / C Code:
What can I do to fix this? I include my class. Thanks, Frank CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: custom array class: operator[] not working for new dynamic allocationQuote:
I won't get into the details of program design and certain no-nos of your code. In other words, I won't try to debug the class itself. I will point out a few technical errors that you need to overcome before you get to the "good stuff". (First of all, the statement a[10]= 3.14 is missing a semicolon, but I assume that you didn't have that particular error in your actual test code.) Anyhow: CPP / C++ / C Code:
The comment is correct. These statements declare a single object and initialize it with itsArraySize = 40. The variable a is a pointer whose value is the address of the newly created object. However, the following statement: CPP / C++ / C Code:
Tries to assign a floating point value to the 10th element of an array of floatArrayFrank objects. In C and C++, there is a very fundamental notation definition. The following is always true: If x is a pointer data type and n is an integer data type then the following two notations are semantically identical x[n] is exactly the same as *(x + n). And I mean exactly. Not usually always. Not almost the same as. Exactly. Therefore, since a is a pointer, and 10 is an integer, the code is trying to make an assignment to the 10th element of an array. (The compiler only knows that you declared a to be a pointer. It does not keep track of the nature of your declaration, which only allocated memory for one object. It doesn't work that way.) This can produce a run-time error, since you are accessing memory that doesn't belong to you. Going on to the next statement: CPP / C++ / C Code:
If you really want one object, and you create it the way that you did a, then, since a is a pointer, you must dereference it in order to access the object itself. So, we could have: CPP / C++ / C Code:
See footnote And you might see: Code:
So, both of the assignment statements now use the [] operator. And for the rest of the program: CPP / C++ / C Code:
Now the results are the same with d as they were with *a As far as the [] operators, the one that you have returning a value doesn't make sense to me. Maybe you have seen things like: CPP / C++ / C Code:
These would be used to allow the [] operator to work with const objects. One final note: Your so-called "copy constructor" is not a copy constructor. It is an overloaded assignment operator. You don't have a copy constructor. A copy constructor is declared like any other constructor (just the class name; no return type). The argument of a copy constructor is a reference to an object of the class. A copy constructor would be invoked by something like CPP / C++ / C Code:
(And if you don't create a copy constructor, then the last statement uses the default copy constructor --- with a "shallow" copy. That is probably fatal at run time for your class.) Regards, Dave Footnote: Consistent with what I said about equivalence of pointer and index notation, you could access the element with a[0][10], and the result would be exactly the same as (*a)[10] Last edited by davekw7x : 05-Sep-2007 at 12:28.
|
|
#3
|
|||
|
|||
Re: custom array class: operator[] not working for new dynamic allocationThanks a lot for your input!
Yes, the big fix was to do (*a)[10]. I tried to improvise with all kinds of erroneous variants like a->[10]. Your a[0][10] suggestion is pretty cool too. Thanks also for pointing to me that I labeled wrongly the assignment operator and have no copy constructor. Of course thinking I had one when I did not, thus allowing the default shallow copy. You found a big programming error, thanks! >Maybe you have seen things like: >const float & floatArrayFrank: >These would be used to allow the [] operator to work with const objects. Would I need this only if I chain two functions, like below? CPP / C++ / C Code:
Best regards, Frank |
|
#4
|
|||
|
|||
Re: custom array class: operator[] not working for new dynamic allocation[quote=transfrank]
Quote:
Quote:
CPP / C++ / C Code:
May not be useful for your class, but that's the idea. Regards, Dave |
Recent GIDBlog
Last Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 19:23 |
| polynomial class can't return a full dynamic array | JKSung5295 | CPP / C++ Forum | 2 | 13-Oct-2006 00:19 |
| Need help deleting the last element in the array | headphone69 | CPP / C++ Forum | 2 | 15-Mar-2006 19:31 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | CPP / C++ Forum | 0 | 19-Aug-2005 09:25 |
| template comiling problems - need expert debugger! | crq | CPP / C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The