Thread: Pointers and Classes

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    12

    Pointers and Classes

    Hello all,

    I was reading a tutorial on C++ programming, and in one example a class, let's call it, MyExample is defined. In the main function the following statements were written:

    Code:
    MyExample *p=new MyExample[2];
    .
    .
    . 
    p->SomeFunc(3,4);// .... (1)
    p[1].SomeFunc(1,7);// ... (2)
    where SomeFuc(int a, int b) is a public member function of the class that takes two int arguments.

    To my understanding, the pointer p points to the first object of two objects of type MyExample that occupy consecutive memory locations. On the other hand, P[0] and p[1] are the first and second objects, respectively. I guess that's why we used -> with p in (1) because it's a pointer, while we used . with p[1] in (2) because it's an object, right?


    It also seems to me that the statement (1) passes the int arguments 3 and 4 to both objects p[0] and [p1]. Is this how it works?

    Thanks
    Last edited by CNewProg; 09-14-2016 at 08:10 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CNewProg
    To my understanding, the pointer p points to the first object of two objects of type MyExample that occupy consecutive memory locations. On the other hand, P[0] and p[1] are the first and second objects, respectively. I guess that's why we used -> with p in (1) because it's a pointer, while we used . with p[1] in (2) because it's an object, right?
    Yes.

    Quote Originally Posted by CNewProg
    It also seems to me that the statement (1) passes the int arguments 3 and 4 to both objects p[0] and [p1]. Is this how it works?
    No. For a pointer p, p[0] is equivalent to *(p + 0) which is equivalent to *p. (*p).foo is equivalent to p->foo. Hence, p->foo is equivalent to p[0].foo. I recommend the p[0].foo syntax here since p effectively represents a dynamic array, and what you are trying to do is to invoke the member function on p[0]. If p pointed to a lone object, or if it were used to traverse a sequence while pointing to individual objects in the sequence, then p->foo would be fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    12
    Quote Originally Posted by laserlight View Post
    ...

    No. For a pointer p, p[0] is equivalent to *(p + 0) which is equivalent to *p. (*p).foo is equivalent to p->foo. Hence, p->foo is equivalent to p[0].foo. I recommend the p[0].foo syntax here since p effectively represents a dynamic array, and what you are trying to do is to invoke the member function on p[0]. If p pointed to a lone object, or if it were used to traverse a sequence while pointing to individual objects in the sequence, then p->foo would be fine.
    That makes sense. It's clear now. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes,pointers,etc
    By lilhawk2892 in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2005, 12:07 PM
  2. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  3. Pointers to classes
    By sirSolarius in forum C++ Programming
    Replies: 9
    Last Post: 11-25-2003, 07:25 PM
  4. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM
  5. Pointers to classes
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2002, 09:25 PM

Tags for this Thread