Array of my class in dynamic memory - return problem

First of all, I'm sorry if the title isn't the best one, I couldn't think of a better one without just explaining the whole problem there.

I'm a very, very erm.. beginning beginner. I'm currently trying to understand how use dynamic memory. I understand the very basics of it, but now I'm stuck. I have a little test program but I can't seem to figure out what I'm doing wrong here. Everytime I give it a valid input it returns "=". I'm not sure if it's because I'm doing something wrong with the dynamic memory stuff, or because I'm making an elemental mistake with the implicit string[index] -> char conversion. Either way, I hope someone can point out what I'm doing wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Main.cpp
#include "CClass.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	int iIndex = 3;
        int iInput;
	string  strString = "Hello!";

	cout << "Please enter a number: ";
	cin >> iInput;
	
	CClass * cclPointer = new CClass[5];
	cclPointer[iIndex].fnSetChar(strString[iInput]);

	cout << "cChar of cclPointer[" << iIndex << "] = " << cclPointer->fnGetChar();
	cin >> iInput;

	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//CClass.h + .cpp
class CClass
{
public:
	char fnGetChar();
	void fnSetChar(char);

private:
	char cChar;
};


#include "CClass.h"


char CClass::fnGetChar()
{
	return cChar;
}

void CClass::fnSetChar(char cInput)
{
	cChar = cInput;
}
Last edited on
cout << "cChar of cclPointer[" << iIndex << "] = " << (cclPointer+3)->fnGetChar();
or
cout << "cChar of cclPointer[" << iIndex << "] = " << cclPointer[3].fnGetChar();

please read more.. here
http://cplusplus.com/doc/tutorial/pointers/
http://cplusplus.com/doc/tutorial/dynamic/
Ehm.. of course, makes sense, I can't believe I missed that. I did it right in the line above that line, right? :/
Thanks (and my apologies).
Topic archived. No new replies allowed.