From -4275017224378129792
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,8c9f1307b83ac2db
X-Google-Attributes: gidf78e5,public
From: "Cristian Georgescu" <Cristian.Georgescu@worldnet.att.net>
Subject: Re: Q: virtual functions not overridable anymore?
Date: 1997/09/11
Message-ID: <01bcbe63$04fa8d20$6e3274cf@worldnet.worldnet.att.net>#1/1
X-Deja-AN: 271674728
References: <01bcb109$1e790180$433574cf@worldnet.worldnet.att.net> <3404A0AE.6F7E@Eng.Sun.COM> <5uimip$l56@bgtnsc03.worldnet.att.net> <340D93AA.643A@Eng.Sun.COM> <01bcb951$ea80a440$909c389d@mtayler> <ocr4t7zlk2o.fsf@ml.com> <5ut97j$nkr@bgtnsc02.worldnet.att.net> <ocr4t7tgprf.fsf@ml.com>
X-Original-Date: 11 Sep 1997 03:32:28 GMT
Organization: Opal Technologies
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBNBhPW0y4NqrwXLNJAQEGdwH/US9JapKlZbiVlXPN7VpvoyDgP2xQnsXL 61in+cTHWkudSvl5m9rYa1NUk7tlPV4d69T3sZnA6lrTOL5fBeFJbg== =FO8Z
Newsgroups: comp.std.c++
Originator: austern@isolde.mti.sgi.com


Colin Rafferty <craffert@ml.com> wrote in article
<ocr4t7tgprf.fsf@ml.com>...
> Cristian Georgescu writes:
> > Consider an Apple base class with a getColor() virtual function. Then I
> > have a YellowApple class derived from Apple, and the getColor()
overridden
> > to return the color Yellow. 
> 
> > The idea is that I do not want the classes that inherit from
YellowApple to
> > override the getColor().
> 
> You certainly do.  If I define LightYellowApple, I would want the
> getColor() to return an instance of LightYellow (derived from Yellow).

This was not my point. Color is an enumeration. 
Yor next remark states better what I meant:

> On the other hand, if the Color attribute is a choice out of a static
> list, then there is no good reason for getColor() to be virtual in the
> base class -- it should be just returning an attribute.

I think that there is a good reason for getColor() to be virtual in the
base class:

class Apple {
public:
  enum Color {red, yellow, green};

  Apple::Color getColor();
  //...
}

class YellowApple {
public:

  Apple::Color getColor(){return Apple::Yellow;};
  //..
}

The idea is that you may have an Apple with an unknown color.
You construct the Apple at night but you cannot see the color, therefore
you set the color attribute later, when you see beter.

Then, if you have something like this:

void paintApple(Apple& anApple){
  //..
  buySomePaint( anApple.getColor() );
}

and you pass an YellowApple, you just want to make sure that whoever is
buying the paint for you is buying Yellow color pain.

For the function paintApple to work well, you have to make the getColor
vitual; The example becomes:

class Apple {
public:
  enum Color {red, yellow, green};

  virtual Apple::Color getColor();
  //...
}

class YellowApple {
public:

  virtual Apple::Color getColor(){return Apple::Yellow;};
  //..
}

-- 
Cristian Georgescu
_________________________________________________
    Smith Industries
    Aerospace & Defense Systems
    7-9 Vreeland Road,
    Florham Park, NJ 07932, USA.
_________________________________________________
E-mail: Georgescu_Christian@si.com      
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



