Name hiding from inherited classes
Posted
by
Mercerbearman
on Stack Overflow
See other posts from Stack Overflow
or by Mercerbearman
Published on 2013-10-29T03:48:56Z
Indexed on
2013/10/29
3:53 UTC
Read the original article
Hit count: 98
c++
I have the following sample code and I wanted to know the correct way to get access to the Pass method in the CBar class. Currently I have found 3 ways to get access to this method and they are as follows:
- Casting the object, ((CBar *) &foo)->Pass(1, 2, 3);
- Using this syntax, foo.CBar::Pass(1,2,3);
- Use the "using" syntax in the CFoo class declaration, using CBar::Pass.
The following is an example of a simple project to test this capability.
Foo.h
#include "bar.h"
class CFoo :
public CBar
{
private:
double m_a;
double m_b;
public:
CFoo(void);
~CFoo(void);
void Pass(double a, double b);
};
Foo.cpp
#include "Foo.h"
CFoo::CFoo(void)
{
m_a = 0.0;
m_b = 0.0;
}
CFoo::~CFoo(void)
{
}
void CFoo::Pass(double a, double b)
{
m_a = a;
m_b = b;
}
Bar.h
class CBar
{
int m_x;
int m_y;
int m_z;
public:
CBar(void);
~CBar(void);
void Pass(int x, int y, int z);
};
Bar.cpp
#include "Bar.h"
CBar::CBar(void)
{
m_x = 0;
m_y = 0;
m_z = 0;
}
CBar::~CBar(void)
{
}
void CBar::Pass(int x, int y, int z)
{
m_x = x;
m_y = y;
m_z = z;
}
And my main class DoStuff.cpp
#include "DoStuff.h"
#include "Foo.h"
CDoStuff::CDoStuff(void)
{
}
CDoStuff::~CDoStuff(void)
{
}
int main()
{
CFoo foo, foo1, foo2;
//This only gets to the Pass method in Foo.
foo.Pass(2.5, 3.5);
//Gets access to Pass method in Bar.
foo1.CBar::Pass(5,10,15);
//Can also case and access by location for the same result??
((CBar *) &foo2)->Pass(100,200,300);
return 0;
}
Are each of these options viable? Are some preferred? Are there pitfalls with using any one of the methods listed?
I am especially curious about the foo.CBar::Pass(1,2,3) syntax.
Thanks,
B
© Stack Overflow or respective owner