why c++ doesnt have many features of many modern languages like c# languages?

Started by
22 comments, last by frob 4 years, 6 months ago

im a c# programmer, also i want to learn c++  in advance. as i searched many different book, i couldnt find many aspects of c# in c++ in those books. features like event, delegate, property and... but we have pointers, references and templates in c++

i know c++ has many different distributions and different versions work on different platforms.  but doesnt really c++ have those features? 

can you introduce me a reference(book or website) that helps me understand all advanced features of c++ specally for programming DX and OpenGL?

 

Advertisement

No C++ doesn't has these features because they are something that is bound to the runtime and languge model. It starts with having delegates (because events are in fact just delegates).

In C++ you don't have delegates, just function pointers. The reason is simple, a function pointer points to an address in the assembly binary. The compiler will perform just a jmp instruction as same as if you call the function directly. Those functions can have and for the kind of instruction the compiler uses need different calling conventions. A static function is just a cdecl/ stdcall, this determines how the stack is created and cleaned after the call returnes. The you have member functions that have a different calling convention: thiscall. It is again some special kind of stack creation, member functions are also statically called except that there is an additional parameter on the stack, the pointer to the object you call it for. It isn't possible to interchange calling conventions to store them in the same variable because the compiler has to ensure correct stack management. This is different for C# because in C# the CLR is able to deduce the function calls at runtime, so you can store whatever you like static or member function in a delegate.

However, there are tricks in C++ using templates to create such kind of delegate too. If you are interested in how this works, I provide delegates in my common C++ source at GitHub. It works because you store two pointers in the dynamic delegate instance, an optional object that will be used for thiscall convention and a calling proxy that is a templated function. If you assign a static function, the cdecl version call guard is used, if you use a thiscall member function, the thiscall version call guard is used. This works because those template functions are static functions and just route the arguments passed to the target function you add in there. So in fact the delegate calls the call guard passing the functions arguments and the object pointer allways and then the call guard decides how to exactly execute the function pointer given in the template argument.

Properties is another topic, they are compiler convinience for function calls. If you declare a property in C#, the compiler will add hidden functions to your class that are named get<PropertyName> and/or set<PropertyName>. You can investigate this if you use reflection to inspect the classes functions. So you can have this in C++ too, just add a function to your class the same way the C# compiler does or more simple


inline void PropertyName(PropertyType const& type)
{
    myVariable = type; 
}
inline PropertyType PropertyName() const
{
    return myVariable; 
}

I use this practise in my code base all the time, the only difference is in calling it


//C#
MyType m = myClassInstance.PropertyName;

//C++
MyType m = myClassInstance.PropertyName();

There are of course more topics that aren't implemented in C++ right now, for example reflection. The potentially most usefull thing in C# is tricky to do in C++ because of the CLR C# runs at. The CLR knows at runtime the type and consistency of each class and so each object. This isn't possible in C++ because objects are just memory, they don't have any information about their name, fields and methods. Yes, there is the function table in an object which inherits from another class but this only contains function pointers for derived functions and virtual overrides. There are some patterns how to implement reflection in C++ but keep in mind, reflection means storing meta information for every type in your assembly, so your code will increase in size.

Some solutions generate code for more complex reflection including fields, functions and their arguments, some other are just a lookup of a typeID or type code like in C#. It is up to you here how complex your reflection support should be. I decided for a mixed version in my code base, have a typeID for every type I use in my code, regardless of if it is built-in or mine and have generated more detailed information for some chosen types I declared by my own

Microsoft brainwashed you with C#, now you belong to microsoft.

15 hours ago, moeen k said:

features like event, delegate, property

Yeah C++ doesn't have those in the 'core' language, mainly because they're the kind of functionality that can be provided through libraries.

For example instead of events you might use Boost Signals2.

For delegates the C++ standard library has std::function that you can use.

For properties I would just say it's something that is typically achieved by writing member functions.

 

15 hours ago, moeen k said:

can you introduce me a reference(book or website) that helps me understand all advanced features of c++

https://en.cppreference.com and http://www.cplusplus.com/reference are pretty good for learning about what's in the core language and what's in the standard library.

While https://www.boost.org/doc/libs/ is a large set of high-quality community libraries and one of the most influential projects in the C++ world. Several libraries from Boost have since been standardised into the C++ standard library (e.g. boost::shared_ptr became std::shared_ptr).

As @dmatter pointed out, some features aren't part of the language itself, but rather of the STL (or Boost). Since in other languages (C#, Java, Python, ...), the default library is such an integral part of the language, some language features can completely depend on the existance of this library. An example would be


MyStruct? value;

wich is equivalent to


System.Nullable<MyStruct> value;

and thus requires System.Nullable to be present.

The short-hand events (meaning you don't implement add or remove explicitly) rely if I'm not mistaken on a List implementation or another data structure to handle += and -= assignments. On the other hand, the explicit syntax (comparable to properties in C#) doesn't depend on any library, because basically only offers an alternate syntax for method calls to add and remove (similar to properties with get and set).

On the other hand, one complaint about properties I heard was "this way you can't differentiate between a direct member access and an (implicit) method call just by looking at the call itself". This would apply to events as well.

Nice downvoting, thanks again.

4 minutes ago, Sound Master said:

Nice downvoting, thanks again.

Well, your post wasn't helpful at all. Instead of just saying "You got brainwashed by microsoft", one should look at the advantages and disadvantages of a certain feature, and how well it could be integrated into an existing language/system.

C++ is a long term language for huge projects
If C++ somehow added all those high-level features into the standard library, there would be even more legacy to maintain until C++ has to break backward compatibility or die. If you want a fresh start, there are many competing languages with a better balance of safety, speed and readability, but their elegance comes at the cost of being forgotten when the next shiny language arrives with adaptations for the next hardware or abstract platform.

STD is already bloated and outdated
The C++ standard collections belong to a museum by having older parts optimized for single-core computers with small but fast memory that's insignificant compared to computation. Today we have huge but slow memory with fast computation and using a linked list as a default container is like a bad joke about cache misses. This obsoletes iterators and the whole system that relies on them. Third-party libraries can be changed with every new project, adapt for custom needs using operand overloads and take advantage of the latest hardware using SIMD extensions and GPU features.

What do you mean linked list cache misses ?

47 minutes ago, Dawoodoz said:

This obsoletes iterators and the whole system that relies on them.

Why do you think that iterators are obsolete ?

Few days ago, while wanting to optimize a part of a project, I removed an std::vector since I was doing many push_back in it, and replaced it with a good old C-style array. And you know what ? std::vector was far more fast.

Except if you deal with big-data (and few are here), std::vector will give you very good performances, when used well.

This topic is closed to new replies.

Advertisement