July 25, 2017 Re: Why structs and classes instanciations are made differently ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Houdini | On Tuesday, 25 July 2017 at 15:56:45 UTC, Houdini wrote: > Yes, but it isn't the default way in C++ to do dynamic instanciation. https://github.com/isocpp/CppCoreGuidelines this? It's only 2 years old. The new operator predates it by decades. |
July 26, 2017 Re: Why structs and classes instanciations are made differently ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Tuesday, 25 July 2017 at 17:16:00 UTC, Kagamin wrote:
> On Tuesday, 25 July 2017 at 15:56:45 UTC, Houdini wrote:
>> Yes, but it isn't the default way in C++ to do dynamic instanciation.
>
> https://github.com/isocpp/CppCoreGuidelines this? It's only 2 years old. The new operator predates it by decades.
I meant :
When you need to instantiate a class, you usually do :
MyClass a;
and not :
MyClass* a = new MyClass();
You're in a value model.
If you find anything in Cpp Guidelines against that, I am interested.
|
July 26, 2017 Re: Why structs and classes instanciations are made differently ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 24 July 2017 at 17:42:30 UTC, Steven Schveighoffer wrote:
> On 7/24/17 11:45 AM, Houdini wrote:
>> On Monday, 24 July 2017 at 15:41:33 UTC, Steven Schveighoffer wrote:
>>
>>> Because types with inheritance generally don't work right if you pass by value (i.e. the slicing problem).
>>>
>>> structs don't support inheritance or virtual functions, so they can be safely passed by value.
>>
>> But in C++, we pass them by reference also to avoid copies (const &).
>> The potential polymorphic usage is not the only point to consider.
>>
>
> In C++ class and struct are pretty much interchangeable, so technically, class is a wasted keyword for default visibility.
>
> In D, I would use classes for any time I need polymorphism, and use structs otherwise.
>
> -Steve
It has also the nice property that porting code from Java/C# is actually really easy when using classes as it has more or less the same semantic. When porting code from C and C++ it is often better to use structs.
|
July 26, 2017 Re: Why structs and classes instanciations are made differently ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Houdini | On 07/26/2017 02:54 AM, Houdini wrote: > On Tuesday, 25 July 2017 at 17:16:00 UTC, Kagamin wrote: >> On Tuesday, 25 July 2017 at 15:56:45 UTC, Houdini wrote: >>> Yes, but it isn't the default way in C++ to do dynamic instanciation. >> >> https://github.com/isocpp/CppCoreGuidelines this? It's only 2 years >> old. The new operator predates it by decades. > > I meant : > > When you need to instantiate a class, you usually do : > > MyClass a; > > and not : > > MyClass* a = new MyClass(); > > You're in a value model. That's my experience as well. However, stack class objects are rare and to repeat Steven, it comes with the problem of slicing. Only after learning D that I realized there were two kinds of C++ types in my code: value types and reference types, latter of which I've achieved with boost::shared_ptr<C>. So, I think D's separation is the right choice. However, classes are unnecessarily expensive due to that 'monitor' member and carry the mistakes of OOP models adopted by Java, C++, and others. I say this under the influence of open multi-methods[1] and anemic domain models[2]. Ali [1] http://forum.dlang.org/thread/cigbfrgipbokyetskypk@forum.dlang.org [2] https://www.meetup.com/D-Lang-Silicon-Valley/events/228027468/ |
Copyright © 1999-2021 by the D Language Foundation