Prevent hiding -- add an overload instead.

  • Thread starter Frederick Gotham
  • Start date
F

Frederick Gotham

Is there any way to prevent hiding of a base class function? I want the
derived class to add an overload of the function, rather than hide the
base class one. The following snippet demonstrates what I'm after:

class Type1 {};
class Type2 {};

class Base {
public:

void Func( Type1 )
{
;
}

};


class Derived : public Base {
public:

void Func( Type2 )
{
;
}

};

int main()
{
Derived derived_obj;

Type1 type1_obj;



derived_obj.Func(type1_obj); /* Any way to make this work? */


/* Instead of having to do: */


static_cast<Base&>(derived_obj).Func( type1_obj );
}
 
F

Frederick Gotham

Frederick Gotham posted:

Is there any way to prevent hiding of a base class function? I want the
derived class to add an overload of the function, rather than hide the
base class one.


It's OK, I figured it out.


class Type1 {};
class Type2 {};

class Base {
public:

void Func( Type1 )
{
;
}

};


class Derived : public Base {
public:

using Base::Func;

void Func( Type2 )
{
;
}

};

int main()
{
Derived derived_obj;

Type1 type1_obj;

Type2 type2_obj;

derived_obj.Func(type1_obj);

derived_obj.Func(type2_obj);
}
 
B

Bo Persson

Frederick Gotham said:
Is there any way to prevent hiding of a base class function? I want
the
derived class to add an overload of the function, rather than hide
the
base class one. The following snippet demonstrates what I'm after:

class Type1 {};
class Type2 {};

class Base {
public:

void Func( Type1 )
{
;
}

};


class Derived : public Base {
public:

using Base::Func; // Now it is visible here!
void Func( Type2 ) // and an overload added
{
;
}

};


Bo Persson
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top