digitalmars.D.learn - subclass to base class but not subclass pointer to base class pointer?
- Namespace (6/6) Apr 20 2013 Why can D implicitly cast from the subclass to the base class,
- John Colvin (27/33) Apr 20 2013 a pointer to class in D is actually a pointer to a pointer as
- Andrej Mitrovic (5/11) Apr 20 2013 I'd really recommend reading one of the D books before asking so many
- John Colvin (2/17) Apr 20 2013 It's not allowed in c++ either. See my original reply.
- Namespace (8/33) Apr 20 2013 It works fine in C++ (of course with -std=c++0x)
- John Colvin (5/7) Apr 20 2013 It's been a while since I did any c++ but:
- Namespace (6/13) Apr 20 2013 I return a pointer from an instance of B and the compiler cast it
- Namespace (5/10) Apr 20 2013 Ah, I think I understand know what you meant.
- John Colvin (14/16) Apr 20 2013 Sorry, no.
- John Colvin (2/18) Apr 20 2013 woops, that should be uint in the D declaration not unsinged int.
Why can D implicitly cast from the subclass to the base class, but not implicitly from the subclasse pointer to the base class pointer? This works: http://dpaste.1azy.net/30dd34a0 This not: http://dpaste.1azy.net/ffacfd83 Makes not much sense for me.
Apr 20 2013
On Saturday, 20 April 2013 at 14:56:25 UTC, Namespace wrote:Why can D implicitly cast from the subclass to the base class, but not implicitly from the subclasse pointer to the base class pointer? This works: http://dpaste.1azy.net/30dd34a0 This not: http://dpaste.1azy.net/ffacfd83 Makes not much sense for me.a pointer to class in D is actually a pointer to a pointer as classes are reference types. Polymorphism for pointers to pointers to objects is not allowed. Consider this (ported from here https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.c++/kNZ-ksORPwU): class Base { void doBaseThings(); } class Dirv { doDirvThings(); } void AssignNewBaseTo(Base* basePtr) { *basePtr = new Base; } void Oops() { Dirv dirv; AssignNewBaseTo(&dirv); dirv.doDirvThings(); // runtime crash -- a Base object // being asked to do Dirv things. }
Apr 20 2013
On 4/20/13, Namespace <rswhite4 googlemail.com> wrote:Why can D implicitly cast from the subclass to the base class, but not implicitly from the subclasse pointer to the base class pointer? This works: http://dpaste.1azy.net/30dd34a0 This not: http://dpaste.1azy.net/ffacfd83 Makes not much sense for me.I'd really recommend reading one of the D books before asking so many fundamental questions: http://wiki.dlang.org/Books And probably the spec too. Bottom line: D is not C++.
Apr 20 2013
On Saturday, 20 April 2013 at 15:32:20 UTC, Andrej Mitrovic wrote:On 4/20/13, Namespace <rswhite4 googlemail.com> wrote:It's not allowed in c++ either. See my original reply.Why can D implicitly cast from the subclass to the base class, but not implicitly from the subclasse pointer to the base class pointer? This works: http://dpaste.1azy.net/30dd34a0 This not: http://dpaste.1azy.net/ffacfd83 Makes not much sense for me.I'd really recommend reading one of the D books before asking so many fundamental questions: http://wiki.dlang.org/Books And probably the spec too. Bottom line: D is not C++.
Apr 20 2013
On Saturday, 20 April 2013 at 15:34:50 UTC, John Colvin wrote:On Saturday, 20 April 2013 at 15:32:20 UTC, Andrej Mitrovic wrote:It works fine in C++ (of course with -std=c++0x) http://codepad.org/qLIjGGd4 But thanks for your answer. I know that D classes a reference types but I was thinking that the compiler could detect what I want and could implicit cast.On 4/20/13, Namespace <rswhite4 googlemail.com> wrote:It's not allowed in c++ either. See my original reply.Why can D implicitly cast from the subclass to the base class, but not implicitly from the subclasse pointer to the base class pointer? This works: http://dpaste.1azy.net/30dd34a0 This not: http://dpaste.1azy.net/ffacfd83 Makes not much sense for me.I'd really recommend reading one of the D books before asking so many fundamental questions: http://wiki.dlang.org/Books And probably the spec too. Bottom line: D is not C++.Not very helpful. But why "many"? I cannot remember one "fundamental question".I'd really recommend reading one of the D books before asking so many fundamental questions: http://wiki.dlang.org/Books And probably the spec too.
Apr 20 2013
On Saturday, 20 April 2013 at 16:36:09 UTC, Namespace wrote:It works fine in C++ (of course with -std=c++0x) http://codepad.org/qLIjGGd4It's been a while since I did any c++ but: 1) I don't see any pointer to pointer polymorphism in there. 2) That c++ code is equivalent to the version of your D code that works, not the version that doesn't.
Apr 20 2013
On Saturday, 20 April 2013 at 17:24:33 UTC, John Colvin wrote:On Saturday, 20 April 2013 at 16:36:09 UTC, Namespace wrote:I return a pointer from an instance of B and the compiler cast it implicit to A*. Look what is inside of 'bstore' (instances of B) and what the type of 'get' is (A*). And no, the equivalent version would use 'A' as return type of 'get', not 'A*'.It works fine in C++ (of course with -std=c++0x) http://codepad.org/qLIjGGd4It's been a while since I did any c++ but: 1) I don't see any pointer to pointer polymorphism in there. 2) That c++ code is equivalent to the version of your D code that works, not the version that doesn't.
Apr 20 2013
I return a pointer from an instance of B and the compiler cast it implicit to A*. Look what is inside of 'bstore' (instances of B) and what the type of 'get' is (A*). And no, the equivalent version would use 'A' as return type of 'get', not 'A*'.Ah, I think I understand know what you meant. Sure there is no pointer to pointer polymorphism in there. But I thought, even if D classes are reference types and not really compareable to C++ classes, that the compiler could do the same implicit cast for pointer of D classes. ;)
Apr 20 2013
On Saturday, 20 April 2013 at 17:42:02 UTC, Namespace wrote:And no, the equivalent version would use 'A' as return type of 'get', not 'A*'.Sorry, no. //C++ A* get(unsigned int id); //D A get(unsigned int id); Those are the equivalent declarations. A reference is, for these intents and purposes, a pointer and D classes are reference types. In D, "get" returns a reference to an object, i.e. a pointer to an object. C++ does not have polymorphic pointers to pointers to class objects, for good reason (see my original reply). AFAIK the same reasons apply to D, hence D has no polymorphic pointers to references to class objects.
Apr 20 2013
On Saturday, 20 April 2013 at 21:36:37 UTC, John Colvin wrote:On Saturday, 20 April 2013 at 17:42:02 UTC, Namespace wrote:woops, that should be uint in the D declaration not unsinged int.And no, the equivalent version would use 'A' as return type of 'get', not 'A*'.Sorry, no. //C++ A* get(unsigned int id); //D A get(unsigned int id); Those are the equivalent declarations. A reference is, for these intents and purposes, a pointer and D classes are reference types. In D, "get" returns a reference to an object, i.e. a pointer to an object. C++ does not have polymorphic pointers to pointers to class objects, for good reason (see my original reply). AFAIK the same reasons apply to D, hence D has no polymorphic pointers to references to class objects.
Apr 20 2013