Why does C need the scope resolution operator

0 votes

Why is the:: operator used in C++ instead of the. operator for this purpose? 

There is no distinct operator in Java, and it works great. 

Is there a distinction between C++ and Java that necessitates the use of a separate operator in order for C++ to be parsable?

My only idea is that:: is required for precedence reasons, but I'm not sure why it should take precedence over, say,... 

The only way I can see it happening is if something like

a.b::c;

would be parsed as

a.(b::c);

, but I can't think of a situation where this syntax would be lawful in any case.

Maybe "they do different things, so they might as well look different" is the case. 

That, however, does not explain why : : has higher priority than.

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
868 views

1 answer to this question.

0 votes
No. There is no scope resolution operator in C.

One can be found in C++ (::).

Perhaps you (or your book) are mixing up C and C++.

You inquired how to get to the global variable a from a function (here main) having its own local variable a.

This isn't possible in C.

It's lexically incorrect.

Of course, you could take the variable's address from somewhere else and use it as a pointer, but that's a different story.

Simply rename the variable to something like 'don't do that.'
answered Jun 1, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
757 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
400 views
0 votes
0 answers

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
351 views
0 votes
1 answer

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,929 views
0 votes
0 answers

What does '&' do in a C++ declaration?

As a C person, I'm attempting to comprehend some C++ code.  The declaration of my function is as follows: int foo(const string &myname) { cout ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
396 views
0 votes
1 answer
0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
852 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
557 views
0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
952 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP