Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just started to learn C++, coming from a C# and Java background.
I like to use interfaces in my code. In C++ though they do not exist so instead I make structs with pure virtual methods.

My questions are:

1) From what I understand there is no difference between struct and class besides the default accessor, but it is common place to see struct being used when the class is of a smaller size. Therefore I decided as a convention to use struct when writing interfaces since the way struct is viewed by programmers more closely resembles an interface. Are there any other conventions on this issue or is my convention bad or could cause confusion in any way?

2) Since an interface only has pure virtual and provides no implementation (most of the times, default methods are now a thing in Java) I thought it would be logical to only make a header file for it, instead of making both a .cpp and a .h file. Since a .cpp file is supposed to have the implementation and the .h file is supposed to have the method prototypes. Again, are there any other conventions on this issue or is my convention bad or could cause confusion in any way?


struct IRender
    virtual void render() = 0;

class Sprite : public IRender
    void render() override
        //code goes here

What I have tried:

NOTE: My intention is to get the opinion of those with more experience on how my pattern works, if it has problems, and if there is any other convention on issue that my convention is addressing. If you think the question is off topic I would appreciate if you could redirect me to a better place to ask my question. This question for example was marked as off topic on the Code Review Stack Exchange, though in my opinion asking for review on a pattern you in your code is not off topic.
Updated 5-Nov-16 5:36am
[no name] 4-Nov-16 10:47am    
If you want to have a discussion about your code, perhaps you should be posting in the Discussion Forums.
Member 12832832 4-Nov-16 10:55am    
Ok then. Thanks for the advice.
Richard MacCutchan 4-Nov-16 10:56am    
Seems a perfectly reasonable question. However, I have never used such patterns in C++ so cannot really offer an opinion. You may like to look at Bjarne Stroustrup's Homepage[^], since he is the creator of C++.
Rick York 4-Nov-16 11:04am    
I think is a suitable place for discussing your code. The lounge is definitely NOT the place for this.
Jochen Arndt 4-Nov-16 11:12am    
The C++ forum might be indeed a better place for this because there is no single convention and this is more a discussion topic than a question that can be simply answered (but it is not off-topic here).

An answer might be that you can do as you want or like. In larger companies there may be conventions or rules on how to write C++ source.

I'm using structures in C++ only like in C as plain data storage classes and use classes when there are member functions. But that is my personal view.

Using a header file only for simple classes is common and I'm using them too.

The major difference between a struct and a class in C++ is members and methods of a struct are public by default and they are private by default in a class. Otherwise they are essentially the same thing. I don't see a problem with what you are doing.

As for what a .h and a .cpp file are "supposed" to contain - that can very a bit. Sometimes quite a bit of implementation will be in a header file like for template code or classes with lots of inline methods. My general rule of thumb is to put as little as possible in header files. They are the public interface, what is shared between modules, and I try to keep that to a minimum. For example, these days I virtually never declare dialog classes in header files. I find that modules that use the dialogs really don't need the details. All they need is an interface function that lets them pass some data in and out. This is simplified dependencies quite a bit for me and minimizes compilation when dialog code changes.
Share this answer
Member 12832832 4-Nov-16 11:07am    
Thank you for your answer. I'm specifically interested in how people work in more "enterprise"-like environments like Google, Facebook, Ubisoft, etc...
I appreciate your post and I will re-post this on the discussion forums.
There are no problem with your code.

However, in C++ you might need to consider how those object get destroyed... so in some case, it might make sense to have a virtual empty destructor but it would not be an "interface" anymore as those in C# or Java.

So it would be similar to other languages but you would need a bit more precaution in some cases.

In particular, since C# and Java are garbage collected, often, you don't need to dispose your object as the run-time would automatically detect at some points that objects are not in use anymore.

It will result that in C++, there would be case where you would have to be able to properly delete the original object somehow while in managed language you won't need to care if the object does not hold a resource that needed to be closed.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900