Jump to content
  • Advertisement
Sign in to follow this  
dist0rted

c++ "interfaces"... what are they?

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been programming in C++ for about five years now, and I feel retarded because I don't even know what makes an interface different from a class. I've done a brief google search and not found anything, but perhaps someone can give me a simple answer to end my stupidity?

Share this post


Link to post
Share on other sites
Advertisement
Interfaces are keywords in some languages. In C++ they probably exist as keywords in C++/.NET, but more than likely it's just a generic phrase to describe the common, unchanging publicly visible bits of a class or group of classes. Most often (depending on the context) it refers to a (usually abstract) base class which acts as those publicly defined bits that other classes may inherit from and then implement.

Share this post


Link to post
Share on other sites
Programming languages such as C# and Java have "interfaces" which consist (in C++ terminology) solely of public pure-virtual functions. That is, they define a contract, which a class can choose to implement by subclassing the interface and providing implementations for the functions. In C++ there's no explicit interface keyword, but the concept is the same.

Share this post


Link to post
Share on other sites
The interface is part that is publicly visable. If you have an API the interface is the publicly useable part. For a class it is the part declared public. For libraries the interface is usally kept static so that people don't have to relearn everything for the next release of the library.

Share this post


Link to post
Share on other sites
so, basically, an interface is meant for every part of its data to be inherited into the next generation of classes? Like it's sole purpose is to be a "IAnimal" for the "IDog" and "ICat"?

Share this post


Link to post
Share on other sites
C++ does not have interfaces as you know them from Java. You can use inheritance in it's various forms to achieve similar results to Java. You could use (pure) virtual member functions, virtual inheritance and multiple inheritance to get the results you want. However, the trick is....

You do not need interfaces in C++.

C++ provides you with excellent generic programming facilities, which completely rid you of the need to represent interfaces. Consider the following functions.



class Bindable;

void bind(Bindable &b)
{
b.bind();
}

// - OR -

template <typename Bindable>
void bind(Bindable &b)
{
b.bind();
}




They give you exactly the same functionality, one using a interface-like inheritance, the other using a generic function. This is only a trivial example, but the same principle applies to bigger entities.

You may need to adapt to a different mindset when designing for C++, but the bottom line is that most of the time you should not use inheritance to create interfaces.

Now sit back and think about it, perhaps go to your local library to read some C++ books and you'll feel enlightened in no time :)

-Riku

Share this post


Link to post
Share on other sites
Quote:
Original post by riku
You do not need interfaces in C++.


Until the time comes when you need dynamic binding.


class Bindable;

void Apply(std::vector<Bindable*> & bindable) {
for (int i = 0; i < bindable.size(); ++i) {
bind(bindable);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by dist0rted
so, basically, an interface is meant for every part of its data to be inherited into the next generation of classes? Like it's sole purpose is to be a "IAnimal" for the "IDog" and "ICat"?


I might have misunderstood you, but interfaces are not supposed to have any data at all. Data is implementation, the interface should only be concerned with providing a template (the dict. meaning of the word) for the public functions of a class.

Share this post


Link to post
Share on other sites
Quote:
Original post by Omid Ghavami
I might have misunderstood you, but interfaces are not supposed to have any data at all. Data is implementation, the interface should only be concerned with providing a template (the dict. meaning of the word) for the public functions of a class.


Well, if we're into dictionary meaning of works, we could as well say that an interface provides the interface of a class (how the class interacts with its environment). Note that C++ (and most languages) enforce only the syntactic interface of classes, not the semantic interface.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Omid Ghavami
I might have misunderstood you, but interfaces are not supposed to have any data at all. Data is implementation, the interface should only be concerned with providing a template (the dict. meaning of the word) for the public functions of a class.


Well, if we're into dictionary meaning of works, we could as well say that an interface provides the interface of a class (how the class interacts with its environment). Note that C++ (and most languages) enforce only the syntactic interface of classes, not the semantic interface.


Fair enough [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!