Topic: Pure Virtual Implementation??


Author: AllanW@my-dejanews.com
Date: 1998/11/18
Raw View

> Paul Grealish wrote:
> > But, of course the 'ill-formed' line should read:
> >
> >             virtual void f() =0 { }
> >
> > Maybe this has been corrected in the Standard proper?

In article <36519B8F.2781@wizard.net>,
  James Kuyper <kuyper@wizard.net> wrote:
> That note merely summarizes a fact which can be derived from the grammar
> given in section 9.2. Specifically, a member-declaration can be a
> function-definition, or a member-declarator-list. A pure-specifier can
> only occur within a member-declarator, which is not part of a
> function-definition. Your 'corrected' form is equally invalid.

Well, yes. But the standard shows a form that nobody would ever
try; this 'corrected' form is one that a lot of us would expect to
work, were it not for the example in the standard. And it even
does work on some compilers, which will help to convince still
others that it's legal, and so on. The standard ought to show
the form listed above.

Also, consider that this:
    class myClass {
    public:
        virtual void f() =0;
    };
    inline void myClass::f() { }
is legal. So why is the 'corrected' form above invalid?

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Chris Ahlstrom <ahlstroc@bellsouth.net>
Date: 1998/11/18
Raw View
Perhaps it is one of those Microsoft extensions.   Turn on ANSI
compliance and see what happens (I'd like to know).

Chris


> > > I've been told/lectured, that you cannot implement a pure virtual method in
> > > C++.
> > >
> > > ie. the following, if stuck in a class, will not compile...
> > >
> > >    virtual void Display() const = 0
> > >    {
> > >      // NOT ON!
> > >       cout << "In: virtual void Display() const = 0..." << endl;
> > >    }
> > >
> > > Is this correct?
> >
> > But, mysteriously, VC++ accepted this code.
> You are right, VC is wrong.  It does not even emit a warning at the highest
> warning level.
>
> > Can someone clarify whether this is legal or not?
> Definitely not legal.
>
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/11/18
Raw View
> Biju Thomas wrote:
> > In 10.4/2 of the standard, there is a note, saying:
> >
> > "A function declaration cannot provide both a pure specifier and a
> > definition".

In article <3651694B.49B6@uk.geopak-tms.com>,
  paul.grealish@uk.geopak-tms.com wrote:
> So there is.  And there's even an example:
>  struct C {
>      virtual void f() { }=0; // ill-formed
>  };
>
> But, of course the 'ill-formed' line should read:
>
>      virtual void f() =0 { }
>
> Maybe this has been corrected in the Standard proper?

No, this section is word-for-word identical to CD2, even to
the placement of the =0 in the "ill-formed" example.

The comment is not incorrect, as far as it goes; the
statement is in fact ill-formed.

Also this is a note, and therefore non-normative. Does
that mean that it "doesn't count" and that statements of
the second form (with the =0 before the function body)
are actually valid? If it does mean this, why is the note
there at all? But if it doesn't mean this, then why is it
a note instead of normative text?

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: ark@research.att.com (Andrew Koenig)
Date: 1998/11/18
Raw View
In article <72sfkc$6jl$1@nnrp1.dejanews.com>,  <AllanW@my-dejanews.com> wrote:

> > So there is.  And there's even an example:
> >  struct C {
> >      virtual void f() { }=0; // ill-formed
> >  };

> > But, of course the 'ill-formed' line should read:

> >      virtual void f() =0 { }

> > Maybe this has been corrected in the Standard proper?

> No, this section is word-for-word identical to CD2, even to
> the placement of the =0 in the "ill-formed" example.

> The comment is not incorrect, as far as it goes; the
> statement is in fact ill-formed.

Indeed.  But it would have been clearer it it had appeared as

 virtual void f() = 0 { }

and I will make a note to change that when the committee eventually
gets around to issuing a correction.

> Also this is a note, and therefore non-normative. Does
> that mean that it "doesn't count" and that statements of
> the second form (with the =0 before the function body)
> are actually valid? If it does mean this, why is the note
> there at all? But if it doesn't mean this, then why is it
> a note instead of normative text?

Both examples are ill-formed because there's no way to derive them
from the grammar.  So the note doesn't need to be normative;
it's there only to make the normative part easier to understand.
I guess it didn't do that in this case :-)

--
    Andrew Koenig
    ark@research.att.com
    http://www.research.att.com/info/ark



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <bijuthom@ibm.net>
Date: 1998/11/16
Raw View
Callanans wrote:
>
> I've been told/lectured, that you cannot implement a pure virtual method in
> C++.
>
> ie. the following, if stuck in a class, will not compile...
>
>    virtual void Display() const = 0
>    {
>      // NOT ON!
>       cout << "In: virtual void Display() const = 0..." << endl;
>    }
>
> Is this correct?
>

[ Adding comp.std.c++ to followup groups ]

I thought this was illegal behaviour. In 10.4/2 of the standard, there
is a note, saying:

"A function declaration cannot provide both a pure specifier and a
definition".

So, according to that, the above definition will be illegal.

I also checked in the grammar provided in Stroustrup, C++PL, 3rd ed. I
could not figure out a way by which the above code will be legal.

But, mysteriously, VC++ accepted this code.

Can someone clarify whether this is legal or not?

OTOH, the following should be legal:

class Shape
{
  virtual void Display () const = 0;
};

void Shape::Display () const
{
  cout << "In: virtual void Display() const = 0..." << endl;
}

Regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/11/17
Raw View
In article <364DC706.1A2108D5@ibm.net>,
  bijuthom@ibm.net wrote:

> Callanans wrote:
> >
> > I've been told/lectured, that you cannot implement a pure
> > virtual method in C++.

That's not true. You CAN implement a pure virtual method in C++.
However, you cannot call that implementation unless you use
the class::member notation, like this:
    myObjectPtr->BaseClass::Display();


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/11/17
Raw View
Biju Thomas <bijuthom@ibm.net> writes:

>Callanans wrote:
>>
>> I've been told/lectured, that you cannot implement a pure virtual method in
>> C++.

Of course you can. In particular, you can declare a destructor to
be pure virtual. The destructor will be called whenever an object
derived from the class is destroyed, so you MUST implement the
destructor if any such object is ever destroyed.

>> ie. the following, if stuck in a class, will not compile...
>>
>>    virtual void Display() const = 0
>>    {
>>      // NOT ON!
>>       cout << "In: virtual void Display() const = 0..." << endl;
>>    }
>>
>> Is this correct?

>[ Adding comp.std.c++ to followup groups ]

>I thought this was illegal behaviour. In 10.4/2 of the standard, there
>is a note, saying:

>"A function declaration cannot provide both a pure specifier and a
>definition".

>So, according to that, the above definition will be illegal.

>I also checked in the grammar provided in Stroustrup, C++PL, 3rd ed. I
>could not figure out a way by which the above code will be legal.

>But, mysteriously, VC++ accepted this code.

>Can someone clarify whether this is legal or not?

Your analysis is correct. A compiler must produce a diagnostic
message about the code. VC++ might accept the code as an extension,
or it might be simply a compiler bug.

>OTOH, the following should be legal:

>class Shape
>{
>  virtual void Display () const = 0;
>};

>void Shape::Display () const
>{
>  cout << "In: virtual void Display() const = 0..." << endl;
>}

Yes, that's how you do it.

A note about a pure virtual function that gets implemented:
The function will NEVER be called via the virtual function
mechanism:
 Shape *ShapePtr = ... ;
 ShapePtr->Display() // never calls Shape::Display
The only way to call the function is by explicitly qualifying it:
 ShapePtr->Shape::Display(); // not a virtual call

My note might seem redundant. Since you cannot create an object
of type Shape, or of any object derived from Shape unless it
provides an override of the Display function, it would seem
impossible to call Shape::Display via the virtual mechanism
anyway.

Ordinarily that is true. But suppose you pass the "this"
pointer from the Shape constructor to an outside function:

void showit(Shape *p) { p->Display(); }

Shape::Shape() { showit(this); }

While the Shape constructor is running, the object temporarily
has type Shape, not the final most-derived class type. Function
showit expects to get a pointer to a complete object of some
type derived from Shape, and call a virtual function. Even in
this case, you will NOT get a call to Shape::Derived, because
a pure virtual function cannot be called via the virtual
mechanism. A special language rule covers this situation.

The result of such code is undefined, but on most systems you
get a run-time error message and program abort.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Paul Grealish <paul.grealish@uk.geopak-tms.com>
Date: 1998/11/17
Raw View
Biju Thomas wrote:
>
> Callanans wrote:
> >
> > I've been told/lectured, that you cannot implement a pure virtual method in
> > C++.
> >
> > (snip)
> >
> > Is this correct?

As I understand it, no.  To quote Meyers [1]:
"If the notion of implementing a pure virtual
function strikes you as odd, you just haven't
been getting out enough.  Declaring a function
pure virtual means:
o the current class is abstract (ie. cannot be
  instantiated), and
o any concrete class inheriting from the current
  class must declare the function as a "normal"
  virtual function (ie. without the "=0")"


> I thought this was illegal behaviour. In 10.4/2 of the standard, there
> is a note, saying:
>
> "A function declaration cannot provide both a pure specifier and a
> definition".

So there is.  And there's even an example:
 struct C {
     virtual void f() { }=0; // ill-formed
 };

But, of course the 'ill-formed' line should read:

     virtual void f() =0 { }

Maybe this has been corrected in the Standard proper?

[1] Scott Meyers "More Effective C++"
    Addison-Wesley ISBN: 0-201-63371-X
--
+---------------------------------+
|         Paul Grealish           |
|       GEOPAK-TMS Limited        |
|       Cambridge, England        |
| paul.grealish@uk.geopak-tms.com |
+---------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jim.hyslop@leitch.com
Date: 1998/11/17
Raw View
In article <364DC706.1A2108D5@ibm.net>,
  bijuthom@ibm.net wrote:
> Callanans wrote:
> >
> > I've been told/lectured, that you cannot implement a pure virtual method in
> > C++.
> >
> > ie. the following, if stuck in a class, will not compile...
> >
> >    virtual void Display() const = 0
> >    {
> >      // NOT ON!
> >       cout << "In: virtual void Display() const = 0..." << endl;
> >    }
> >
> > Is this correct?
> >
>
> [ Adding comp.std.c++ to followup groups ]
>
> I thought this was illegal behaviour. In 10.4/2 of the standard, there
> is a note, saying:
>
> "A function declaration cannot provide both a pure specifier and a
> definition".
>
> So, according to that, the above definition will be illegal.
[snip]
> But, mysteriously, VC++ accepted this code.
You are right, VC is wrong.  It does not even emit a warning at the highest
warning level.

> Can someone clarify whether this is legal or not?
Definitely not legal.

--
Jim

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/17
Raw View
Paul Grealish wrote:

> Biju Thomas wrote:
...
> > I thought this was illegal behaviour. In 10.4/2 of the standard, there
> > is a note, saying:
> >
> > "A function declaration cannot provide both a pure specifier and a
> > definition".

> So there is.  And there's even an example:
>         struct C {
>             virtual void f() { }=0; // ill-formed
>         };

> But, of course the 'ill-formed' line should read:
>
>             virtual void f() =0 { }

> Maybe this has been corrected in the Standard proper?

That note merely summarizes a fact which can be derived from the grammar
given in section 9.2. Specifically, a member-declaration can be a
function-definition, or a member-declarator-list. A pure-specifier can
only occur within a member-declarator, which is not part of a
function-definition. Your 'corrected' form is equally invalid.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]