S
sks
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
could anyone explain me why definition to a pure virtual function
is allowed ?
sks said:Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
sks said:Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
Murali said:May be you are asking why it is not allowed.
See FAQ. it is better explained there.
http://www.parashift.com/c++-faq-lite/
-- Murali Krishna
*sks:
May be you are asking why it is not allowed.
May be you are asking why it is not allowed.
Stuart said:
Alf P. Steinbach said:* Stuart Golodetz:
Unfortunately reason #3 in that text is, if not exactly bu*****t, not
something one can rely on, and is with most compiler a technique that
/does not work/. Where it works -- if such a compiler exists
nowadays -- it's because Undefined Behavior can be anything, including
that a virtual call to a pure virtual function might end up in that pure
virtual function's definition. But if I could (unfortunately I can't) I'd
light a huge bonfire and throw that advice on top.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
sks said:No , My question was why defintion to pure virtual is allowed in the
base class eg this way
class ABC
{
public:
virtual f1()=0;
{
std::cout<<"Hi";
}
}
You may derive this class and provide your own implementation to f1()
but what is the use in allowing the definition in base class?
Alf said:* Murali Krishna:*sks:
May be you are asking why it is not allowed.
Sorry, the OP is correct that you can provide a definition for a pure
virtual function. But that definition can't be provided in the class
definition. As to the why of that, I don't know any good reason, and
that's better asked in [comp.std.c++].
One use for a defined pure virtual function is a "marker interface" like
struct Serializable
{
inline virtual ~Serializable() = 0;
};
inline Serializable::~Serializable() {}
Here a definition is necessary because the destructor will be called
(although it's never called virtually), and the destructor is the only
member function that for this class can be used to make it abstract.
Alf said:* Murali Krishna:Sorry, the OP is correct that you can provide a definition for a pure*sks:
could anyone explain me why definition to a pure virtual function
is allowed ?
May be you are asking why it is not allowed.
virtual function. But that definition can't be provided in the class
definition. As to the why of that, I don't know any good reason, and
that's better asked in [comp.std.c++].
One use for a defined pure virtual function is a "marker interface" like
struct Serializable
{
inline virtual ~Serializable() = 0;
};
inline Serializable::~Serializable() {}
Here a definition is necessary because the destructor will be called
(although it's never called virtually), and the destructor is the only
member function that for this class can be used to make it abstract.
Well, if no polymorphism is needed, but the class shouldn't be
instantiatable, one can always make the destructor protected.
Alf said:Sorry, the OP is correct that you can provide a definition for a pure
virtual function. But that definition can't be provided in the class
definition. As to the why of that, I don't know any good reason, and
that's better asked in [comp.std.c++].
Earl said:Sorry, the OP is correct that you can provide a definition for a pure
virtual function. But that definition can't be provided in the class
definition. As to the why of that, I don't know any good reason, and
that's better asked in [comp.std.c++].
because the compiler cannot understand the = 0 followed by an opening
brace?
(Maybe too hard for compiler vendors).
sks said:Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
sks said:could anyone explain me why definition to a pure virtual function
is allowed ?
Static (non-virtual) call method is used when the function is invoked: 1) by a
qualified name, 2) with an actual object (not a pointer or reference) 3) when
base class destructor is implicitly called from derived class destructor.
Alf said:Accepting this terminology,
point (2) does not apply to a call to a pure
virtual function from a non-static member function of the class (where
you can very easily have UB if that member function is directly or
indirectly called from a constructor of the class),
and it does not
apply to a call on a named variable of the class, since no such variable
can exist if the class has a pure virtual function;
hence it doesn't
seem to apply to anything in this context, and for the case of a call of
function that's not pure virtual, it's an implementation detail that
only affects the efficiency of a call, if anything, so at the C++ level
it doesn't make much sense to say if that call is virtual or not.
sks said:Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
If you are referring to the above use of terms "static" and "dynamic", I agree
that they are a bit ambiguous, since similar terms are used elsewhere in the
language, where they mean something completely different. However, this is not
entirely my invention. This terminology is derived directly from the one used in
the FAQ, see
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.2
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.3
I'd say that this is more relevant to point (1), i.e. what I referred to as
"call by a qualified name".
That would be point (2), yes.
I don't understand what exactly you are referring to by the "implementation
detail" part, since I don't see anything in my post that would be an
implementation detail.
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.