I created a class called Kwadrat
. The class has three int
fields. My Development Environment suggests that I access the fields from Kwadrat
created objects via the ::
& ->
operators. I tried both operators, and found that the ->
operator is able to successfully access the data in the objects fields, although, the same cannot be said for the ->
operator. I have also found that the .
operator will access class members as well. I am confused, and don't understand why there are three members for accessing object members &/or methods. Can someone please explain to me what the difference is between the three operators?
->
::
.
#include <iostream> using namespace std; class Kwadrat{ public: int val1, val2, val3; Kwadrat(int val1, int val2, int val3) { this->val1 = val1; // Working this.val2 = val2; // Doesn't Work! this::val3 = val3; // Doesn't Work! } }; int main() { Kwadrat* kwadrat = new Kwadrat(1,2,3); cout<<kwadrat->val1<<endl; cout<<kwadrat->val2<<endl; cout<<kwadrat->val3<<endl; return 0; }
It's to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference.
In C++, scope resolution operator is ::. It is used for following purposes. 1) To access a global variable when there is a local variable with same name: // C++ program to show that we can access a global variable. // using scope resolution operator :: when there is a local.
C standard refers to -> as arrow operator in the Index only. The main text of the document doesn't seem to use any specific name. Show activity on this post. According to Wikipedia's list of operators in C and C++, it's called "member by pointer".
OR ( || ) - If EITHER or BOTH sides of the operator is true, the result will be true. AND ( && ) - If BOTH and ONLY BOTH sides of the operator are true, the result will be true.
1.->
for accessing object member variables and methods via pointer
to object
Foo *foo = new Foo(); foo->member_var = 10; foo->member_func();
2..
for accessing object member variables and methods via object instance
Foo foo; foo.member_var = 10; foo.member_func();
3.::
for accessing static variables and methods of a class/struct
or namespace
. It can also be used to access variables and functions from another scope (actually class, struct, namespace are scopes in that case)
int some_val = Foo::static_var; Foo::static_method(); int max_int = std::numeric_limits<int>::max();
In C++ you can access fields or methods, using different operators, depending on it's type:
Note that :: should be used with a class name rather than a class instance, since static fields or methods are common to all instances of a class.
class AClass{ public: static int static_field; int instance_field; static void static_method(); void method(); };
then you access this way:
AClass instance; AClass *pointer = new AClass(); instance.instance_field; //access instance_field through a reference to AClass instance.method(); pointer->instance_field; //access instance_field through a pointer to AClass pointer->method(); AClass::static_field; AClass::static_method();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With