Click here to Skip to main content
15,885,546 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: IAccessible, enable/disable, ... Pin
Albert Holguin5-Nov-14 4:34
professionalAlbert Holguin5-Nov-14 4:34 
QuestionStatic linking Pin
Member 112017882-Nov-14 22:27
Member 112017882-Nov-14 22:27 
AnswerRe: Static linking Pin
Garth J Lancaster2-Nov-14 22:37
professionalGarth J Lancaster2-Nov-14 22:37 
AnswerRe: Static linking Pin
Richard MacCutchan2-Nov-14 23:09
mveRichard MacCutchan2-Nov-14 23:09 
AnswerRe: Static linking Pin
Albert Holguin3-Nov-14 11:31
professionalAlbert Holguin3-Nov-14 11:31 
GeneralRe: Static linking Pin
Member 112017883-Nov-14 23:31
Member 112017883-Nov-14 23:31 
AnswerRe: Static linking Pin
Albert Holguin4-Nov-14 4:24
professionalAlbert Holguin4-Nov-14 4:24 
QuestionTemplate Class Pointer - Would like explanation for unexpected behavior Pin
Foothill2-Nov-14 8:01
professionalFoothill2-Nov-14 8:01 
I am expanding my knowledge of class templates so I am building a very basic template of type Array<T>.
I'll include only the code that pertains to the behavior that is confusing me.

Template Header
C++
// MyArray.h

#include <stdexcept>

#define CORE_ARRAY_DEFAULT_LENGTH 20

template <class T>
class Array
{
private:
T** _array;
int _size;
int _length;

public:
Array(void)
{
_array = new T*[CORE_ARRAY_DEFAULT_LENGTH]();
_size = CORE_ARRAY_DEFAULT_LENGTH;
_length = 0;
}
~Array(void) { Clear(); }

int Size(void) const { return _size; }
int Length(void) const { return _length; }

T operator [] (int index) const
{
if ((index < 0) || (index >= _length))
   throw std::out_of_range("index");
else
   return * _array[index];
}

void Add(T value)
{
if (_length == _size)
   Resize();

_array[_length] = new T(value);
++_length;
}

/* Other functions follow CopyArray(), CloneArray(), Resize(), Clear(), InsertAt(), etc... */
}


And the source
C++
#include "MyArray.h"
#include <cstdlib>
#include <cstdio>

void main(void)
{
// This behaves as expected
Array<int> iArrayA;

for (int i = 0; i < 10; ++i)
   iArrayA.Add(i);
for (int i = 0; i < 10; ++i)
   printf("%d\n", iArrayA[i]);

// But this was returning odd results
Array<int> * iArrayB = new Array<int>();

for (int i = 0; i < 10; ++i)
   iArrayB->Add(i);  // confirmed that the values were stored correctly
for (int i = 0; i < 10; ++i)
   printf("%d\n", iArrayB[i];  // outputs were not what I expected

delete iArrayB;

system("pause");
}


Here is what I had to change it to to get the desired results:
C++
printf("%d\n", iArrayB[0][i]);


What I don't get here is why when the array was declared as a local (Array<int> iArrayA;) did the accessor [] pull the stored int at the correct element but when I had the array declared as a pointer on the heap (Array<int> * iArrayB = new iArray<int>();) the accessor [] was seemingly pulling random data until I added the [0] before the [i]?

I am wondering if this has something to do with overloading the array subscript operator. Which is what has jumped out at me while I am writing this.

Also, is there a way I can code the accessor to work for both locals and heap pointers? Is there any way around this?
AnswerRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
Richard MacCutchan2-Nov-14 8:13
mveRichard MacCutchan2-Nov-14 8:13 
GeneralRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
Foothill2-Nov-14 8:44
professionalFoothill2-Nov-14 8:44 
AnswerRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
Freak303-Nov-14 2:50
Freak303-Nov-14 2:50 
GeneralRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
Foothill3-Nov-14 5:59
professionalFoothill3-Nov-14 5:59 
GeneralRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
the_foobar3-Dec-14 20:26
the_foobar3-Dec-14 20:26 
AnswerRe: Template Class Pointer - Would like explanation for unexpected behavior Pin
Volynsky Alex4-Nov-14 23:12
professionalVolynsky Alex4-Nov-14 23:12 
QuestionERROR No Target Architecture Pin
ForNow1-Nov-14 17:05
ForNow1-Nov-14 17:05 
SuggestionRe: ERROR No Target Architecture Pin
Richard MacCutchan1-Nov-14 22:03
mveRichard MacCutchan1-Nov-14 22:03 
GeneralRe: ERROR No Target Architecture Pin
ForNow2-Nov-14 4:04
ForNow2-Nov-14 4:04 
GeneralRe: ERROR No Target Architecture Pin
Richard MacCutchan2-Nov-14 5:49
mveRichard MacCutchan2-Nov-14 5:49 
QuestionHow to protect a member function Pin
Drugodrf31-Oct-14 4:35
Drugodrf31-Oct-14 4:35 
AnswerRe: How to protect a member function Pin
Jochen Arndt31-Oct-14 5:18
professionalJochen Arndt31-Oct-14 5:18 
AnswerRe: How to protect a member function Pin
Chris Losinger31-Oct-14 5:18
professionalChris Losinger31-Oct-14 5:18 
GeneralRe: How to protect a member function Pin
Drugodrf3-Nov-14 2:31
Drugodrf3-Nov-14 2:31 
AnswerRe: How to protect a member function Pin
Albert Holguin3-Nov-14 11:39
professionalAlbert Holguin3-Nov-14 11:39 
AnswerRe: How to protect a member function Pin
Aescleal4-Nov-14 2:29
Aescleal4-Nov-14 2:29 
GeneralRe: How to protect a member function Pin
Drugodrf4-Nov-14 7:41
Drugodrf4-Nov-14 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.