4

I'm having some trouble understanding the use of pointers in this program:

#include<iostream.h>
class ABC
{
    public:
        int data;
        void getdata()
        {
            cout<<"Enter data: ";
            cin>>data;
            cout<<"The number entered is: "<<data;
        }
};
void main()
{
    ABC obj;
    int *p;
    obj.data = 4;
    p = &obj.data;
    cout<<*p;
    ABC *q;
    q = &obj.data;
    q->getdata();
}

I get everything until the following step : ABC *q;
What does that do? My book says it's a class-type pointer (it's very vague with pathetic grammar). But what does that mean? A pointer pointing to the address of the class ABC?

If it is, then the next step confuses me. q = &obj.data;
So we're pointing this pointer to the location of data, which is a variable. How does that ABC *q; fit in, then?

And the last step. What does q->getdata(); do? My book says it's a 'pointer to member function operator', but gives no explanation.

Glad to recieve any help!

10
  • 30
    Throw away that book. Commented Nov 22, 2013 at 12:36
  • 2
    I'm assuming that q = &obj.data equals q = &obj ? Commented Nov 22, 2013 at 12:37
  • First, the q is a pointer to an ABC class object, so q=&obj. Secondly q->getdata() will invoke the method for that instance of ABC Commented Nov 22, 2013 at 12:40
  • 4
    I'm curious - what's the title of that book? Or did you already throw it away? :D Commented Nov 22, 2013 at 12:45
  • It looks like the author is either making mistakes or making needless and hideous assumptions - ie: that a pointer to the first data member in a class is the same as a pointer to the object. Follow the copious advice already given, get a new book. Commented Nov 22, 2013 at 12:52

4 Answers 4

5

That book is wrong because it should be:

ABC *q;
q = &obj;
q->getdata();

Or using a int pointer:

ABC *q;
int *qq;
qq = &obj.data;
q = &obj;
q->getdata();
Sign up to request clarification or add additional context in comments.

3 Comments

actually because int data is the first member of the class &obj and &obj.data represent the same address. So it'd perfectly valid
No, It's not perfectly valid. The compiler will complain that it can't convert int* to ABC*. Yes, they will be the same address, but they aren't the same type, and therefore the original code won't compile.
@Pandrei Yeah, it's not only about the memory location. The data-types matter too.
3
ABC * q

This instruction creates to pointer to fragment of memory, where resides instance of class ABC. For example:

q = new ABC();

This instantiates ABC and stores address of that instance in q variable.

ABC abc;
q = &abc;

This instantiates ABC automatically (that means, compiler takes care of allocation and deallocation of that instance) and stores address to that class in q.

Also, -> is not pointer to member function operator. This is only shorter way of writing something else:

a -> b

equals

(*a).b

If you know, that a points to a class instance (or struct instance, what in C++ is more less the same) and you want to access a member (field or method) of that instance, you can quickly write a->b = 5; or a->DoSth(); instead of (*a).b = 5; or (*a).DoSth();.

7 Comments

I think OP was confused by q = &obj.data.
Fun fact -> isn't the only C++ operator with a simple definition like that. The array subscript operator a[b] is defined as *((a) + (b)) meaning that b[a] is just as valid.
"This instruction creates to pointer to fragment of memory, where resides instance of class ABC" I think this is wrong. ABC *p; only declares a pointer if you don't assign anything to the pointer than it has no meaning; think of what a compiler would generate for the declaration (assuming it does not strip away not used symbols)
That's quite fluent. It will still be a pointer to instance of ABC even though there is no actual instance. In C++ you can, for example, allocate a bunch of memory, cast it to ABC* and sometimes it will even work (if ABC is not polymorphic and if you allocated enough memory), but of course this is something you definitely don't want to do. The point is, that compiler always believes, that there is ABC in the place pointed to by p.
@Pandrei I'm sorry, I didn't understand that at all! :P
|
2
ABC obj;
ABC *q;
q = &obj.data;

should not compile.

More logical would be

q = &obj;

which assigns the address of obj (an instance of class ABC) to the pointer-to-ABC called q.

Once this is done, if you wanted to call getdata on q you use

q -> getdata();

...because q is a pointer. Compare this to what you'd do with obj (which is a stack variable)

obj.getdata();

Same function, called in different ways.

3 Comments

actually it compiles and it's perfectly fine. the problem would be in case of the following statement: q=&(obj.data) because now you are assigning a int* to a ABC* and the compiler can't do an implicit cast.
@Pandrei No, it doesn't compile. I tried it. The compiler highlighted the following line: q=&obj.data; and gave me the following error: "Cannot convert 'int*' to 'ABC*'".
Putting the brackets won't make any difference. The compiler will accept obj.data as one element even without brackets. It won't point q to simply obj just because you haven't included the whole thing in brackets.
2

The q = &obj.data part, as stated by 'Lame-up-duck' doesn't add up:

Your pointer:

ABC *q;

Is a variable, usually 32 or 64 bit. It can contain the memory address of an object, in this case of type ABC

So you have this object in obj.

To let the pointer actual 'point' to this object, you'll need to assign the memory address value to the pointer with the 'address operator' &, like this:

q = &obj;

q now points to the obj object. You can access this obj object with

q->

Now q->getdata() will call the getdata function in obj.

2 Comments

"q now points to the obj.data object." Are you sure about that? :-)
@user1158692: thanks :), I was confused due to the 'bad book'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.