digitalmars.D.learn - Why structs and classes instanciations are made differently ?
- Houdini (7/7) Jul 24 2017 Hello,
- Andrea Fontana (3/10) Jul 24 2017 Maybe this will help you:
- Houdini (2/4) Jul 24 2017 Thanks for this informative link.
- Steven Schveighoffer (6/13) Jul 24 2017 Because types with inheritance generally don't work right if you pass by...
- Houdini (5/9) Jul 24 2017 But in C++, we pass them by reference also to avoid copies (const
- via Digitalmars-d-learn (2/3) Jul 24 2017 Exactly... in C++ you basically always pass by reference, so D made that...
- Steven Schveighoffer (6/17) Jul 24 2017 In C++ class and struct are pretty much interchangeable, so technically,...
- Houdini (4/6) Jul 25 2017 OK, I'll adhere to this method. :)
- Patrick Schluter (6/26) Jul 26 2017 It has also the nice property that porting code from Java/C# is
- Kagamin (4/7) Jul 25 2017 C++ is big, there's always something you don't know about it.
- Houdini (4/7) Jul 25 2017 Yes, but it isn't the default way in C++ to do dynamic
- Kagamin (3/5) Jul 25 2017 https://github.com/isocpp/CppCoreGuidelines this? It's only 2
- Houdini (9/14) Jul 26 2017 I meant :
- =?UTF-8?Q?Ali_=c3=87ehreli?= (13/25) Jul 26 2017 That's my experience as well. However, stack class objects are rare and
Hello, I am a C++ coder, and I am learning D (just reading a book, for now). D is very similar to C++ (and also grabs godd ideas from Python), but I have a naive question : why does Walter Bright chose to instanciate classes like in Java ? And why is it different for structs ?
Jul 24 2017
On Monday, 24 July 2017 at 15:21:54 UTC, Houdini wrote:Hello, I am a C++ coder, and I am learning D (just reading a book, for now). D is very similar to C++ (and also grabs godd ideas from Python), but I have a naive question : why does Walter Bright chose to instanciate classes like in Java ? And why is it different for structs ?Maybe this will help you: https://stackoverflow.com/questions/10965577/usage-preference-between-a-struct-and-a-class-in-d-language
Jul 24 2017
On Monday, 24 July 2017 at 15:37:51 UTC, Andrea Fontana wrote:Maybe this will help you: https://stackoverflow.com/questions/10965577/usage-preference-between-a-struct-and-a-class-in-d-languageThanks for this informative link.
Jul 24 2017
On 7/24/17 11:21 AM, Houdini wrote:Hello, I am a C++ coder, and I am learning D (just reading a book, for now). D is very similar to C++ (and also grabs godd ideas from Python), but I have a naive question : why does Walter Bright chose to instanciate classes like in Java ? And why is it different for structs ?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. -Steve
Jul 24 2017
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.
Jul 24 2017
On Mon, Jul 24, 2017 at 03:45:29PM +0000, Houdini via Digitalmars-d-learn wrote:But in C++, we pass them by reference also to avoid copies (const &).Exactly... in C++ you basically always pass by reference, so D made that the default.
Jul 24 2017
On 7/24/17 11:45 AM, Houdini wrote:On Monday, 24 July 2017 at 15:41:33 UTC, Steven Schveighoffer wrote: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. -SteveBecause 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.
Jul 24 2017
On Monday, 24 July 2017 at 17:42:30 UTC, Steven Schveighoffer wrote:In D, I would use classes for any time I need polymorphism, and use structs otherwise.OK, I'll adhere to this method. :) Thanks to all for your answers.
Jul 25 2017
On Monday, 24 July 2017 at 17:42:30 UTC, Steven Schveighoffer wrote:On 7/24/17 11:45 AM, Houdini wrote: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.On Monday, 24 July 2017 at 15:41:33 UTC, Steven Schveighoffer wrote: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. -SteveBecause 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.
Jul 26 2017
On Monday, 24 July 2017 at 15:21:54 UTC, Houdini wrote:D is very similar to C++ (and also grabs godd ideas from Python), but I have a naive question : why does Walter Bright chose to instanciate classes like in Java ?C++ is big, there's always something you don't know about it. Java actually instantiates classes the C++ way: http://en.cppreference.com/w/cpp/language/new
Jul 25 2017
On Tuesday, 25 July 2017 at 15:15:59 UTC, Kagamin wrote:C++ is big, there's always something you don't know about it. Java actually instantiates classes the C++ way: http://en.cppreference.com/w/cpp/language/newYes, but it isn't the default way in C++ to do dynamic instanciation. Usually, you do static initialization, except when the situation make taht impossible.
Jul 25 2017
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.
Jul 25 2017
On Tuesday, 25 July 2017 at 17:16:00 UTC, Kagamin wrote:On Tuesday, 25 July 2017 at 15:56:45 UTC, Houdini wrote: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.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.
Jul 26 2017
On 07/26/2017 02:54 AM, Houdini wrote:On Tuesday, 25 July 2017 at 17:16:00 UTC, Kagamin wrote: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/On Tuesday, 25 July 2017 at 15:56:45 UTC, Houdini wrote: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.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.
Jul 26 2017