472,804 Members | 735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

pure member functions

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
10 2011
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
10
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
2
by: Rahul | last post by:
Hi Everyone, It seems that pure virtual member functions can be defined in the abstract base class, but what is the need (or) use to do so? and how is it done? Thanks in advance!!!
13
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.