Thread: Pointers to classes

  1. #1
    Unregistered
    Guest

    Pointers to classes

    When should I use pointers to classes? I mean Instead of Class1.Member, Class1->Member. And why should I use that instead of . sometimes? I recently read a tutorial about pointers so I know several things.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    When you want polymorphism, you need to use pointers and the "arrow operator".
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Unregistered
    Guest
    Can you explain some more please?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Whenever you have big projects you will need pointers to classes. Instead of passing an entire class by value to a function it is much easier to simply pass a pointer (usually 4 bytes) instead of the whole class which is much bigger in size. Did this help you?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Use "." when you have created an instance of an object and use "->" when you have a pointer....ala;

    Code:
    #include <iostream>
    using namespace std;
    
    class MYCLASS{
    public:
          void f(void){cout << "Hello World" << endl;}
    };
    
    int main(int argc, char *argv[])
    {
      MYCLASS myClass, //instance
              *ptrmyClass;//pointer
    
      ptrmyClass = &myClass;
    
      myClass.f();//call from instance
    
      ptrmyClass->f();//call from pointer
    
    
      return 0;
    }

  6. #6
    Unregistered
    Guest
    Ok, thank you, i got it. So, when I have really big classes, i should pass values to the variables or call classes using ->, right?

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Well, it also goes for smaller classes. There's still a difference between passing 4 bytes and 20 (especially when you do it often). Also, when you pass a pointer to a function instead of the whole contents of an instance, you can modify the class within that function.

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you don't fully understand pointers, then don't bother using them. You'll just be wasting a lot of precious time.

    Uses your classes normally (don't declare pointers),
    and if you want to pass it to functions, then pass the address of the instance to the function.

    eg.

    type function(Person*);

    Person peter; //Person is a defined class

    blah
    blah
    blah
    blah
    ...

    function(&peter); //pass the address of the instance

    ...

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by The Dog
    If you don't fully understand pointers, then don't bother using them. You'll just be wasting a lot of precious time.
    I disagree...if you dont understand pointers keep working until you do....

    You will not get anywhere in C or C++ without pointers......they are the mechanism that makes these languages what they are and many important principals rely on them

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originall posted by Fordy
    You will not get anywhere in C or C++ without pointers......
    I fully agree with this statement, but what I'm getting at, is that pointers should be taught to you. You shouldn't have sit hours on end trying to teach yourself.

    Teaching yourself is good practice, I agree.
    But you'll progress much more if you were tought properly.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    ..when I have really big classes...

    that depends.. how many people are in your class??

    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  12. #12
    about 12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ confusion
    By Eirik in forum Windows Programming
    Replies: 14
    Last Post: 04-29-2009, 01:54 PM
  2. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  3. Class with pointers to other classes as member data.
    By Sclorch in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2009, 05:59 AM
  4. Pointers, Classes, and Errors o my!
    By Scottc1988 in forum C++ Programming
    Replies: 12
    Last Post: 03-13-2003, 10:14 PM
  5. question time (classes and pointers)
    By swarm in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 11:33 AM