| Thread overview | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 20, 2013 subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
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. | ||||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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. } | |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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++. | |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Saturday, 20 April 2013 at 15:32:20 UTC, Andrej Mitrovic wrote:
> 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++.
It's not allowed in c++ either. See my original reply.
| |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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: >> 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++. > > It's not allowed in c++ either. See my original reply. 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. >> 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. Not very helpful. But why "many"? I cannot remember one "fundamental question". | |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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/qLIjGGd4
It'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.
| |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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:
>> It works fine in C++ (of course with -std=c++0x)
>> http://codepad.org/qLIjGGd4
>
> It'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.
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*'.
| |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | > 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. ;)
| |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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.
| |||
April 20, 2013 Re: subclass to base class but not subclass pointer to base class pointer? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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:
>> 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.
woops, that should be uint in the D declaration not unsinged int.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply