This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
nested classes C++
nested class access control in C++
I'm a little confused as to what access a nested class has to the members of an enclosing class. What is the relationship it shares with the outer class?
The nested class does not have any special access to the enclosing class that would not be available to any other class - it can see public members, etc. The nesting only provides a useful scoping mechanism for nested class, but doesn't change its behavior or capabilities.
This might help you : nested class access control in C++. It is not exacly what you asked, but gives some interesting informations.
Related
This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
What's the difference between friendship and inheritance?
(4 answers)
Closed 5 years ago.
I am not exactly sure what the use of a friend function or class is when you can easily just use inheritance (parent/child classes)? From my understanding, a friend function or class allows a non-member function to access the members of another class that it is declared a friend of. Similarly, a child class can access the members of a parent class. I am wondering, in which situation would you pick one over the other?
Because you may want to use some private functions/member of a class without being a child of it....
You should read more about inheritance. You can't consider inheritance as a solution to access "members of a class" as you mentioned.
My knowledge of C++ is limited as I've only started using it about 8 months ago. Recently I've received a review comment on my code that states:
"The header doesn't define all class methods before members in the public section"
Sounds simple, or so I thought. Until I realized I don't quite know the theory behind this. What is class method vs members?
Am I right in understanding that int a is a class member, while double doubleToInt(int a) is a class method?
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 8 years ago.
I know that is possible for a struct:
struct A{
A* p;
};
But could we do that to a class:
class B{
B*p;
}
I tried to google it, but end not found it. Thank you very much!
Yes. There is no difference between a class declared with struct and one declared with class other than the default access for members and bases.
Yes, a class can contain pointers to a class of the same type, and the symbol representing the class can appear in signatures of functions or in the bodies of functions defined in that class. Furthermore, it should be noted that, in C++, struct and class are identical with only one difference -- the default access level for a struct is "public", while the default access level for a class is "private" (of course, one can trivially write "public:" or "private:" at the beginning of either in order to change the access level; it is only convention that results in "class" being used for types that include more functionality, encapsulation and "struct" for pure data objects).
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 9 years ago.
I have known that structs in C doesn't support functions / constructors like a class in C++ . However I did try putting in function definitions and constructors and the code behaved as if I have defined a class and not struct . I used visual studio 2010 for my code . Is this a standard feature or just that it works only with MS C++.
I searched many forums and they had mixed responses .
In C++ Structs and Classes are the same except for one thing. A class' members and methods are private be default, a Struct's are public by default.
struct and class are functionally the same in C++ except members in a struct are public by default and in a class are private by default. in fact this previous thread covers it in itty bitty details.
If you compiled successfully than you must have been using C++.
This is a standard feature of C++ but not of C. You must have been compiling in C++.
This question already has answers here:
Using "super" in C++
(19 answers)
Closed 9 years ago.
In Java, instead of using the scope operator, super is used ex:
C++ -> GenericBase::SomeVirtualFunction();
Java -> super.someVirtualMethod();
Is there something like this in C++ or does this not make sense in C++ because of multiple inheritance?
Thanks
There is a convention of defining a super typedef in every class.
There's no such thing in C++, although you can provide your own typedef :
struct Derived : Base
{
typedef Base super;
};
Microsofts compilers have (rejected by C++ standard commitee) extension __super.
Edit: Super may confuse readers of code. Because of multiple inheritance in C++ it is better to be more explicit. Multiple inheritance is already complex. There was AFAIK discussion about usefulness of super for templates that calmed down after it was realized that anyone can typedef super if they need it.
The typedef trick in Martin's link works quite well (and that's partial reason that's why C++ doesn't have super or inherited keywork AFAIR.)
The only thing we need to care about is that the typedef should be in private section. Don't put it in protected or public section, otherwise, an derived class may wrong use the typedef to refer to its grandparent rather than its parent.