By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

pure member functions

P: n/a
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?
Dec 18 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Dec 18, 11:52 pm, Rahul <sam_...@yahoo.co.inwrote:
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.
Dec 18 '07 #2

P: n/a
On 2007-12-18 13:52:49 -0500, Rahul <sa*****@yahoo.co.insaid:
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

Dec 18 '07 #3

P: n/a
On Dec 18, 11:56 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Dec 18, 11:52 pm, Rahul <sam_...@yahoo.co.inwrote:
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...
Dec 18 '07 #4

P: n/a
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
ta*******@rocketmail.com
Dec 18 '07 #5

P: n/a
On 2007-12-18 20:02, Rahul wrote:
On Dec 18, 11:56 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
>On Dec 18, 11:52 pm, Rahul <sam_...@yahoo.co.inwrote:
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
Dec 18 '07 #6

P: n/a
On Dec 19, 12:00 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-12-18 13:52:49 -0500, Rahul <sam_...@yahoo.co.insaid:
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...
}
Dec 19 '07 #7

P: n/a
On Dec 19, 8:31 am, Rahul <sam_...@yahoo.co.inwrote:
On Dec 19, 12:00 am, Kira Yamato <kira...@earthlink.netwrote:


On 2007-12-18 13:52:49 -0500, Rahul <sam_...@yahoo.co.insaid:
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!
Dec 19 '07 #8

P: n/a
On 2007-12-18 22:31:02 -0500, Rahul <sa*****@yahoo.co.insaid:
On Dec 19, 12:00 am, Kira Yamato <kira...@earthlink.netwrote:
>On 2007-12-18 13:52:49 -0500, Rahul <sam_...@yahoo.co.insaid:
>>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

Dec 19 '07 #9

P: n/a
On Dec 18, 7:52 pm, Rahul <sam_...@yahoo.co.inwrote:
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 19 '07 #10

P: n/a
On 2007-12-18 22:31:02 -0500, Rahul <sa*****@yahoo.co.insaid:
On Dec 19, 12:00 am, Kira Yamato <kira...@earthlink.netwrote:
>On 2007-12-18 13:52:49 -0500, Rahul <sam_...@yahoo.co.insaid:

>>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.

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...
}
If A was an abstract class, you wouldn't be able to call fun at all,
because you wouldn't be able to create an A object for its argument.

A obj; // illegal: A is abstract
fun(obj); // can't get here

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 19 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.