What I don't understand is this, "->," symbol. They say it makes stuff point to classes but why don't you just do this? (\/ \/)
Code:int * mypointer = &myclass;
What I don't understand is this, "->," symbol. They say it makes stuff point to classes but why don't you just do this? (\/ \/)
Code:int * mypointer = &myclass;
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
Presumably myclass is an object of a class, and so you want a pointer to that class type (or a base type), and not a pointer to an int.
The arrow pointer is equivalent to dereferencing an object pointer (getting the object), and then accessing it with the '.' operator. It is just shorthand. For example:
Code:struct foo { int bar; }; int main( ) { foo* obj = new foo; foo->bar = 5; // This is equivalent to... (*foo).bar = 5; return 0; }
The arrow operator as it's called allows you to access the member of an object through a pointer. As you know, to access a member of a structure or class you do this:
Now, when you have a pointer to a structure or class, as is more common than you might imagine, you would need to first dereference the pointer and then use the dot operator to access the member:Code:struct da { int member; }; struct da s; s.member = 10; /* Access with <instance>.<member> */
The arrow operator is a syntactic convenience that allows you to make that clumsy dereference/member-access into something simple and elegant:Code:struct da s; struct da *p = &s; (*p).member = 10; /* Very awkward */
This is all the arrow operator does.Code:p->member = 10;
The -> is also used in polymorphism.
say you have base class Point and derived class Circle, and they both have their own print functions as virtual void print();
Code:Point point(); Point *pointPtr = 0; Circle circle(); Circle *circlePtr = 0; point.print() // static binding, uses base class point print function circle.print() // static binding, uses derived class circle print function pointPtr = &circle; pointPtr ->print(); //dynamic binding..base class pointPtr invokes derived class circle's print function.
big146
If you follow that line of thinking then the dot operator is used in polymorphism as well:
However, despite that, the fact remains that even with polymorphism, the arrow operator is still dereferencing an object and accessing a member. So you are not referring to an overload of the operator as your wording implies.Code:#include <iostream> struct a { virtual void print() { std::cout<<"a"<<std::endl; } }; struct b: public a { void print() { std::cout<<"b"<<std::endl; } }; int main() { a ao; b bo; a& ra = ao; a& rb = bo; ra.print(); rb.print(); }
Declaring a member function virtual enables the program to determine which function to invoke based on the type of object to which the handle points, rather than on the type of the handle.
ra.print() // static binding(compile time)
ra->print() //dynamic biding( runtime) polymorphism..decides at runtime which object function print to call.
Last edited by big146; 07-28-2004 at 06:37 AM.
big146
The dot operator does not necessarily imply static binding. Here is an example program to demonstrate what you are talking about with polymorphism (which really is independent of the operators mentioned):
Code:#include <iostream> struct A { virtual void f() { std::cout << "A\n"; } }; struct B : public A { void f() { std::cout << "B\n"; } }; int main() { A* obj = new B; obj->f(); (*obj).f(); B obj2; A& ref = obj2; ref.f(); return 0; }
Could anyone give me an explanation of what the "->" operator with commands I actually learned? I have no clue what the print() function does (I know what printf() does) nor what a foo variable is nor what a virtual void return type is (all I know is I hate that pesky four letter word) and you don't need to explain to me what the "." operator does because that's simple.
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
Basically, the "." is used to access member variables and methods of a non-pointer, and the "->" is the same thing, but is used with pointers instead. So if you understand the ".", and you understand what a pointer is, then you understand the "->".
Look again at Zach L.'s example in the second post of the thread. If you are still having trouble, maybe you can give some simple code that uses the ".", and we can change it so that it uses the "->".
So is this using correct syntax? :
file1.cpp
PHP Code:
//This is C++ code, not PHP. I like PHP code tags since it makes me a little more organised.
#include<iostream.h> //////////////I don't use iostream, I'm a fan of iostream.h :)
#include<string.h>
#include<conio.h>
using std::string;
struct mystruct
{
int * pointer;
};
int main()
{
string hi = "Hi!";
mystruct->pointer = &hi; ////////Why didn't Bjorne Stroustrap make the "." operator for pointers too?
cout << *hi;
cout.flush()
getch();
return 0;
}
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
no your going to want to do something like this
btw: <iostream.h> is old <iostream> is new and they both do the same thing so if you like one, you like the otherCode:#include <iostream> using std::cout; class loser { public: int loserPower; } int main() { loser brian; loser *pBrian; pBrian=&brian; pBrian->loserPower=10; cout<<brian.loserPower; cout<<pBrian->loserPower; }
Last edited by prog-bman; 07-28-2004 at 07:31 PM.
Woop?
I tried to use your setup to show the simmilarities and differences between "." and "->".PHP Code:
#include<iostream.h>
#include<conio.h>
struct mystruct
{
int value;
};
int main()
{
// Create an instance of mystruct - name the variable "instanceOfMyStruct"
mystruct instanceOfMyStruct;
// Use "." to access the member variable like you already understand (I hope).
instanceOfMyStruct.value = 5;
// Create a pointer that can point to any instance of mystruct.
mystruct* pointerToMyStruct;
// Set the pointer to point to the instanceOfMyStruct.
pointerToMyStruct = &instanceOfMyStruct;
// Use "->" like we used "." above, to access the member variable of an
// instance of mystruct.
pointerToMyStruct->value = 10;
// Output the value using the instance or the pointer.
// Both should output the same number (10) because the
// pointer simply points to the instance.
cout << "Instance value using ".": ";
cout << instanceOfMyStruct.value << endl;
cout << "Pointer value using "->": ";
cout << pointerToMyStruct->value << endl;
getch();
return 0;
}
nope, not true. In <iostream> you have to put the namespace std before mostly everthing that require a namespace. In iostream.h, u don't.Originally Posted by prog-bman
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)
Really i didn't know that thanks for telling me
They both accomplish the same thing <iostream> is now the standard so if you want to use old headers thats your call but how hard is it to put using namespace std; at the top of your c++ files or using std::whatever
Woop?
And tell me why u would use the -> operator there? In class loser u set loserPower as an integer. U said u only use the -> operator when the > side of -> is a pointer! Ur confusing me.Originally Posted by prog-bman
Child who knows C++Using Borland C/C++ Compiler 5.5 (Command Line Version)