I read this code section from a tutorial, but I cannot understand it thoroughly. It is about Pointer & Class Pointer & Array Pointer.

This is it:

// pointer to classes example
#include <iostream>
using namespace std;

class CRectangle {
	private:
		int width, height;
	public:
		// prototype
		void set_values (int,int);
		// method
		int area (void) {
			return (width * height);
		}
};

// definion
void CRectangle :: set_values (int a, int b) {
	width = a;
	height = b;
}

void main () {
	CRectangle a, *b, *c;
	[b]CRectangle * d = new CRectangle[2];[/b]
	//
	b = new CRectangle;
	c = &a;
	a.set_values(1,2);
	b -> set_values(3,4);
	[b]d -> set_values(5,6);
	d[1].set_values(7,8);[/b]
	//
	cout << "a area: " << a.area() << endl;
	cout << "*b area: " << b -> area() << endl;
	cout << "*c area: " << c -> area() << endl;
	[b]cout << "d[0] area: " << d[0].area() << endl;
	cout << "d[1] area: " << d[1].area() << endl;[/b]
	//
	delete[] d;
	delete b;	
}

I try to search google and understand some parts of this code. But when it contains many concepts (i.e. pointer, array, class) in the BOLD section, I cannot grasp the points. Many operators (*, ->, etc. ), and I failed to distinguish between them.

Please explain it for me, especially the section in BOLD.

BTW, I see the "new" operator is very different from java one, is it right?

Thanks for any help.

Recommended Answers

CRectangle * d = new CRectangle[2];

That is just allocating an array of 2 CRectangle objects. It could have been written like then: [icode]CRectangle d[2];[/icode]. But if it did that then it could not replace the 2 with something else at runtime.

d -> …

Jump to Post

[B]>I see the "new" operator is very different from java one, is it right?[/B]
Not especially. The Java new expression creates an object of the specified type, calls the specified constructor, and returns a reference to the object. This is pretty much identical to C++ where a new expression creates …

Jump to Post

All 3 Replies

Re: Please explain some concepts about Pointer & Class & Array Pointer 80 80

>>CRectangle * d = new CRectangle[2];

That is just allocating an array of 2 CRectangle objects. It could have been written like then: CRectangle d[2]; . But if it did that then it could not replace the 2 with something else at runtime.

>>d -> set_values(5,6);
That is the same thing as d[0].set_values(5,6) only it uses -> operator. When using an array I prefer d[0].xxx instead of the pointer operator because it more clearly describes what that code is doing.

commented: Thanks. +1
Re: Please explain some concepts about Pointer & Class & Array Pointer 80 80

>I see the "new" operator is very different from java one, is it right?
Not especially. The Java new expression creates an object of the specified type, calls the specified constructor, and returns a reference to the object. This is pretty much identical to C++ where a new expression creates an object of the specified type, calls the specified constructor, and returns a pointer to the object. The only real difference (at the superficial level you should be comparing C++ and Java) is you can do more with a C++ pointer than a Java reference.

commented: Thank for your info. +1
Re: Please explain some concepts about Pointer & Class & Array Pointer 80 80

Thank Ancient Dragon. I get it now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.