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.
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.
When you want polymorphism, you need to use pointers and the "arrow operator".
Can you explain some more please?
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?
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; }
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?
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.
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
...
I disagree...if you dont understand pointers keep working until you do....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.
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
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.Originall posted by Fordy
You will not get anywhere in C or C++ without pointers......
Teaching yourself is good practice, I agree.
But you'll progress much more if you were tought properly.
..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.