Really Dumb (easy) Question
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
well the . and the -> actually do the same thing they server the same purpose but the difference is that the '.' (dot operator) is used when are dealing with a NORMAL object while the -> operator is used for a POINTER object like check this little snippet below class foo { public : int i; }; when making an object of this class foo myobject; myobject.i = 0; //this is the normal way to access the i variable now look at this object foo *pobject; //now this is a pointer to object pobject->i = 1; //this is way i will be accessed
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
ok :) small words? you got em :) // a header file class A { public: A(); ~A(); void foo(); }; // a source file int main(int, char**) { A a_object; A* a_[ointer_to_the_object = new A(); a_object.foo(); a_pointer_to_the_object->foo(); return 0; } you see? when you deal with a pointer - you use -> operator...... debug - is my life style
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
Roger Wright wrote: see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? To the best of MY understanding :) The member selection operator -> is used whenever the expression before is coupled with an indirection operator *
// No indirection
CMyObject myObj;
myObj.Test();// Using indirection
CMyObject* myObj;
myObj->Test();You can acheive the same thing using
CMyObject myObj;
(*myObj).Test();
Basically when you use -> or *. are accessing members indirectly with a pointer. The word of the day is legs, let's go back to my house and spread the word ;P
-
Roger Wright wrote: see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? To the best of MY understanding :) The member selection operator -> is used whenever the expression before is coupled with an indirection operator *
// No indirection
CMyObject myObj;
myObj.Test();// Using indirection
CMyObject* myObj;
myObj->Test();You can acheive the same thing using
CMyObject myObj;
(*myObj).Test();
Basically when you use -> or *. are accessing members indirectly with a pointer. The word of the day is legs, let's go back to my house and spread the word ;P
Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
-
Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
Well, perhaps I'll add my knowledge to the heap. Consider the following: you have an object of some type. Let's name this type 'Roger', and let it have an attribute called 'Age' (No sarcasm intended !) Now, if in your code, you created a static object, you would use
Roger MyRoger
declaration. This would create a static object for you. You would then access the member variable by usingMyRoger.Age = 30
, for example. Let's take a step beyond and consider a pointer. See in front of yourself a 3D-space. In this space, you see theMyRoger
-object standing at some point. Then, you createa pointer of type Roger
using a declarationRoger* pToMyRoger = &MyRoger
. Now, thepToMyRoger
pointer points towards the MyRoger object, just like an extended finger. A static object is aware of two things:what
it is, andwhere
it resides. A pointer to any object is aware of two things as well:where
it resides, andwhat
is to be found in that location. For a more in-depth scenario, consider that your Roger-object (MyRoger) reserves a place for itself in memory (&MyRoger). If you issuedMyRoger.Age
, the compiler would get the address ofMyRoger
, move forwards from there to theAge
variable, and retrieve it's contents. If you used a pointer, then the compiler would take the address you give to that pointer, and ASSUME that the object found from that address is the one specified by the type of the pointer (Roger*). It would then search for theAge
variable again, and retrieve it. Then to the term 'dereferencing'. When you have pointer to the object Roger (pToMyRoger), you can dereference it to an OBJECT of type Roger by using the asterisk (*) operator. For example, this declaration(*pToMyRoger).Age
is the same thing asMyRoger.Age
. As you might guess,*pToMyRoger
equals toMyRoger
hence leading us to the following conclusion: the dereferencing operator (*) can sometimes be named as the 'contents-of' operator. Thus,the contents of pToMyRoger
(*pToMyRoger) equals tothe object MyRoger
(MyRoger). Does this make sense ? In the conclusion, the '.' and the '->' operators behave similarly, but they are used in different contexts. For example, the following declarations do all the same thingsMyRoger.Age
=(*pToMyRoger).Age
=pToMyRog
-
Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
I just finished writting a bunch of verbage and an error occurd when I sent it :( And I don't think it sent...sux when that happens :) Anyways...you've helped me more often then not during my times here at CP so any time I can help back i'm more then happy. Cheers :) The word of the day is legs, let's go back to my house and spread the word ;P
-
I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
Not knowing the difference between . and -> does not make you a stupid guy :) and Knowing the difference between . and -> does not make us any smarter either :) :-D you are OK :rose: C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg
-
Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
The feelings you are having towards c++ are normal and understandable for a person trying to learn the language. Once you fully grasp the concepts and advantages (as well as disadvantages) of pointers you will view them in a much different light for sure.
-
Well, perhaps I'll add my knowledge to the heap. Consider the following: you have an object of some type. Let's name this type 'Roger', and let it have an attribute called 'Age' (No sarcasm intended !) Now, if in your code, you created a static object, you would use
Roger MyRoger
declaration. This would create a static object for you. You would then access the member variable by usingMyRoger.Age = 30
, for example. Let's take a step beyond and consider a pointer. See in front of yourself a 3D-space. In this space, you see theMyRoger
-object standing at some point. Then, you createa pointer of type Roger
using a declarationRoger* pToMyRoger = &MyRoger
. Now, thepToMyRoger
pointer points towards the MyRoger object, just like an extended finger. A static object is aware of two things:what
it is, andwhere
it resides. A pointer to any object is aware of two things as well:where
it resides, andwhat
is to be found in that location. For a more in-depth scenario, consider that your Roger-object (MyRoger) reserves a place for itself in memory (&MyRoger). If you issuedMyRoger.Age
, the compiler would get the address ofMyRoger
, move forwards from there to theAge
variable, and retrieve it's contents. If you used a pointer, then the compiler would take the address you give to that pointer, and ASSUME that the object found from that address is the one specified by the type of the pointer (Roger*). It would then search for theAge
variable again, and retrieve it. Then to the term 'dereferencing'. When you have pointer to the object Roger (pToMyRoger), you can dereference it to an OBJECT of type Roger by using the asterisk (*) operator. For example, this declaration(*pToMyRoger).Age
is the same thing asMyRoger.Age
. As you might guess,*pToMyRoger
equals toMyRoger
hence leading us to the following conclusion: the dereferencing operator (*) can sometimes be named as the 'contents-of' operator. Thus,the contents of pToMyRoger
(*pToMyRoger) equals tothe object MyRoger
(MyRoger). Does this make sense ? In the conclusion, the '.' and the '->' operators behave similarly, but they are used in different contexts. For example, the following declarations do all the same thingsMyRoger.Age
=(*pToMyRoger).Age
=pToMyRog
Thanks! That helps clarify things quite a bit.:-) "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
-
The feelings you are having towards c++ are normal and understandable for a person trying to learn the language. Once you fully grasp the concepts and advantages (as well as disadvantages) of pointers you will view them in a much different light for sure.
I suppose so, though I haven't yet found any use for a pointer, nor any need to use one other than to comply with the declaration of some class or other.:sigh: "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
-
Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.
Roger Wright wrote: But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. Technically, pointers existed long before C++.
A rich person is not the one who has the most, but the one that needs the least.