Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pure member functions

Reply
Thread Tools

pure member functions

 
 
Rahul
Guest
Posts: n/a
 
      12-18-2007
Hi,

I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...

I'm trying to think the reason behind this restriction... i just want
to want a base class to be abstract so as to avoid object slicing into
the base type from derived type, and in my case base class doesn't
need to have a virtual function.

Is there any alternative? and what is the reason behind only virtual
member functions being pure?
 
Reply With Quote
 
 
 
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      12-18-2007
On Dec 18, 11:52 pm, Rahul <(E-Mail Removed)> wrote:
> Hi,
>
> I tried to create a abstract class by having a non-virtual member
> function as pure, but i got a compilation error saying "only virtual
> member functions can be pure"...
>
> I'm trying to think the reason behind this restriction... i just want
> to want a base class to be abstract so as to avoid object slicing into
> the base type from derived type, and in my case base class doesn't
> need to have a virtual function.
>
> Is there any alternative? and what is the reason behind only virtual
> member functions being pure?


If you don't have any virtual function, probably atleast you will have
a virtual destructor. Make it pure, provide an implementation. IF a
non-virtual function is pure - how would you override it? And if you
will not override it, what does it mean? Nothing. So, no use having
such members.
 
Reply With Quote
 
 
 
 
Kira Yamato
Guest
Posts: n/a
 
      12-18-2007
On 2007-12-18 13:52:49 -0500, Rahul <(E-Mail Removed)> said:

> Hi,
>
> I tried to create a abstract class by having a non-virtual member
> function as pure, but i got a compilation error saying "only virtual
> member functions can be pure"...
>
> I'm trying to think the reason behind this restriction... i just want
> to want a base class to be abstract so as to avoid object slicing into
> the base type from derived type, and in my case base class doesn't
> need to have a virtual function.


I don't understand your goal. What do you mean by "object slicing into
the base type from derived type?"

>
> Is there any alternative? and what is the reason behind only virtual
> member functions being pure?


--

-kira

 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      12-18-2007
On Dec 18, 11:56 pm, Abhishek Padmanabh <(E-Mail Removed)>
wrote:
> On Dec 18, 11:52 pm, Rahul <(E-Mail Removed)> wrote:
>
> > Hi,

>
> > I tried to create a abstract class by having a non-virtual member
> > function as pure, but i got a compilation error saying "only virtual
> > member functions can be pure"...

>
> > I'm trying to think the reason behind this restriction... i just want
> > to want a base class to be abstract so as to avoid object slicing into
> > the base type from derived type, and in my case base class doesn't
> > need to have a virtual function.

>
> > Is there any alternative? and what is the reason behind only virtual
> > member functions being pure?

>
> If you don't have any virtual function, probably atleast you will have
> a virtual destructor. Make it pure, provide an implementation. IF a
> non-virtual function is pure - how would you override it? And if you
> will not override it, what does it mean? Nothing. So, no use having
> such members.


Yes, you are probably referring to current standards and
implementation of c++,
however, when the standard was being developed, some one could have
thought of pure ordinary member functions and a mechanism to override
them too... for some reason they haven't done that... which is what i
was wondering about...
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      12-18-2007
Rahul wrote:
> Hi,
>
> I tried to create a abstract class by having a non-virtual member
> function as pure, but i got a compilation error saying "only virtual
> member functions can be pure"...
>
> I'm trying to think the reason behind this restriction... i just want
> to want a base class to be abstract so as to avoid object slicing into
> the base type from derived type, and in my case base class doesn't
> need to have a virtual function.
>
> Is there any alternative? and what is the reason behind only virtual
> member functions being pure?


If a member function is pure, it needs to be virtual so it can be
overridden. Otherwise a derived class can't override it and so a method of
the class can never be declared. Without being virtual the best that can be
done is name hiding which isn't quite the same as overriding.

--
Jim Langston
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      12-18-2007
On 2007-12-18 20:02, Rahul wrote:
> On Dec 18, 11:56 pm, Abhishek Padmanabh <(E-Mail Removed)>
> wrote:
>> On Dec 18, 11:52 pm, Rahul <(E-Mail Removed)> wrote:
>>
>> > Hi,

>>
>> > I tried to create a abstract class by having a non-virtual member
>> > function as pure, but i got a compilation error saying "only virtual
>> > member functions can be pure"...

>>
>> > I'm trying to think the reason behind this restriction... i just want
>> > to want a base class to be abstract so as to avoid object slicing into
>> > the base type from derived type, and in my case base class doesn't
>> > need to have a virtual function.

>>
>> > Is there any alternative? and what is the reason behind only virtual
>> > member functions being pure?

>>
>> If you don't have any virtual function, probably atleast you will have
>> a virtual destructor. Make it pure, provide an implementation. IF a
>> non-virtual function is pure - how would you override it? And if you
>> will not override it, what does it mean? Nothing. So, no use having
>> such members.

>
> Yes, you are probably referring to current standards and
> implementation of c++,
> however, when the standard was being developed, some one could have
> thought of pure ordinary member functions and a mechanism to override
> them too... for some reason they haven't done that... which is what i
> was wondering about...


The difference between a virtual and non-virtual function is that
virtual functions are designed to be overrideable, if you try to make a
non-virtual function overrideable you will end up with a virtual function.

There are three kinds of languages, those with no virtual functions
(they usually do not have much OO support either), those with both
virtual and non virtual functions (like C++) and those with only virtual
functions (Java).

--
Erik Wikström
 
Reply With Quote
 
Rahul
Guest
Posts: n/a
 
      12-19-2007
On Dec 19, 12:00 am, Kira Yamato <(E-Mail Removed)> wrote:
> On 2007-12-18 13:52:49 -0500, Rahul <(E-Mail Removed)> said:
>
> > Hi,

>
> > I tried to create a abstract class by having a non-virtual member
> > function as pure, but i got a compilation error saying "only virtual
> > member functions can be pure"...

>
> > I'm trying to think the reason behind this restriction... i just want
> > to want a base class to be abstract so as to avoid object slicing into
> > the base type from derived type, and in my case base class doesn't
> > need to have a virtual function.

>
> I don't understand your goal. What do you mean by "object slicing into
> the base type from derived type?"
>
>
>
> > Is there any alternative? and what is the reason behind only virtual
> > member functions being pure?

>
> --
>
> -kira


class A
{
};

class B : public A
{
};

fun(A obj)
{
}

int main()
{
B obj;
fun(obj); // Object of type B is upcasted into type of A and it
is sliced. If i had a pure virtual member function in A, this could
have been avoided, i would have basically got a compilation error...
}
 
Reply With Quote
 
Abhishek Padmanabh
Guest
Posts: n/a
 
      12-19-2007
On Dec 19, 8:31 am, Rahul <(E-Mail Removed)> wrote:
> On Dec 19, 12:00 am, Kira Yamato <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On 2007-12-18 13:52:49 -0500, Rahul <(E-Mail Removed)> said:

>
> > > Hi,

>
> > > I tried to create a abstract class by having a non-virtual member
> > > function as pure, but i got a compilation error saying "only virtual
> > > member functions can be pure"...

>
> > > I'm trying to think the reason behind this restriction... i just want
> > > to want a base class to be abstract so as to avoid object slicing into
> > > the base type from derived type, and in my case base class doesn't
> > > need to have a virtual function.

>
> > I don't understand your goal. What do you mean by "object slicing into
> > the base type from derived type?"

>
> > > Is there any alternative? and what is the reason behind only virtual
> > > member functions being pure?

>
> > --

>
> > -kira

>
> class A
> {
>
> };
>
> class B : public A
> {
>
> };
>
> fun(A obj)
> {
>
> }
>
> int main()
> {
> B obj;
> fun(obj); // Object of type B is upcasted into type of A and it
> is sliced. If i had a pure virtual member function in A, this could
> have been avoided, i would have basically got a compilation error...


You could simply mark the base copy constructor as protected member!
 
Reply With Quote
 
Kira Yamato
Guest
Posts: n/a
 
      12-19-2007
On 2007-12-18 22:31:02 -0500, Rahul <(E-Mail Removed)> said:

> On Dec 19, 12:00 am, Kira Yamato <(E-Mail Removed)> wrote:
>> On 2007-12-18 13:52:49 -0500, Rahul <(E-Mail Removed)> said:
>>
>>> Hi,

>>
>>> I tried to create a abstract class by having a non-virtual member
>>> function as pure, but i got a compilation error saying "only virtual
>>> member functions can be pure"...

>>
>>> I'm trying to think the reason behind this restriction... i just want
>>> to want a base class to be abstract so as to avoid object slicing into
>>> the base type from derived type, and in my case base class doesn't
>>> need to have a virtual function.

>>
>> I don't understand your goal. What do you mean by "object slicing into
>> the base type from derived type?"
>>
>>
>>
>>> Is there any alternative? and what is the reason behind only virtual
>>> member functions being pure?

>>
>> --
>>
>> -kira

>
> class A
> {
> };
>
> class B : public A
> {
> };
>
> fun(A obj)
> {
> }


True. It will avoid your case, but it won't avoid

fun(A &obj)

or

func(A *obj)
even if A is abstract.

>
> int main()
> {
> B obj;
> fun(obj); // Object of type B is upcasted into type of A and it
> is sliced. If i had a pure virtual member function in A, this could
> have been avoided, i would have basically got a compilation error...
> }


So, if you want to prevent using a derived class as its base class, you
can inherit as protected or private:

class B : protected A

or

class B : private A.

--

-kira

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-19-2007
On Dec 18, 7:52 pm, Rahul <(E-Mail Removed)> wrote:
> I tried to create a abstract class by having a non-virtual member
> function as pure, but i got a compilation error saying "only virtual
> member functions can be pure"...


> I'm trying to think the reason behind this restriction...


Maybe the fact that making a non-virtual function pure has no
sense. Making a function pure means that you must override it
in a derived class. Making a function non-virtual means that
the function cannot be overridden in a derived class. What
would combining the two mean (other than making the class
unusable)?

> i just want to want a base class to be abstract so as to avoid
> object slicing into the base type from derived type, and in my
> case base class doesn't need to have a virtual function.


If you are deriving from the class, it should either have a
virtual destructor, or the semantics of the base class are such
that it wouldn't occur to anyone to declare one (e.g. something
like std::iterator). If there is reason to worry about slicing,
you almost certainly need a virtual destructor as well.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
pure virtual member functions Rahul C++ 3 12-02-2007 06:57 AM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Pure functions still pure after definition Todd Aspeotis C++ 3 05-30-2005 03:53 AM



Advertisments