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

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.

Related

Changing/punning types [duplicate]

This question already has answers here:
C++ cast syntax styles
(10 answers)
What's the difference between type(myVar) and (type)myVar?
(2 answers)
What is the difference between (type)value and type(value)?
(5 answers)
Closed 3 years ago.
Consider:
b = (int) a; // C-like cast notation
b = int (a); // Functional notation
Apparently I was wrong in my initial cut at an answer. They are roughly equivalent. And while compound type names like long long or void * can't use functional syntax directly (i.e. long long(val) doesn't work), using typedef can get around this issue.
Both cast notations are very bad and should be avoided. For example:
const char c = 'a';
void *fred = (void *)(&c);
works, and it shouldn't.
Both the C-style cast notation will sometimes behave like static_cast, sometimes like const_cast, sometimes like reinterpret_cast, or even a combination of the two depending on the exact situation in which it's used. These semantics are rather complex and it's not always easy to tell exactly what's happening in any given situation.
I have gone to using mostly C++ static_cast<type>(val) style casts, and never use C-style casts. Based on my research for this question I'm going to also stop using function-style casts for anything. The question "C++ cast syntax styles" has an excellent answer (the accepted one) that details why.
There's hardly any difference. Officially, the first tells the compiler that the value is an integer. This probably doesn't generate any extra code at all. The function call is an actual call that internally performs the other typecast. A smart compiler will optimize this, so they are actually the same.
There isn't any difference. It is a matter of preference. These are old-style casts.
It depends where you use it and how. I.e. if you have values or pointers (or pointers of pointers).
With C++ you should read up on *_cast<> and use them instead.

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.

Confusion regarding char[] [duplicate]

This question already has an answer here:
Should statically-declared character arrays with a specified size be initialized with a literal in C?
(1 answer)
Closed 8 years ago.
I have recently started programming in C++ and that's why I am facing lots of confusions.
I wanted to know what's wrong in using the following piece of code:
char interface[20];
interface="USB01";
The C++ language does not allow you to assign to arrays.
You can, however, initialize arrays. The syntax is similar, but the assignment operator is used in the same statement as the declaration:
char interface[20] = "USB01";
However, in C++, one would typically use a standard container like std::string rather than C strings. These are far easier to use and do allow for natural assignments.
std::string interface;
....
interface = "USB01";
Note that we don't need to decide up front how much space to reserve for the string. This is just one of the many benefits of using the standard string class.
You can't assign arrays like that, but you can initialise them:
char interface[20] = "USB01";
In C++ though you should be using proper C++ strings, i.e. std::string, not C-style char * strings:
std::string interface;
interface = "USB01";

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.

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

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