pure member functions

R

Rahul

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?
 
A

Abhishek Padmanabh

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

Kira Yamato

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?"
 
R

Rahul

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

Jim Langston

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

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

Erik Wikström

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

Rahul

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

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

Abhishek Padmanabh

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!
 
K

Kira Yamato

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

James Kanze

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

Pete Becker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top