Pure Virtual Function declaration

S

srivant.samir

What is the difference between two forms of following virtual functions :-

virtual void foo const =0;

virtual void foo {}
 
R

Richard Damon

What is the difference between two forms of following virtual functions :-

virtual void foo const =0;

virtual void foo {}

first, neither is a function since there is no parameter list with
either of them, not even an empty ().


assuming you mean between:

virtual void foo() const =0;
virtual void foo() {}

the first defines a function, that
1) promises that it, and all version that over ride it, will not change
the object it is called on (except for mutable members),and

2) must be over-ridden by a deriving class (and thus no object of this
class can exist as full object, as there must be a deriving class.


The second form just defines a function that does nothing, it CAN be
over-ridden is a derived class, but it doesn't need to be. That
over-ridding version is also allowed to change the object it is called
on. an object of this class might be able to be defined (assuming
nothing else prohibits it).
 
S

Saeed Amrollahi

What is the difference between two forms of following virtual functions :-



virtual void foo const =0;



virtual void foo {}

Hi

At first I assume you meant:
virtual void foo1() =0;
virtual void foo2();
for more clarity, I use foo1 and foo2.
Also, please note at the moment the const
qualifier doesn't affect on our discussion
Both are virtual functions, and
virtual means: may be redefined later in a class derived
from this one.
More precisely the first one is "pure" virtual function.
Using pure virtual function, we can make an abstract
class like this:
class C1 {
public:
virtual void foo1() =0;
};
Please note you "can" define foo1 but
you can't create an object of C1, because C1
has one pure virtual function. You can derive
a class like D from C1 and -re-define foo1:
class D : public C1 {
public:
void foo1() {/* ... */ }
};
foo2 isn't pure virtual. It's just virtual:
class C2 {
virtual void foo2() { /* ... */ }
};
You "have to" define foo2 at class C1 because it's a C++ rule:
A virtual function must be defined in the class in which
they are first declared.
Also a derived class like D can -re-define foo2.
It's obvious C2 isn't abstract and you can make object of
that.

HTH,
-- Saeed Amrollahi Boyouki
 
A

admin.tosite

What is the difference between two forms of following virtual functions :-



virtual void foo const =0;



virtual void foo {}


First one is Pure virtual functions which is nothing but definition is there in Derived classes.

Second one is Virtual functions i.r run time polymorphisam

for more information
http://ccppcoding.blogspot.in/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top