Need Help on Pointers

Hi everyone,

I started programming a few months ago, and complicated syntax has kept torturing me since then. What I understand about a pointer is that a pointer can point to the address of other variable. But when a pointer is declared in the class as its member, I do not understand as to what exactly pointers point to and their meaning. Also I don't know the syntax for access functions and call expressions for those pointer. I created a class Cars whose private members are pointers. Any help will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

class cars{
private:
        int *size; // What is the difference between this and next pointer?
	cars *brand;
	
public:
	

	// syntax of access functions' for each pointer

};

int main(){
	// syntax to call two pointers, size and brand
	

	system("pause");
	return 0;
}


Thank you for your help.

Sincerely,
Justin
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

A pointer is an address. Pointers don't point to addresses - they point to objects.

Deliberation wrote:
What is the difference between this and next pointer?
The difference is the type of object they point to.

In general, you should avoid using raw pointers unless you have to.
Thank you so much for your help LB :)

I have a short question about a pointer.
So you mentioned that pointer is an address itself, and pointer points to an object.

Two pointers in my example were int *size and Cars *brand inside the class Cars. So for the first pointer int is an object, and for the second pointer Cars is an object?
I am curious of how Cars, which was declared as a class, can be an object. Does object always occupy some memory in free store?

Justin
Last edited on
I think part of the problem here is that the purpose of the pointers in class cars is not clear. Why are size and brand pointers at all? What will they be used for?

I am curious of how Cars, which was declared as a class, can be an object.

You are correct that it is a class. What this means is that the object that the pointer points to is of type cars.
Thank you for your help!
an object is an instance of a class.

the object is representing the class . int * size , points to an integer value. Note also it can points to the first element of an integer array if the memory has been dynamically allocated. The thing is the size of both pointers is the same , their content althought is not , size points to an integer or integer array ; brand points to a car instance , which contains an integer pointer and a car pointer .
Topic archived. No new replies allowed.