C++ equivalent of "super"? [duplicate] - 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.

Related

How to get a list of a class's member variables in C++? [duplicate]

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++.

Why do we need a `class` in C++, when a `struct` can be used to achieve the same? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Using a struct we can achieve all the functionality of a class: constructors (that can be modified/overloaded), destructors (that can be modified/overloaded), operator overloading, instance methods, static methods, public/private/protected fields/methods.
Why do we need class then?
Note: I don't want the answer saying that in struct, fields/methods are public by default.
You don't need classes, the language just gives you another option to choose from. Technically, you're right, you can achieve anything a class can do with a struct.
Besides the default access level, there's also the meaning most programmers associate with the two - struct generally means a light-weight, typically POD, data-type with little to no functionality. A class is usually associated with something bigger.
As Tal Pressman answered at When should you use a class vs a struct in C++?:
From the C++ FAQ:
The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.
struct and class are otherwise functionally equivalent.
OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.
http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8
I think one addition to this reason could be that C already had structs. When Bjarne Stroustrup designed C++, he wanted to add classes with all the functionalities you listed in your original question, features which were not present in C. When implementing those features, he probably realised it didn't make sense to make two separate implementations for struct and class (except the public/private default visibility).
TL/DR: in C++ structs/classes describe the intent of the programmer to have POD or more complex abstractions, and the introduction of the class keyword is probably here for historical reasons (add an additional keyword in order to create featurefull classes, then backport those features into the struct keyword because it's more pragmatic to implement).
class is simply the commonly accepted name in OO for a type used for instantiating objects. When introducing the OO paradigm in C++, it was deemed less surprising to use class instead of struct.
struct was kept to maximize backwards compatibility with C.
Today's usage of the two is in line with this : struct is most commonly used for C-style POD types, while class is used for the OO concept of classes.
To make a long story short, class really wasn't needed at all. It changes the defaults to ones that are arguably safer and more applicable to OO programming, but you could use a struct (as defined by C++) for any place that you currently use a class (if you wanted to get cute and meta about it, you could call struct a base class of class that satisfies the LSP).
At the same time, misunderstanding of struct in C++ is rampant, and class fits the intended concept enough better that it's often much easier to explain. New users often seem to find it at least marginally more understandable -- a reasonable goal in itself.
There are no such difference between C++ struct and C++ class, you can perform almost all the functions with struct as you can do with class, but struct is a C keyword which gradually got modified/evolved in C++ and named as class. A we are in C++, it is better to use class rather than struct.
Take an example, if you have done some coding in C++ and some person who works in Java came after 2 months to review your code, which one will he find comfortable to understand a code with "struct" or a code with "class"?

Defining functions in Structs [duplicate]

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++.

What is the most basic class in C++

I hope this question is not too silly, but what is the most basic class in standard C++?
object? Object?
class MyObject : public object{ ...
and I get "Expected class-name before token{"
Is there any map, diagram or image that shows standard c++ classes inheritance?
Something like this but for C++ ?
There is no most basic class in C++ i.e. there is no common base class for all the classes.
There is no fundamental object type in C++, unlike in e.g. Java.
In Cocoa, the NSObject class is fundamental to the framework but not to the Objective-C language itself. In Objective-C, it is possible to create a root class by not deriving from anything (but in order to make it work you'll probably have to hack your way through runtime calls).
Similarly, some C++-based frameworks may define a root class that all other classes in that framework derive from, but it is specific to the framework, not the language.
This is the simplest most basic class you can compile:
class Null
{
};
Inheritance diagram for IOstream library is here. STL is a template library and doesn't use OOP.

Structs vs classes in C++ [duplicate]

This question already has answers here:
Closed 14 years ago.
When should someone use structs instead of classes or vice versa in C++? I find myself using structs when a full-blown class managing some information seems like overkill but want to indicate the information being contained are all related. I was wondering what are some good guidelines to be able to tell when one is more appropriate than the other?
Edit:
Found these links while reading the material Stack Overflow indicated was related after the question was submitted:
When should you use a class vs a struct in C++?
What are the differences between struct and class in C++?
Technically, the only difference between the two is that structs are public: by default and classes are private:
Other than that, there is no technical difference.
struct vs class then becomes a purely expressive nuance of the language.
Usually, you avoid putting complicated methods in a struct, and most of the time structs data members will stay public. In a class you want to enforce strong encapsulation.
struct = data is public, with very simple helper methods
class = strongly encapsulated, data is modified / accessed only through methods
I use structs for simple containers of types that provide no constructors or operators.
Classes for everything else.
Use a struct when you simply need a "bucket of stuff" that doesn't have logical invariants that you need to keep. Use a class for anything else.
See also what the C++ FAQ says on the subject.
Use a class if you have methods, a struct if not.
A class should hide all its internals only exposing methods or properties. A struct tends to expose all its internals and has no accessor methods.
Where only one bit of code is accessing some (related) data, a struct may be perfectly reasonable. Where multiple bits of code need to modify the data or if it's anything slightly complicated, a class would be a better bet.
The difference between Classes and Structs are that structs are groups of variables and classes represent objects. Objects have attributes AND methods and be part of a hierarchy.
If you're using C++ to take advantage of the OO capabilities it's best to use classes / objects which are more natural.
I always use class, even for just containers, for consistency. Its purely a choice of style since the difference between the two is negligible.
If you need to control access to the data, you should use classes. If you don't care who is accessing what, and what they're storing in there, then a struct is probably more appropriate.
Also, a class is more appropriate if you need to do any checks on the integrity of the data itself.
See existing questions:
What are the differences between struct and class in C++
When should you use a class vs a struct in C++?
Personally, I use structs when all I need is a container for data (no member functions).
Otherwise, I use classes.
The only time I make an exception to that rule is if I need a simple functor: e.g.
struct compare { bool operator() { ... } };
sort(v.begin(), v.end(), compare());
The need for a public: label would just clutter up the code unnecessarity.
structs in C++ are classes with a default access method of public, so technically other than that default there is no difference and you can use both equivalently.
Yet there are some expectations and natural tendencies, in part because structs in C++ come from C.
My approach: If it has any private data, a constructor/destructor, or any complex member functions (which do more than just conversion upon set/get, etc.), use class.