By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Disabling base class conversions?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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?

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

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

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* 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?

--
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 23 '05 #3

P: n/a

David wrote:
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-

Jul 23 '05 #4

P: n/a
* 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
* 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?


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();
}

--
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 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.