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

public base class

P: n/a
Hi all,

If I recall correctly, the functions inherited from a base class are
private in the derived class.

I want to expose some of them (but not all of them) directly through my
derived class. In other words, I'm trying to avoid something like:

void PublicFunction_DerrivedClass()
{
PublicFunction_BaseClass();
}

I would appreciate any help.
--

Best wishes,
Allen

No SPAM in my email !!
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ET2 Ng wrote:
Hi all,

If I recall correctly, the functions inherited from a base class are
private in the derived class.

I want to expose some of them (but not all of them) directly through
my
derived class. In other words, I'm trying to avoid something like:

void PublicFunction_DerrivedClass()
{
PublicFunction_BaseClass();
}

I would appreciate any help.


That all depends on what kind of inheritance you use. The default for
classes is private inheritance, and the default for structs is public
inheritance.
To use public inheritance for classes, do something like this:

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

int main(){
B b;
b.foo();
}

--
Where am I going, and what am I doing in this handbasket?

Jul 19 '05 #2

P: n/a
Hi John, Jacques, all,

Aha! Exactly what I was looking for! Thanks a bunch; I knew there had
to be a way to do that!
--

Best wishes,
Allen

No SPAM in my email !!

"John Carson" <do***********@datafast.net.au> wrote in message
news:3f******@usenet.per.paradox.net.au...
"ET2 Ng" <al************@att.woo-haa.net> wrote in message
news:NN*********************@bgtnsc05-news.ops.worldnet.att.net
Hi all,

If I recall correctly, the functions inherited from a base class
are private in the derived class.


As Jacques says, this depends on the type of inheritance. Private
inheritance is the default for classes but you can specify public or
protected.
I want to expose some of them (but not all of them) directly
through my derived class. In other words, I'm trying to avoid
something like:

void PublicFunction_DerrivedClass()
{
PublicFunction_BaseClass();
}

I would appreciate any help.
--

Best wishes,
Allen

No SPAM in my email !!


Rather than redefine functions, you can utilise "using" declarations. Use
private inheritance and then make a using declaration for every function
that you want to bring from private access to public access, e.g.,

class Base
{
public:
void print5()
{
std::cout << 5 << '\n';
}
void print10()
{
std::cout << 10 << '\n';
}
};

// private inheritance so print5 and print1 are private
class Derived : Base
{
public:
using Base::print5; // make print5 public
};
int main()
{
Derived d;
d.print5(); // OK
d.print10(); // error
return 0;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.