473,785 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"C++ with Interfaces" (article in CUJ vol 22 no 9)

Interesting article. However, it starts with an example which I find
rather misleading. I hope the author (Christopher Diggins) wouldn't
mind if I post a quote from the article. I know he probably reads or
even writes to c.l.c++.m sometimes.

Christopher gives this [pseudo-]code (yes, it's not C++):

interface IFuBar {
contract:
void FuBar();
};

struct BaseFuBar {
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() {
DerivedFuBar d;
BaseFuBar& b = d;
IFuBar i = b;
i.FuBar(); // outputs BaseFuBar
}

and then claims that "the common way to approximate this design
in standard C++ is:"

struct AbcFuBar {
virtual void FuBar() = 0;
};

// vb: missing #include <iostream>

struct BaseFuBar : public AbcFuBar { // vb: public is unnecessary
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}

Well, simple superfluosity of 'public', the missing <iostream>, and the
absense of 'main' return value type aside, the code seems logical, yes?

I submit that by changing a single character on the line marked with
asterisks, the desired behaviour can be _correctly_ implemented:

BaseFuBar b = d;

contrary to what the author implies.

Now, I am thinking: should I really read the rest of the article after
seeing that and

main() {
...

in a supposedly _standard_ C++ program?

Christopher is a developer of Heron, another language. I am wondering,
do we really need another language developed because somebody hasn't got
around to learning existing ones well enough? Nah, that can't be it...

Anyway...

Victor
Jul 22 '05 #1
17 1597
* Victor Bazarov:

struct AbcFuBar {
virtual void FuBar() = 0;
};

// vb: missing #include <iostream>

struct BaseFuBar : public AbcFuBar { // vb: public is unnecessary
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}

Well, simple superfluosity of 'public', the missing <iostream>, and the
absense of 'main' return value type aside, the code seems logical, yes?

I submit that by changing a single character on the line marked with
asterisks, the desired behaviour can be _correctly_ implemented:

BaseFuBar b = d;

contrary to what the author implies.


Haven't read the article (do you have a link?), but:

What is the desired behavior?

Judging from what you write it is to get a call of BaseFuBar::FuBa r?

That's easy to achieve as you've shown; there are also other ways.

Btw. you're posting in clc++, not clc++m... ;-)

--
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 22 '05 #2

"Alf P. Steinbach" wrote:
[...]
What is the desired behavior?


Virtual dispatch with Warp 9.9999 velocity and zero space overhead.

Lieutenant Commander Data is full of such things.

regards,
alexander.
Jul 22 '05 #3
Alf P. Steinbach wrote:
* Victor Bazarov:
struct AbcFuBar {
virtual void FuBar() = 0;
};

// vb: missing #include <iostream>

struct BaseFuBar : public AbcFuBar { // vb: public is unnecessary
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}

Well, simple superfluosity of 'public', the missing <iostream>, and the
absense of 'main' return value type aside, the code seems logical, yes?

I submit that by changing a single character on the line marked with
asterisks, the desired behaviour can be _correctly_ implemented:

BaseFuBar b = d;

contrary to what the author implies.

Haven't read the article (do you have a link?), but:


No, I don't have a link. I just have my copy of the magazine.
What is the desired behavior?
As I understood from the pseudo-code it's to make the BaseFuBar subobject
to do its thing, but not the DerivedFuBar.
Judging from what you write it is to get a call of BaseFuBar::FuBa r?
I thought so.
That's easy to achieve as you've shown; there are also other ways.
You should post your versions.
Btw. you're posting in clc++, not clc++m... ;-)


I know. I rarely post in c.l.c++.m. Too 'm' for my taste.

V
Jul 22 '05 #4
* Victor Bazarov:

main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}

What is the desired behavior?


As I understood from the pseudo-code it's to make the BaseFuBar subobject
to do its thing, but not the DerivedFuBar.
Judging from what you write it is to get a call of BaseFuBar::FuBa r?


I thought so.
That's easy to achieve as you've shown; there are also other ways.


You should post your versions.


Well one unsafe way to avoid copying is

a.BaseFubar::Fu Bar();

and one more safe way to capture the the essence of having two diffent
behaviors of same virtual function on same object is to provide an
interface- getter in BaseFuBar

AbcFuBar a& = b.theBaseAndBas eOnlyFuBarInter face();

which can be implemented e.g. on a contained object in BaseFuBar,

but I question the wisdom of the whole thing, because when the object
is really a DerivedFuBar then doing the BaseFuBar thing might break
the class invariant of DerivedFuBar (I think that's also the main reason
why public virtual functions are frowned on by purists).

Btw. you're posting in clc++, not clc++m... ;-)


I know. I rarely post in c.l.c++.m. Too 'm' for my taste.


I meant, are you sure the author of that article is reading _this_ group?

--
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 22 '05 #5
Alf P. Steinbach wrote:
* Victor Bazarov:
main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}
What is the desired behavior?
As I understood from the pseudo-code it's to make the BaseFuBar subobject
to do its thing, but not the DerivedFuBar.

Judging from what you write it is to get a call of BaseFuBar::FuBa r?


I thought so.

That's easy to achieve as you've shown; there are also other ways.


You should post your versions.

Well one unsafe way to avoid copying is

a.BaseFubar::Fu Bar();


Why is it unsafe?
and one more safe way to capture the the essence of having two diffent
behaviors of same virtual function on same object is to provide an
interface- getter in BaseFuBar

AbcFuBar a& = b.theBaseAndBas eOnlyFuBarInter face();

which can be implemented e.g. on a contained object in BaseFuBar,

but I question the wisdom of the whole thing, because when the object
is really a DerivedFuBar then doing the BaseFuBar thing might break
the class invariant of DerivedFuBar (I think that's also the main reason
why public virtual functions are frowned on by purists).


To be honest, I struggled to understand whey the author wanted the
"base" (intermediate) version of the function called in the first
place. That was completely unclear from the article. Perhaps it's
just the thing: I didn't get the fact that the whole "interface"
discussion is academical.

What's your take on it?
Btw. you're posting in clc++, not clc++m... ;-)


I know. I rarely post in c.l.c++.m. Too 'm' for my taste.

I meant, are you sure the author of that article is reading _this_ group?


No. Nor do I really care. I posted to express my opinion (possibly in
a too convoluted way), not to bring something to his attention. CUJ
publishes e-mail addresses of authors in case somebody wants to contact
them. I didn't. I am, on the other hand, interested in the opinions of
the community at large, especially considering that rarely topics from
c.l.c++.m trickle down to c.l.c++ so folks here participate.

Victor
Jul 22 '05 #6
* Victor Bazarov:
* Alf:

Well one unsafe way to avoid copying is

a.BaseFubar::Fu Bar();
Why is it unsafe?


First, should be 'b' not 'a' there.

It's unsafe because
when the object
is really a DerivedFuBar then doing the BaseFuBar thing might break
the class invariant of DerivedFuBar

e.g. DerivedFooBar might add an update of some cached information
which is assumed to always be in synch with the rest and valid.

I didn't get the fact that the whole "interface"
discussion is academical.

What's your take on it?


Don't know because I don't know what the discussion is... :-)

--
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 22 '05 #7
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:bE******** *******@newsrea d1.dllstx09.us. to.verio.net...
[snip]


Interfaces can be useful. Here's an example:

Suppose I have a system in which there are bunches of objects graphically
drawn on the screen (yes, I know that can't be done in STL, it's just an
example). Suppose I want to implement a drag and drop UI. I want some
objects to be able to be able to have other objects dropped on them, but I
want other objects not to have that functionality. Hence, I create an
interface:

class DropTargetable
{
public:
virtual bool canAcceptDrop(D rawnObj info) = 0;
virtual void acceptDrop(Draw nObj info) = 0;
}

Methods that dealt with a dropping would simply call canAcceptDrop on
whatever they were trying to drop something on, and if it could, they would
call accept drop, where info would be whatever the user had clicked on.

That example is probably more practical than looking at a FooBar example.

Yours,

James

PS, if I missed the point, sorry. =) This is just what I understood to be
your question about interfaces.
Jul 22 '05 #8

Victor Bazarov wrote:
[...]
I meant, are you sure the author of that article is reading _this_ group?


No. Nor do I really care. I posted to express my opinion (possibly in
a too convoluted way), not to bring something to his attention. CUJ
publishes e-mail addresses of authors in case somebody wants to contact
them. I didn't. I am, on the other hand, interested in the opinions of
the community at large, especially considering that rarely topics from
c.l.c++.m trickle down to c.l.c++ so folks here participate.


Bazarov, Bazarov. Community at large really loved the idea. Tons
of kudos.

http://google.com/groups?threadm=qVi....videotron.net
(Subject: C++ with interfaces)

regards,
alexander.
Jul 22 '05 #9
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:bE******** *******@newsrea d1.dllstx09.us. to.verio.net...
Interesting article. However, it starts with an example which I find
rather misleading. I hope the author (Christopher Diggins) wouldn't
mind if I post a quote from the article. I know he probably reads or
even writes to c.l.c++.m sometimes.

Christopher gives this [pseudo-]code (yes, it's not C++):

interface IFuBar {
contract:
void FuBar();
};

struct BaseFuBar {
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() {
DerivedFuBar d;
BaseFuBar& b = d;
IFuBar i = b;
i.FuBar(); // outputs BaseFuBar
}

and then claims that "the common way to approximate this design
in standard C++ is:"

struct AbcFuBar {
virtual void FuBar() = 0;
};

// vb: missing #include <iostream>

struct BaseFuBar : public AbcFuBar { // vb: public is unnecessary
void FuBar() { std::cout << "BaseFuBar" ; }
};

struct DerivedFuBar : public BaseFuBar {
void FuBar() { std::cout << "DerivedFuB ar"; }
};

main() { // vb: where is 'int'?
DerivedFuBar d;
BaseFuBar& b = d; /// *************** *********
AbcFuBar& a = b;
a.FuBar(); // output DerivedFuBar
}

Well, simple superfluosity of 'public', the missing <iostream>, and the
absense of 'main' return value type aside, the code seems logical, yes?

I submit that by changing a single character on the line marked with
asterisks, the desired behaviour can be _correctly_ implemented:

BaseFuBar b = d;

contrary to what the author implies.


After reading up a little bit on these interfaces, I think I understand what
the author was trying to point out. While it is true that you _can_ get
the same output as the code that uses interfaces, you have to create another
instance of AbcFuBar to do it. IFuBar, on the other hand, does not perform
any slicing and is operating on the DerivedFooBar instance created in the
main function.

Also, if I understand correctly, the following code will display
"DerivedFuB ar" without slicing as well:

IFuBar i2 = d;
i2.FuBar(); // outputs DerivedFuBar

Under the covers, the interface is maintaining a pointer to the object and
invoking the FuBar method. This means that the interface is usable by any
class that implements the required methods, not just those that choose to
inherit from it, and it can invoke methods on objects without using the
standard dynamic dispatching mechanisms used by the second example. It also
means that the same interface could invoke base class functions or derived
class functions on the same object, if they both have the same name and are
not virtual, which is something that you can't do with virtual methods in
ABCs.

I don't know if this functionality is necessary, but it sounds neat.

--
David Hilsee
Jul 22 '05 #10

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

Similar topics

4
12086
by: David List | last post by:
I'm working on a analyzation/design project that is supposed to uncover the intricacies and implications in entering the market for systems used by public administrations here in Denmark. XML and its use is a very important part of constructing such a system, since it is advised and specified from higher levels that systems of this kind should make use of XML as basis for exchange of documents. In the documents and specifications...
77
5390
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
6
2953
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that will employ a software "Plug-In" architecture. Taking the plug-in route will give us a design that can adapt to, as yet, undefined future requirements (within the scope of the plug-in interface spec of course). In the past I've done this with...
7
3608
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
3
1550
by: Peter K | last post by:
Hi I am having trouble writing a webservice which is to provide access to the functions of an existing codebase. The existing code may not be changed at all. For example, I obtain an IDataProvider from a factory. IDataProvider has a method "GetCustomer()" which returns an ICustomer object (ICustomer is an interface, and at the level of my code I do not know the concrete implementation).
8
1680
by: bob | last post by:
Hi, we're faced with a horrible scenario whereby we are not able to access exported classes in a DLL due to cross compiler issues. Traditional approaches such as using extern "C" and LoadLibrary (nothing to do with C++ I know but I'll get to my question... :). Anyways, we need to access our exported objects and the only safe route I know to get to the destination dll/lib from our client (not in same environment) is by using extern "C"...
5
2401
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how many heavy duty pro programmers in this newsgroup have actually used a delegate in their code, say in the last year of coding. By "use" I mean constructed a delegate and using it in an Event Source class and an Event Receiver class. Interfaces...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10096
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
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
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.