473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 2056
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.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.