www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ frequently questioned answers

reply Walter Bright <newshound1 digitalmars.com> writes:
An interesting and sometimes amusing read:

http://yosefk.com/c++fqa/
Oct 26 2007
next sibling parent "dominik" <aha aha.com> writes:
"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
prev sibling next sibling parent reply Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
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
next sibling parent reply Nathan Reed <nathaniel.reed gmail.com> writes:
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
parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Nathan Reed Wrote:

 Bruce Adams wrote:
Isn't inlining available via mixins?
Isn't inlining available via the compiler switch -inline (for DMD)? Thanks, Nathan Reed
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.
Oct 27 2007
prev sibling next sibling parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
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
parent reply Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Bruno Medeiros Wrote:

 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
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.
Oct 27 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Bruce Adams wrote:
 Bruno Medeiros Wrote:
 
 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
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.
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#D
Oct 27 2007
parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Bruno Medeiros Wrote:

 Bruce Adams wrote:
 Bruno Medeiros Wrote:
 
 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
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.
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#D
Oh no! My example is now completely undermined and devalued!
Oct 27 2007
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Bruce Adams escribió:
 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]
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". :-)
Oct 27 2007
parent reply torhu <no spam.invalid> writes:
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
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
torhu escribió:
 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?
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() { } ---
Oct 27 2007
parent reply Regan Heath <regan netmail.co.nz> writes:
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
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Regan Heath wrote:
 Ary Borenszweig wrote:
 Try compiling and running these:
[snip normal "static this" version]
 2.
 ---
 import std.stdio;

 class X {
     static {
             this() {
         writefln("static");
             }
     }
 }

 void main() {
 }
 ---
Has this been reported as a bug?
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.
Oct 29 2007
parent reply Matti Niemenmaa <see_signature for.real.address> writes:
Frits van Bommel wrote:
 Regan Heath wrote:
 Ary Borenszweig wrote:
[snip normal "static this" version]
 2.
 ---
 import std.stdio;

 class X {
     static {
             this() {
         writefln("static");
             }
     }
 }

 void main() {
 }
 ---
Has this been reported as a bug?
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.
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) fi
Oct 29 2007
parent Ary Borenszweig <ary esperanto.org.ar> writes:
Matti Niemenmaa escribió:
 Frits van Bommel wrote:
 Regan Heath wrote:
 Ary Borenszweig wrote:
[snip normal "static this" version]
 2.
 ---
 import std.stdio;

 class X {
     static {
             this() {
         writefln("static");
             }
     }
 }

 void main() {
 }
 ---
Has this been reported as a bug?
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.
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"
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).
Oct 29 2007
prev sibling next sibling parent reply TomD <t_demmer no.spam.web.de> writes:
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
parent Nathan Reed <nathaniel.reed gmail.com> writes:
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
prev sibling parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
TomD Wrote:

 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
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.
Oct 29 2007