0

I am reading some open-source code, and am confused by the use of pointers. Can anybody help me analyze the following code?

for (int i = 0; i < podRecords; i++)
{
    WaterRight *pRight = new WaterRight;
    pRight->m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);
    int reachID = pRight->m_reachComid; 

So, my understanding is that by creating new WaterRight, the memory that stores the address of WaterRight members is dynamically allocated. Then the value (or address?) of m_colReachComid is assigned to m_reachComid, and then assigned to reachID. I am always not sure which identifier is the address and which is the value. For example, is reachID an integer value, or is it an address that stores the value?

simonc
  • 41,632
  • 12
  • 85
  • 103
James
  • 99
  • 9
  • Read some good [C++ tutorial](http://www.cplusplus.com/doc/tutorial/) and some good [tour of C++](http://www.stroustrup.com/Tour.html). Use Linux. Then compile the code with warnings & debugging enabled `g++ -Wall -g` and use the `gdb` debugger (e.g. to put a breakpoint then step each statement) – Basile Starynkevitch Jul 22 '14 at 19:43
  • For people first trying to get into C++, I would really not recommend `gdb` immediately... – Suedocode Jul 22 '14 at 19:52
  • I am using visual studio. I don't think I am going to be a programmer/software developer, but I have to know some basics since I am using codes from others without documentation. – James Jul 22 '14 at 20:02

4 Answers4

2

reachID is an int value. You store numbers there.

pRight is a pointer to a WaterRight. It stores the address of some WaterRight.

pRight->m_reachComid is also an int value. Specifically it is the m_reachComid field of the WaterRight instance pointed to by pRight.

So:

WaterRight *pRight = new WaterRight;
// pRight is a *pointer to a WaterRight instance*: 
// you use pRight->m_reachComid to access the instance's m_reachComid field
pRight->m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);

WaterRight right;
// right is a *WaterRight instance* 
// you use pRight.m_reachComid to access the instance's m_reachComid field
pRight.m_reachComid = m_podDb.GetAsInt(m_colReachComid, i);
gpeche
  • 21,974
  • 5
  • 38
  • 51
0

The line WaterRight *pRight = new WaterRight; declares a pointer to an object of a class called WaterRight that was just initialized using the default constructor of that class. This class has a member variable called m_reachComid which is set in your next line as the return value of m_podDb.GetAsInt(m_colReachComid, i) (whatever that function may be). Finally, you declare reachId (an integer) and set it equal to whatever value is in your m_reachComid variable. But as Basile says in the comments, you really should start with a good tutorial or a good book.

Community
  • 1
  • 1
wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • Thank you all. Yes. I should start with a good book. Actually I've read some tutorial for beginners. I thought I understand pointers once, but without practice, it is very easy to return to 0. – James Jul 22 '14 at 19:51
0

reachID is an int, pRight is a pointer the "->" causes a derefernce of the pointer "pRight->m_reachComid" could be rewriten as "(*pRight).m_reachComid"

mpop
  • 499
  • 11
  • 21
0

Assuming that GetAsInt may return one of these case:

Case 1, type check ok (integer as expected)

If pRight->m_reachComid is integer, you are right:

int reachID = pRight->m_reachComid; // int <- int

Case 2, type check wrong

If it's not, and pRight->m_reachComid is another variable (struct, class, but not pointers) you will receive an error (size is different)
int reachID = pRight->m_reachComid; // int <- ???

Case 3, storing a pointer inside an int

If pRight->m_reachComid is a pointer to something, for example a pointer to int, you will receive a warning
But you will able to store a pointer inside an integer (not a great idea, it's quite stupid/useless/dangerous), because (in 32bit mode!!)
sizeof(int) = 4 and sizeof(int*) = sizeof(anyType*) = sizeof(void*) = 4

Case 4, storing with cast

If pRight->m_reachComid is double or float, you can cast it (and truncate the decimal part)

int reachID = (int)pRight->m_reachComid;

However, int reachID IS an integer, you can store everything inside but it will always be an integer

incud
  • 541
  • 1
  • 9
  • 17