What exactly does the .(dot) operator do in Objective-C [duplicate] - c++

This question already has answers here:
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 9 years ago.
I taught myself C and C++ and am trying to learn Objective-C now, but I am a little confused by the dot operator used on instances of classes. Say I declared a class:
MyClass* myinstance = [[MyClass alloc] init];
//Then I call the member function foo:
myinstance.foo;
Obviously this works in Objective-C, but in C++ or C (in the case of a struct), this wouldn't. You would have to use the operator ->. So I'm looking for an explanation of what exactly the .(dot) operator does in Objective-C and how the two different meanings from ObjC and C don't cause compatibility issues between C and Objective-C even though Objective-C is a strict superset of C.

Dot notation is to call the method as we do it by using space. we can call methods by using space with all variables but dot notation is used with property and synthesizes variables only
example
variable.method_name; //is a dot notation with property and synthesized variable
[variable method_name]; // is space notation
and the dot notation is for all default properties because those are already property and synthesized like
label.text = #"Ashok";
and as said space can be used by synthesizing separately using set method as
[label setText:#"Ashok"];
ThanQ

Related

What is the meaning of multiple class methods separated by "::"? [duplicate]

This question already has answers here:
What does the "::" mean in C++?
(4 answers)
Closed last year.
I found this line of code where I'm working, but I don't quite understand its meaning:
virtual method1::method2::method3 f() = 0;
In the main function, I have method1::method2::method3.g1().g2(). I really don't understand. I know method::A where A is a class.
:: is a scope operator.
You may need to append multiple of them either when using nested classes (a class defined within another class) or when you use namespaces. Resolution is done the same way.
:: is the scope resolution operator. It allows you to statically traverse scopes such as namespaces and classes in order to reference the identifier you want.
I think what you have is not methods but namespaces and classes.

Since C++ knows type, can it infer dot and arrow? [duplicate]

This question already has answers here:
Why does the arrow (->) operator in C exist?
(3 answers)
Closed 3 years ago.
Say I have this code
std::string s = "hello";
std::string* p = &s;
int l = p.length();
Visual Studio and CLion know dot operator is incorrect. Actually when I press key ., both IDE commit the keystroke as ->.
Since C++ is typed, it knows p is a pointer, why doesn't it combine . and -> into one?
Can the syntax be to always use ., if p is pointer, dereference it first, otherwise directly get its member?
Classes can overload the arrow operator, so there can be objects for which both . and -> are valid. Smart pointers (e.g. std::unique_ptr or std::shared_ptr) and iterators are common examples.
std::unique_ptr<Foo> f;
f.reset(new Foo(42));
f->bar();
Could you design a statically typed language without this distinction? Of course. But C++ has evolved this way, and that's not something you can change today without breaking tons of code.

is overloading parantheses c++ a good idea [duplicate]

This question already has answers here:
What are C++ functors and their uses?
(14 answers)
Closed 4 years ago.
I have programmed in c++ for a couple of months, and I am starting to understand the core. At the moment I am trying to make a calender program with classes and inheritance (just to get more comfortable with object oriented programming), and a few weeks ago I learned about operation overloading.
I am wondering, is it a bad idea to overload parantheses for an object, such that I could for instance write this, or can an error occure becuase the compiler can confuse it for something else(constructor or something like that)?
//creating a valid year-object
Year year1998 = Year(1998,true);
// the parantheses operator returns a day(another object)
Day d = year1998(1,10);
//the overloading
Day& Year::operator()(int monthNumber, int dayNumber){
//Just returns a day from the month class
return months[monthNumber][dayNumber];
}
Overloading this operator is a basic concept in C++ and is extensively used in function objects, a typical example being comparison functions.
You can savely use this operator, but nowadays often lambda functions are preferred to function objects.

finding addressof a object whose ampersand operator is overloaded and private [duplicate]

This question already has answers here:
If an operator is overloaded for a C++ class how could I use a default operator instead?
(3 answers)
Closed 8 years ago.
I have a class which is overloading ampersand(&) operator and made it private. I don't have a latest C++11 compliant compiler so is there any way by which I can get address of my object using current C++ compiler only
Thanks
reinterpret_cast<T *>(&reinterpret_cast<char&>(obj))
Dunno if it's safe though.. (well clearly it's a bit dodgy)
Use the addressof() function from Boost.Utility. If you don't want to use Boost, you can still look at it's implementation which consists of just a single header.

C++ Classes - dot notation vs pointer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the difference between the dot (.) operator and -> in C++?
What's the difference between using dot notation and the pointer way?
Instantiating an object with or without a pointer.
Instantiate w/o a pointer = then use dot notation
Instantiate w/ a pointer = then use ->
What are the differences between both? When and why should one be used over the other?
If I understand your question: in C++, a->b is just shorthand for (*a).b -- they're exactly the same (Edit: unless you've overloaded them to behave differently!), it's just that the first is easier to type. :)
If you're referring to using string a; versus string* a = new string(), that's a different topic -- look up stack-based and heap-based allocation.