Jump to page: 1 2
Thread overview
access subclass functions
Nov 20, 2008
Saaa
Nov 20, 2008
Saaa
Nov 20, 2008
Saaa
Nov 20, 2008
BCS
Nov 20, 2008
Kagamin
Nov 20, 2008
Saaa
Nov 21, 2008
Kagamin
Nov 21, 2008
Saaa
Nov 20, 2008
sclytrack
Nov 20, 2008
Saaa
Nov 21, 2008
Christopher Wright
Nov 21, 2008
Saaa
Nov 21, 2008
Christopher Wright
Nov 21, 2008
Saaa
November 20, 2008
Is this not possible, or am I doing anything wrong?

Fruit[2] fruits; // Fruit has no peel function

fruit[0]= new Apple();
fruit[1]= new Banana(); //Banana has a protected peel() function returning a
bool

bool b;
b=fruit[1].peal();

Error: no property 'peel' for type 'project.fruitclass.Fruit'
Error: function expected before (), not 1 of type int
Error: cannot implicitly convert expression (cast(Banana)1()) of type
'project.banana.Banana' to bool


November 20, 2008
"Saaa" wrote
> Is this not possible, or am I doing anything wrong?
>
> Fruit[2] fruits; // Fruit has no peel function
>
> fruit[0]= new Apple();
> fruit[1]= new Banana(); //Banana has a protected peel() function returning
> a bool
>
> bool b;
> b=fruit[1].peal();
>
> Error: no property 'peel' for type 'project.fruitclass.Fruit'
> Error: function expected before (), not 1 of type int
> Error: cannot implicitly convert expression (cast(Banana)1()) of type
> 'project.banana.Banana' to bool

I think you have a typo in your example (and your example doesn't reflect the error messages), but that isn't the problem.

In a strongly typed language, you cannot access subclass functions that aren't defined in the base class unless you are sure the instance is of the subclass type.

to fix, you should do this:

if(auto ban = cast(Banana)fruit[1])
  ban.peel();

the if(auto ban = ...) ensures that fruit[1] is a Banana.  If you are sure it will always be a banana (i.e. the cast doesn't return null), you can do:

(cast(Banana)fruit[1]).peel();

-Steve


November 20, 2008
>
> I think you have a typo in your example
:)
> (and your example doesn't reflect the error messages),
How do you mean? Like not at all?
As you noticed it isn't the actual code, but I thought it would reflect it
though.

> but that isn't the problem.
>
> In a strongly typed language, you cannot access subclass functions that aren't defined in the base class unless you are sure the instance is of the subclass type.
>
> to fix, you should do this:
>
> if(auto ban = cast(Banana)fruit[1])
>  ban.peel();
>
> the if(auto ban = ...) ensures that fruit[1] is a Banana.  If you are sure it will always be a banana (i.e. the cast doesn't return null), you can do:
>
> (cast(Banana)fruit[1]).peel();
Yay, thanks!
Now that you mention it, I think I've had this problem before :/
even though it is really logical, thx again.

In stead of numbers I use an enum, which makes it really difficult to get
this wrong:
fruits[BANANA].peel();
:D
Now that I got this out of the way I see peel() should be public.


November 20, 2008
== Quote from Saaa (empty@needmail.com)'s article
> Is this not possible, or am I doing anything wrong?
> Fruit[2] fruits; // Fruit has no peel function
> fruit[0]= new Apple();
> fruit[1]= new Banana(); //Banana has a protected peel() function returning a
> bool
> bool b;
> b=fruit[1].peal();
> Error: no property 'peel' for type 'project.fruitclass.Fruit'
> Error: function expected before (), not 1 of type int
> Error: cannot implicitly convert expression (cast(Banana)1()) of type
> 'project.banana.Banana' to bool


from the list (private, protected, public) pick public.
Note the difference between peel and peal.


public YellowBanana: Banana
{
  void doStuff()
  {
     bool e = peel();   //visible from derived
                        //class when defined protected or public.
  }
}

Banana a = new Banana();
bool f = a.peel();      //visible only when public

All non-static non-private non-template member functions are virtual.
(Rule only valid in D)

If you want to peel your fruit your fruit needs a peel member function,
or else you need to cast it (dynamic_cast). You can make an abstract
virtual function (pure virtual function).

What kind of a programme are you making? Just curious.
November 20, 2008
"Saaa" wrote
>> (and your example doesn't reflect the error messages),
> How do you mean? Like not at all?
> As you noticed it isn't the actual code, but I thought it would reflect it
> though.

I mean, you have some cast error, but there is no casting in your code sample.

-Steve


November 20, 2008
My bad, I meant:

b=cast(Banana)fruits[1].peel();


"Steven Schveighoffer" <schveiguy@yahoo.com> wrote in message news:gg3p0v$2494$1@digitalmars.com...
> "Saaa" wrote
>>> (and your example doesn't reflect the error messages),
>> How do you mean? Like not at all?
>> As you noticed it isn't the actual code, but I thought it would reflect
>> it though.
>
> I mean, you have some cast error, but there is no casting in your code sample.
>
> -Steve
> 


November 20, 2008
Saaa Wrote:

> In stead of numbers I use an enum, which makes it really difficult to get
> this wrong:
> fruits[BANANA].peel();

why not using struct?
November 20, 2008
>
>
> from the list (private, protected, public) pick public.
> Note the difference between peel and peal.
:)
>
>
> public YellowBanana: Banana
> {
>  void doStuff()
>  {
>     bool e = peel();   //visible from derived
>                        //class when defined protected or public.
>  }
> }
>
> Banana a = new Banana();
> bool f = a.peel();      //visible only when public
>
> All non-static non-private non-template member functions are virtual.
> (Rule only valid in D)
What does this mean: to be virtual?

>
> If you want to peel your fruit your fruit needs a peel member function,
> or else you need to cast it (dynamic_cast). You can make an abstract
> virtual function (pure virtual function).
I can? :D

>
> What kind of a programme are you making? Just curious.
A game, containing loads of fruits.


November 20, 2008
"Kagamin" <spam@here.lot> wrote in message news:gg3t81$2flu$1@digitalmars.com...
> Saaa Wrote:
>
>> In stead of numbers I use an enum, which makes it really difficult to get
>> this wrong:
>> fruits[BANANA].peel();
>
> why not using struct?

How do you mean?
What should be a struct?


November 20, 2008
Reply to Saaa,

> 
> b=cast(Banana)fruits[1].peel();
> 

I'm not shure that will work. IIRC DMD reads that as

b=cast(Banana)( fruits[1].peel() );


« First   ‹ Prev
1 2