#when should I use the interface class?

1 messages · Page 1 of 1 (latest)

stark orchid
#

I read somewhere that you need to use interfaces to access an object, but what's the point if cpp has private and protected? And in what cases should I use them?

magic scrollBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp kindle
#

C++ does not have interfaces

#

it does allow one to construct code in the pattern of an interface

#

but it is not directly a language feature

#

the point of the interface pattern is to reduce concepts down to only the code relevant to describing them and then allow multiple chunks of code to rely on and implement that common interface (which also makes it easier for people to think about and work with)

stark orchid
#

yes, but what makes people do this:

class ISomeClass
{
public:
virtual void foo()=0
//other virtual functions 
};
class CSomeClass : public ISomeClass
{
public:
virtual void foo() override final {/*implementation */}
private:
int var;
};

Instead of this

class CSomeClass
{
public:
virtual void foo() override final {/*implementation */}
private:
int var;
};
sharp kindle
#

because more than one other class may inherit from ISomeClass so they can refer to ISomeClass and mean any of those other implementing objects