Hi there,
When creating interfaces and implementations , the usual thing is doing
somethign like
class Interface {
public:
virtual void f() = 0;
virtual void g() = 0;
};
class Imp1 : public Interface {
public:
void f();
void g();
};
So we can have Interface* if = new Imp1; and so on..
But we could also use private inheritance:
class Imp1 {
protected:
void f();
void g();
};
class Interface : private Imp1 {
public:
void f() { Imp1::f(); }
...
};
What are the advantages/disadvantages of each? I believe in both cases
a level of indirection happens (through the virtual table or calling
the base class function in the second case). But probably today
compilers do some optimizations?
Any advise?
Thanks!
protected:
void f(); 4 1627
On Jun 28, 4:36*pm, sip.addr...@gma il.com wrote:
[...]
But we could also use private inheritance:
class Imp1 {
protected:
* void f();
* void g();
};
class Interface : private Imp1 {
public:
* void f() { Imp1::f(); }
* ...
};
What are the advantages/disadvantages of each?
Normally, interfaces have more than one implementation as in Cat and
Dog inheriting from the Animal interface. So your private
implementation inheritance wouldn't work.
I believe in both cases
a level of indirection happens (through the virtual table or calling
the base class function in the second case). But probably today
compilers do some optimizations?
Sometimes, when the actual type of the object is known at compile time
I guess. Otherwise, in general the actual type is not known without
the use of virtual function table.
At a recent ACCU Silicon Valley talk, Ulrich Drepper mentioned how
indirect calls like C++'s virtual functions hurt the CPU's caching
schemes. Something to keep in mind, but I am not getting rid of my
virtual functions yet... :)
Ali si*********@gma il.com wrote:
When creating interfaces and implementations , the usual thing is doing
somethign like
class Interface {
public:
virtual void f() = 0;
virtual void g() = 0;
};
class Imp1 : public Interface {
public:
void f();
void g();
};
So we can have Interface* if = new Imp1; and so on..
But we could also use private inheritance:
class Imp1 {
protected:
void f();
void g();
};
class Interface : private Imp1 {
public:
void f() { Imp1::f(); }
...
};
Note, the above is effectively the same as composition.
class Imp1 {
public:
void f();
void g();
};
class Interface {
Imp1 imp;
public:
void f() { imp.f(); }
};
What are the advantages/disadvantages of each?
The old "inheritanc e versus composition" question. This isn't strictly a
C++ question. Do a Google search on that term and you will get a
plethora of opinions.
Any advise?
"Favor object composition over class inheritance" (GoF)
On Jun 29, 7:16*am, "Daniel T." <danie...@earth link.netwrote:
sip.addr...@gma il.com wrote:
[...]
But we could also use private inheritance:
class Imp1 {
protected:
* void f();
* void g();
};
class Interface : private Imp1 {
public:
* void f() { Imp1::f(); }
* ...
};
Note, the above is effectively the same as composition.
class Imp1 {
public:
* *void f();
* *void g();
};
class Interface {
* *Imp1 imp;
public:
* *void f() { imp.f(); }
};
What are the advantages/disadvantages of each?
The old "inheritanc e versus composition" question.
Not in this case though. It would be that question if the OP swapped
the class names; so to get to your example:
class Impl {
Interface interface;
public:
void f() { interface.f(); }
};
where Interface would not be an interface anymore.
This isn't strictly a
C++ question. Do a Google search on that term and you will get a
plethora of opinions.
Any advise?
"Favor object composition over class inheritance" (GoF)
Makes sense, but not in this question because there is an interface
that the OP is talking about. For the other types of class inheritance
like "a car HAS-AN engine", yes, that's composition.
Ali ac******@gmail. com wrote:
"Daniel T." <danie...@earth link.netwrote:
sip.addr...@gma il.com wrote:
But we could also use private inheritance:
class Imp1 {
protected:
* void f();
* void g();
};
class Interface : private Imp1 {
public:
* void f() { Imp1::f(); }
* ...
};
Note, the above is effectively the same as composition.
class Imp1 {
public:
* *void f();
* *void g();
};
class Interface {
* *Imp1 imp;
public:
* *void f() { imp.f(); }
};
What are the advantages/disadvantages of each?
The old "inheritanc e versus composition" question.
Not in this case though.
Yes, even in this case. Don't get too caught up in the names the OP
happened to choose.
class A { };
class B : private A { };
is effectively the same as:
class A { };
class B { A a; }
The only case where it is different is if class B overrides some virtual
member-function of A that is called internally within A. Like this:
class A {
public:
void foo() { bar(); }
virtual void bar(); /* can be pure virtual */
};
class B : private A {
void foo() { A::foo(); }
virtual void bar();
};
The above is *not* equivalent to composition. Rather, it is equivalent
to delegation. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lloyd Sheen |
last post by:
I have the following in one form:
Public Class ExeScript
Inherits System.Windows.Forms.UserControl
Implements ToadInterfaces.Interfaces.GotFocus,
ToadInterfaces.Interfaces.HasConfiguration, ToadInterfaces.Interfaces.UCForm
and the implementation is
Public ReadOnly Property SBToolBar() As TD.SandBar.ToolBar Implements
|
by: Geoff Pennington |
last post by:
Is there an easy way to find out what classes implement a given interface?
For example, it might be useful to know what classes implement ICollectible.
Much obliged,
Geoff.
|
by: lester |
last post by:
a pre-beginner's question:
what is the pros and cons of .net, compared to ++
I am wondering what can I get if I continue to learn C# after I have learned
C --> C++ --> C# ??
I think there must be many know the answer here.
thanks
|
by: John Underwood |
last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll
paste below.. I'm not grasping what you would gain by using a
interface, does any one have a brief description of their benefit?
Thanks,
John Underwood
|
by: tshad |
last post by:
I am trying to understand why I would use interfaces.
In the following example for IPrinciple, I have the following code:
************************************************************
using System;
using System.Collections;
using System.Security;
using System.Security.Principal;
| |
by: denis |
last post by:
Hi All,
I'm making a class which derives(implements) from 5 interfaces:
class myClass: public base1, public base2, public base3,.......
I want to be able to ask from any interface about a pointer to another
interface
What I mean is:
|
by: Larry |
last post by:
I checked definition of class CollectionBase
public abstract class CollectionBase : IList, ICollection,
IEnumerable, it implements 3 interface IList, ICollection and
IEnumerable.
I found IList is derived or extended from ICollection and
IEnumerable. Why the definition of class CollectionBase just implement
IList interface which has functions of ICollection and IEnumerable?
|
by: brianlanning |
last post by:
I have a class that represents a business object. Currently, this
class exists as a vb6 "class" which will be ported to .net. The plan
is to separate this out into a .net object residing in a web service,
then replace the vb6 class with a different vb6 class which is just a
proxy that calls the web service through a COM wrapper, then back to
the original vb6 class through interop (don't ask). For now,
everything will just pass through. ...
|
by: parez |
last post by:
Layer A is on top on Layer B.
Layer A talks to layer B using the interfaces defined in B. For input
and return values.
My question is should i take out the interfaces from B and put it in a
separate project?
|
by: S_K |
last post by:
Hi,
I've been toying around with interfaces in C#.
They are fun but can anybody give me some examples of where interfaces
are used
and what they are used for?
Thanks so much.
Steve
|
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,...
| |
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |