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++.
Related
This question already has answers here:
What are access specifiers? Should I inherit with private, protected or public?
(2 answers)
Closed 3 years ago.
I am trying to understand how exactly c++ class data member access has been restricted/controlled using access specifiers
The compiler reads the file and stores whether each member was declared public, private or protected. That's it.
There is nothing else. The linker doesn't care. It doesn't impact the executable generated. It doesn't prevent people hacking around it.
Side-story: I've seen code of a major corporation (with influence on the C++ committee) containing this pearl:
#define private public
Just before an #include. And guess what? It made the private members accessible as public. So, really, there's no mechanism past just remembering what's written on the source file and complaining if you try to bypass it.
This question already has answers here:
How can I add reflection to a C++ application?
(28 answers)
Closed 7 years ago.
Is there a way that I could obtain a list or maybe iterate through a list of each member variable of a class -regardless of it's type.
My intention is to add them all to a template std::map.
class Person
{
int Age;
char *Name;
template<typename Container> std::map<char *, Container> List;
void MakeList()
{
for(auto i : Person.memberVariables)
{
List.emplace(i);
}
}
};
Could I do something like the above to add each variable of my class to the list, regardless of how incorrect the other code is. The adding to the list is what I'm interested in.
Thanks in advance.
No, you can't do it in C++. this is called reflection which is currently not supported in C++.
The ISO C++ comitee talks about compile time reflection that is proposed for C++17 that will let you to iterate over class members on compile time. but it will take much time until this feature will be standarized and be implemented on the common compilers
as a side note, I don't see why one would want to iterate over member variables other then maybe implementing serialization method , which in this case I would require the class to implement serialize method and enforce it with SFINAE.
If there is, I would be very surprised.
This is called reflection, and as far as I know this isn't supported by C++.
This question already has answers here:
How do you declare an interface in C++?
(17 answers)
Closed 8 years ago.
I wanted to write shell extensions for windows in plain C++, but then I got confused by the keyword interface. In many articles I read that I can create interfaces in C++ by writing classes containing only virtual methods without any code. For example:
class IIsThisAnInterface_QuestionMark {
virtual MyMethod (
int firstParameter,
double secondParameter) = 0;
virtual AnotherMethod (
wchar_t *firstParameter) = 0;
}
But the author of this article defined interfaces by using the interface keyword. So my question is: How to correctly define interfaces in C++? (Becuase I grew up in C#'s world, I know interfaces as constructs specifying methods for classes that are implementing these interfaces.)
C++ doesn't strictly provide interfaces in the way that some languages do. The C++ mechanism is to provide a class with one or more pure virtual methods that declare the desired interface. Strictly speaking such a class is just an abstract class, but one could consider calling it an interface that child classes would then implement.
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.
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.