Can a static variable be access by nonstatic functions? [duplicate] - c++

This question already has answers here:
Can Non-static function modify a static variable in c++
(3 answers)
Closed 4 years ago.
My questions is: if a static variable (access level: private) is defined in a class and we want to access it by non static function of that class. . is it possible in c++?

The answer is yes.
This question was already somewhat asked here: Can non-static methods modify static variables
You are just asking if it can access it though - I am fairly certain if you can modify something, that means you can also access it.

Related

What is the substitute of this specific "if condition with a this pointer"? [duplicate]

This question already has answers here:
When should I make explicit use of the `this` pointer?
(12 answers)
Closed 2 years ago.
if(strcmp(un,this->username)==0 && strcmp(pw,this->password)==0)
Could anyone of you please instruct me the significance of "this" function? Also, what could be a possible alternative of it?
It's only necessary if you need to distinguish the member variable username from one that's local to the function that contains your if statement.
The same applies to password.
If neither apply then you can write
if (!strcmp(un, username) && !strcmp(pw, password))
By the way, using a contiguous character array to store a password is not particularly secure. You wouldn't see code like this in a professional context.

Can I use the static keyword for classes and structs in C++? [duplicate]

This question already has answers here:
Superiority of unnamed namespace over static?
(2 answers)
Closed 2 years ago.
I understand that you can use the static keyword for functions and variables to prevent them from being visible from other files. Can I use it for classes/structs? For example, inside a function, I use a struct that only that function uses, but I don't want to be able to use it from outside that file and I don't return that struct.
Can I use the static keyword for classes and structs in C++?
No. However, you can define classes in an anonymous namespace which will prevent it from being named in other translation units.

C++ "::" without class name [duplicate]

This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 5 years ago.
I came across the following code structure in C++:
uint32_t AClass::Action(....)
{
..
status = ::Action(...);
..
}
I am not sure what ::Action() means. Which class does it belongs to? NOTE: the argument list of ::Action(...) is different from AClass::Action(...).
The leading :: just means that Action here refers to a non-member function in the global namespace, instead of referring to AClass::Action in the current namespace.
::Action() mean it is a function under the the global namespace.

Why do constructors need to be named exactly after the class? [duplicate]

This question already has answers here:
Why constructors will always have same name as of class and how they are invoked implicitly?
(7 answers)
Closed 8 years ago.
What is the reason a constructor needs to have the exact same name than the class? Is it purely a sintactic reason? Is it possible to define the constructor with a different name?
It needs to have the same name as the class to tell it apart from other (non-special) member functions. This is a convention for the constructor (and destructor, when prefixed with a tilde) to unambiguously tell the compiler that you are creating a constructor for your class and not just a member function in the class.

When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program? [duplicate]

This question already has answers here:
When should you use a class vs a struct in C++? [duplicate]
(27 answers)
C++ - struct vs. class [duplicate]
(5 answers)
Closed 9 years ago.
When do we need to use a structure in a pure C++ program? Is structure at all required in a pure C++ program?
I understand that structure is the only way in a C program to encapsulate members and function pointers. But, if I use C++ to code my complete module, is it 100% alright if I stay away from structures and use a Class instead? Am I missing something?
(I googled but my question is very specific - is structure useless in pure C++ code)
=== EDITED ===
My question is not about the difference or commonalities between a structure and a class, but, do we need "struct" at all in C++, when you can 100% do away with a class. Is structure not redundant construct in C++, except for backward compatibility with C? I understand the difference between them w.r.t. the access specifiers but there are only syntactic differences.
The struct has been kept in C++ for backward compatibility with C. By default the struct members are public but the class members are private. You can replace class and struct with each other and get the same result if the type of members are pre-defined.