digitalmars.D - C++ frequently questioned answers
- Walter Bright (2/2) Oct 26 2007 An interesting and sometimes amusing read:
- dominik (11/13) Oct 26 2007 speaking of faq - not fqa :)
- Bruce Adams (13/16) Oct 26 2007 In amongst the religous fervour (of the defective C++ bit) there are som...
- Nathan Reed (4/5) Oct 26 2007 Isn't inlining available via the compiler switch -inline (for DMD)?
- Bruce Adams (4/11) Oct 27 2007 Good point. I'm a gcc/gdc person by preference. But it is an optimisatio...
- Bruno Medeiros (5/18) Oct 27 2007 What's so surprising about that, even for a Java newcomer?
- Bruce Adams (2/22) Oct 27 2007 Its the fact that a simple typo can cause such mayhem. Remove the void f...
- Bruno Medeiros (6/30) Oct 27 2007 No one in the Java world writes classes with lowercase first letter
- Bruce Adams (2/34) Oct 27 2007 Oh no! My example is now completely undermined and devalued!
- Ary Borenszweig (16/37) Oct 27 2007 In D:
- torhu (4/24) Oct 27 2007 Static constructors are allowed, they don't replace the regular
- Ary Borenszweig (27/52) Oct 27 2007 That was exactly what I meant, thanks for the response. :-)
- Regan Heath (3/34) Oct 29 2007 Has this been reported as a bug?
- Frits van Bommel (6/26) Oct 29 2007 IIRC Walter has said that "static this" needs to be two adjacent tokens
- Matti Niemenmaa (7/30) Oct 29 2007 And it's not only "Walter said", it's been explicitly stated in the doc ...
- Ary Borenszweig (5/34) Oct 29 2007 Yeah, for me it's not a bug either. I just wanted to point out that also...
- TomD (13/16) Oct 28 2007 The chapter about iostream tells me why I do not like Tango's IO :-)
- Nathan Reed (17/18) Oct 29 2007 *snip*
- Bruce Adams (4/28) Oct 29 2007 I have been bothered by that for many years. I had an idea to implement ...
An interesting and sometimes amusing read: http://yosefk.com/c++fqa/
Oct 26 2007
"Walter Bright" <newshound1 digitalmars.com> wrote in message news:fft2ak$2m38$1 digitalmars.com...An interesting and sometimes amusing read: http://yosefk.com/c++fqa/speaking of faq - not fqa :) someone should take a few minutes and write a few sentences to http://www.digitalmars.com/rtl/gc.html for programmers who never used GC and what should it mean to them, possibly with an example. And by example I mean "So, should I just declare pointers with objects on the heap with new and without delete anywhere or what?" .. and then a simple explanation with example. If it already exists somewhere on the site - there should be a FAQ: How to find stuff on this site
Oct 26 2007
Walter Bright Wrote:An interesting and sometimes amusing read: http://yosefk.com/c++fqa/In amongst the religous fervour (of the defective C++ bit) there are some genuine points. The amusement comes from the obviously java biased/blinded viewpoint of the author. It seems to me the main retorts to this fqa is the C++ is spiderman retort "With great power comes great responsibility" another less polite one that comes to mind is the Harry Hill retort "If its too hard I can't understand it". Templates add a whole new dimension to typing that flatlanders find hard to grasp at first. Leaving the poor education aside D seems to address all the genuine concerns except reflection and inlining. I know reflection will be added eventually but what about inlining. Isn't inlining available via mixins? [OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]
Oct 26 2007
Bruce Adams wrote:Isn't inlining available via mixins?Isn't inlining available via the compiler switch -inline (for DMD)? Thanks, Nathan Reed
Oct 26 2007
Nathan Reed Wrote:Bruce Adams wrote:Good point. I'm a gcc/gdc person by preference. But it is an optimisation and as such its not a bad idea to leave it as a compiler option. For a start you can inline differently from module to module that way. Then again a mixin would achieve the same localisation of optimisation in a compilation independent way. Regards, Bruce.Isn't inlining available via mixins?Isn't inlining available via the compiler switch -inline (for DMD)? Thanks, Nathan Reed
Oct 27 2007
Bruce Adams wrote:[OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]What's so surprising about that, even for a Java newcomer? -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 27 2007
Bruno Medeiros Wrote:Bruce Adams wrote:Its the fact that a simple typo can cause such mayhem. Remove the void from foo() and it becomes a constructor and everthing works just fine.[OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]What's so surprising about that, even for a Java newcomer? -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 27 2007
Bruce Adams wrote:Bruno Medeiros Wrote:No one in the Java world writes classes with lowercase first letter anyways. :P -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DBruce Adams wrote:Its the fact that a simple typo can cause such mayhem. Remove the void from foo() and it becomes a constructor and everthing works just fine.[OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]What's so surprising about that, even for a Java newcomer? -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 27 2007
Bruno Medeiros Wrote:Bruce Adams wrote:Oh no! My example is now completely undermined and devalued!Bruno Medeiros Wrote:No one in the Java world writes classes with lowercase first letter anyways. :P -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#DBruce Adams wrote:Its the fact that a simple typo can cause such mayhem. Remove the void from foo() and it becomes a constructor and everthing works just fine.[OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]What's so surprising about that, even for a Java newcomer? -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Oct 27 2007
Bruce Adams escribió:Walter Bright Wrote:In D: class Foo { private { int x, y; } static { float SomeGlobalVariable; this() { // Do some global initializations } } } Nope, no compiler warning here either. At least Eclipse gives you a warning saying "That method's name is the same as the class' name". :-)An interesting and sometimes amusing read: http://yosefk.com/c++fqa/In amongst the religous fervour (of the defective C++ bit) there are some genuine points. The amusement comes from the obviously java biased/blinded viewpoint of the author. It seems to me the main retorts to this fqa is the C++ is spiderman retort "With great power comes great responsibility" another less polite one that comes to mind is the Harry Hill retort "If its too hard I can't understand it". Templates add a whole new dimension to typing that flatlanders find hard to grasp at first. Leaving the poor education aside D seems to address all the genuine concerns except reflection and inlining. I know reflection will be added eventually but what about inlining. Isn't inlining available via mixins? [OT] Someone at work pointed out this java chestnut recently. If you've used java much you will know it but it may come as a shock to a mainly C++ coder. class foo { bar x; void foo () { x = new bar(); } } foo is NOT a constructor its actually a method named foo. The java compiler or interpreter generates a default constructor that sets all members to null so any use of x in any method of foo will result in a null pointer exception. This apparently compiles with no warnings. [/OT]
Oct 27 2007
Ary Borenszweig wrote: > In D:class Foo { private { int x, y; } static { float SomeGlobalVariable; this() { // Do some global initializations } } } Nope, no compiler warning here either. At least Eclipse gives you a warning saying "That method's name is the same as the class' name". :-)Static constructors are allowed, they don't replace the regular constructor. Or wasn't that what you meant?
Oct 27 2007
torhu escribió:Ary Borenszweig wrote: > In D:That was exactly what I meant, thanks for the response. :-) Try compiling and running these: 1. --- import std.stdio; class X { static this() { writefln("static"); } } void main() { } --- 2. --- import std.stdio; class X { static { this() { writefln("static"); } } } void main() { } ---class Foo { private { int x, y; } static { float SomeGlobalVariable; this() { // Do some global initializations } } } Nope, no compiler warning here either. At least Eclipse gives you a warning saying "That method's name is the same as the class' name". :-)Static constructors are allowed, they don't replace the regular constructor. Or wasn't that what you meant?
Oct 27 2007
Ary Borenszweig wrote:Try compiling and running these: 1. --- import std.stdio; class X { static this() { writefln("static"); } } void main() { } --- 2. --- import std.stdio; class X { static { this() { writefln("static"); } } } void main() { } ---Has this been reported as a bug? Regan
Oct 29 2007
Regan Heath wrote:Ary Borenszweig wrote:[snip normal "static this" version]Try compiling and running these:IIRC Walter has said that "static this" needs to be two adjacent tokens for it to be parsed as such, and this is not a bug but working as designed. I'm not sure whether this is a good thing or not, but it's the current state of affairs.2. --- import std.stdio; class X { static { this() { writefln("static"); } } } void main() { } ---Has this been reported as a bug?
Oct 29 2007
Frits van Bommel wrote:Regan Heath wrote:And it's not only "Walter said", it's been explicitly stated in the doc for as long as I can remember: http://digitalmars.com/d/1.0/class.html#StaticConstructor "The static in the static constructor declaration is not an attribute, it must appear immediately before the this" -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fiAry Borenszweig wrote:[snip normal "static this" version]IIRC Walter has said that "static this" needs to be two adjacent tokens for it to be parsed as such, and this is not a bug but working as designed.2. --- import std.stdio; class X { static { this() { writefln("static"); } } } void main() { } ---Has this been reported as a bug?
Oct 29 2007
Matti Niemenmaa escribió:Frits van Bommel wrote:Yeah, for me it's not a bug either. I just wanted to point out that also in D there are some things that may not be that obvious to a newcomer. But it's not a big deal (same as "void foo()" isn't a big deal in the previous example).Regan Heath wrote:And it's not only "Walter said", it's been explicitly stated in the doc for as long as I can remember: http://digitalmars.com/d/1.0/class.html#StaticConstructor "The static in the static constructor declaration is not an attribute, it must appear immediately before the this"Ary Borenszweig wrote:[snip normal "static this" version]IIRC Walter has said that "static this" needs to be two adjacent tokens for it to be parsed as such, and this is not a bug but working as designed.2. --- import std.stdio; class X { static { this() { writefln("static"); } } } void main() { } ---Has this been reported as a bug?
Oct 29 2007
Walter Bright Wrote:An interesting and sometimes amusing read: http://yosefk.com/c++fqa/The chapter about iostream tells me why I do not like Tango's IO :-) But I wonder if the following is also true for D: ----snip--- [10.1] What's the deal with constructors? FAQ: A constructor initializes an object given a chunk of memory having arbitrary (undefined) state, the way "init functions" do. It may acquire resource like memory, files, etc. "Ctor" is a common abbreviation. FQA: That's right - constructors initialize objects. In particular, constructors don't allocate the chunk of memory used for storing the object. This is done by the code calling a constructor. The compiler thus has to know the size of a memory chunk needed to store an object of a class (this is the value substituted for sizeof(MyClass)) at each point where the class is used. That means knowing all members (public & private). This is a key reason why changing the private parts of a C++ class definition requires recompilation of all calling code, effectively making the private members a part of the public interface. ---snap--- Given the compilation speed of DMD, it does not bother me, but still, he has a point here. Ciao Tom
Oct 28 2007
TomD wrote:But I wonder if the following is also true for D:*snip* I believe it is. If you take a look at .di files generated by DMD you will see they contain all of a class's instance variables (public and private). They also contain implementations of certain methods and constructors (usually short ones), presumably for inlining. While it does violate encapsulation to some extent (ideally, I shouldn't have to know how big a class is to create an instance of it, and I shouldn't need to recompile client code if I add an instance variable) the amount of harm done is small IMHO, and allowing inlining of small class methods (and constructors) is well worth it. Also, in case someone wants to truly hide the implementation, it's always possible to make an interface of all the public functions, and a factory function for creating instances, and put the actual implementation class in another module. Thanks, Nathan Reed
Oct 29 2007
TomD Wrote:Walter Bright Wrote:I have been bothered by that for many years. I had an idea to implement truly opaque types by having the size of the object used as a link time variable. If I recall it wasn't possible using the ELF binary format. I think I ended up using code generation and the PIMPL idiom instead. However, D is not constrained to use an existing object format itself only for its C/C++ interface. Regards, Bruce.An interesting and sometimes amusing read: http://yosefk.com/c++fqa/The chapter about iostream tells me why I do not like Tango's IO :-) But I wonder if the following is also true for D: ----snip--- [10.1] What's the deal with constructors? FAQ: A constructor initializes an object given a chunk of memory having arbitrary (undefined) state, the way "init functions" do. It may acquire resource like memory, files, etc. "Ctor" is a common abbreviation. FQA: That's right - constructors initialize objects. In particular, constructors don't allocate the chunk of memory used for storing the object. This is done by the code calling a constructor. The compiler thus has to know the size of a memory chunk needed to store an object of a class (this is the value substituted for sizeof(MyClass)) at each point where the class is used. That means knowing all members (public & private). This is a key reason why changing the private parts of a C++ class definition requires recompilation of all calling code, effectively making the private members a part of the public interface. ---snap--- Given the compilation speed of DMD, it does not bother me, but still, he has a point here. Ciao Tom
Oct 29 2007