473,735 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pure virttual function

sks

Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

Jul 5 '06 #1
21 2571

sks wrote:
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
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

Jul 5 '06 #2
"sks" <es******@gmail .comwrote in message
news:11******** **************@ l70g2000cwa.goo glegroups.com.. .
>
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
Yes, Herb Sutter can:

http://www.gotw.ca/gotw/031.htm

Hope this helps :)
Stu
Jul 5 '06 #3
sks
Murali Krishna wrote:
sks wrote:
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

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

Jul 5 '06 #4
* Murali Krishna:
*sks:
>could anyone explain me why definition to a pure virtual function
is allowed ?

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.

--
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?
Jul 5 '06 #5
TB
Murali Krishna skrev:
sks wrote:
>Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

May be you are asking why it is not allowed.
No he isn't.

class A {
public:
virtual void foo() = 0;
};

void A::foo() { }

class C : public A {
public:
void foo() {
A::foo();
}
};

int main(int argc, char* argv[])
{
C c;
c.foo();
return 0;
}
--
TB @ SWEDEN
Jul 5 '06 #6
sks

Stuart Golodetz wrote:
"sks" <es******@gmail .comwrote in message
news:11******** **************@ l70g2000cwa.goo glegroups.com.. .

Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

Yes, Herb Sutter can:

http://www.gotw.ca/gotw/031.htm

Hope this helps :)
Stu
Thanks stuart . I have got answers to the question .

Jul 5 '06 #7
* Stuart Golodetz:
"sks" <es******@gmail .comwrote in message
news:11******** **************@ l70g2000cwa.goo glegroups.com.. .
>Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

Yes, Herb Sutter can:

http://www.gotw.ca/gotw/031.htm

Hope this helps :)
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?
Jul 5 '06 #8
"Alf P. Steinbach" <al***@start.no wrote in message
news:4h******** *****@individua l.net...
>* Stuart Golodetz:
>"sks" <es******@gmail .comwrote in message
news:11******* *************** @l70g2000cwa.go oglegroups.com. ..
>>Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?

Yes, Herb Sutter can:

http://www.gotw.ca/gotw/031.htm

Hope this helps :)

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?
Hmm, my bad it would seem. Reading it again, I noticed that #2 isn't
entirely right either; it says "B::f(i)" when B::f doesn't take any
parameters. Oh well, it seemed like a good page to direct the OP to at the
time...

Apologies,
Stu
Jul 5 '06 #9
sks wrote:
Murali Krishna wrote:
sks wrote:
Hi ,
could anyone explain me why definition to a pure virtual function
is allowed ?
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

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?
This is the first time I am seeing this and I could not believe it is
compiling. I don't know when will I open my eyes.

Murali.. come on wakeup!!

thanks for that sks.

-- Murali Krishna

Jul 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4367
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 it inline. Like this.
6
3488
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
10
4802
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a pure virtual function body
2
3324
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
10
2094
by: Rahul | last post by:
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.
14
2752
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
8784
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9462
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4558
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2187
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.