On Oct 18, 8:41 pm, Christopher <cp...@austin.rr.com> wrote:
I am surprised this hasn't come up for me more in the past, but the
situation is:
I need to have an interface that is usable for all
I need to have an interface that is only usable for some
I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?
Example:
I am making a texture class for my graphics library
The only thing the application should have access to is a string
filename to create it with, perhaps dimensions, and a few other
things.
However, some classes inside the engine, such as say the renderer
should have access to the hardware level texture resource, which is in
this case a pointer to a struct from a 3rd party library. So, I need
accessors for my renderer to use, but do not want them to be available
to the application. Both are using the same class.
Provide access(...) using interfaces only
We need a dummy Resource,
a core Texture type (string member and a pointer)
application needs an interface: IApplication
renderer needs another interface: IRenderer
type Texture overloads access(...) based on interface type.
#include <iostream>
struct Resource
{
};
struct IApplication
{
virtual void set(const std::string&) = 0;
};
struct IRenderer
{
virtual void set(Resource* p) = 0;
};
class Texture
{
const std::string m_s;
Resource* p_res;
public:
Texture(std::string s) : m_s(s), p_res(0) { }
void access(IApplication& ia) const
{
ia.set(m_s);
}
void access(IRenderer& ir) const
{
ir.set(p_res);
}
void setp(Resource* p)
{
p_res = p;
}
};
class Application : public IApplication
{
std::string m_s;
public:
void set(const std::string& r_s)
{
m_s = r_s;
}
void display() const
{
std::cout << m_s << std::endl;
}
};
class Renderer : public IRenderer
{
Resource* p_res;
public:
void set(Resource* p)
{
p_res = p;
}
void display() const
{
std::cout << p_res << std::endl;
}
};
int main()
{
// setup an environment
Resource res;
Texture tex("some string");
tex.setp( &res );
// application's access
Application app;
tex.access( app );
app.display();
// renderer's access
Renderer rend;
tex.access( rend );
rend.display();
}
/*
some string
0x7fff4c23687f
*/- Hide quoted text -
- Show quoted text -
Not sure I follow.
methods and data specific to the texture.
Maybe I am just getting confused on names and made it too specific.