Disabling base class conversions?

D

David

Hi,

I'm writing some code at the moment where I would like to make use of
inheritance from a base class to avoid lots of re-coding in various child
classes sharing very similar interfaces. However I wish to disallow the
ability for a user to refer these sub-classes using base class pointers.
I'm wondering if there are any available techniques in C++ which can allow
this? Appreciate any help you can provide.

Thanks,

David
 
A

Alf P. Steinbach

* David:
I'm writing some code at the moment where I would like to make use of
inheritance from a base class to avoid lots of re-coding in various child
classes sharing very similar interfaces. However I wish to disallow the
ability for a user to refer these sub-classes using base class pointers.
I'm wondering if there are any available techniques in C++ which can allow
this? Appreciate any help you can provide.

What's wrong with private inheritance?
 
D

David

Sorry I forgot to mention that I need these inherited interfaces needs to
publicly available. Is there still a way to do this?
 
S

Shezan Baig

David said:
Sorry I forgot to mention that I need these inherited interfaces needs to
publicly available. Is there still a way to do this?


Inherit privately and provide forwarding functions in your derived
class for the methods you want to expose.

Hope this helps,
-shez-
 
A

Alf P. Steinbach

* David:
[top-posting]
[quoting signature]
[using Outlook Express without OE Quotefix]

Do not top-post. See the FAQ. Corrected.

Do not quote the signature (unless you're commenting on it). See the
FAQ. Corrected.

Please do not use Outlook Express without OE QuoteFix. See
<url: http://home.in.tum.de/~jain/software/oe-quotefix/>. Correct... ;-)


* David:
* Alf P. Steinbach

Sorry I forgot to mention that I need these inherited interfaces needs to
publicly available. Is there still a way to do this?

Compiles with MSVC 7.1 (warning: dominance), GNU g++ 3.4.2 and Comeau Online,
but I'm still not 101% sure of the legality. You might also want to consider
efficiency (probably not worse than with forwarding, but measure if in doubt),
and the fact that virtual inheritance limits your options wrt. construction.
If the below therefore isn't Good Medicine, use manual forwarding.


#include <iostream> // std::cout
#include <ostream> // <<, std::endl

class IFish
{
public:
virtual void swim() = 0;
};

class DefaultFish: public virtual IFish
{
public:
virtual void swim()
{
std::cout << "Swoosh! Swoosh!" << std::endl;
}
};

class Cod: public virtual IFish, private virtual DefaultFish
{
public:
using DefaultFish::swim;
};

int main()
{
Cod fish;
IFish& fish2 = fish;

fish.swim();
fish2.swim();
}
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,651
Latest member
VeraPiw932

Latest Threads

Top