Jump to content
  • Advertisement
Sign in to follow this  

Is it generally wrong to style a C++ library like Java or C#?

This topic is 2955 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 really enjoy the way Java and C Sharp organize their classes
(Especially Java for collecting its trashes)
But C Plus-plus looks nothing like Sharp
For the members are not covered by the double-colon's tarp.

Generally constants or values are accessed by the dot operator.
And everything is treated like an object. (Hey, I'm no hater).
The case in point is that I have not seen any code in C plus-plus
That looks anything like Sharp or Java; I wonder if there is a reason that is just.

i.e. C++
Colors::WHITE // Color constant
std::cout << "Blah blah blah" << std::endl;
Singleton* s = Singleton::GetInstance(); s->DoSomething();

i.e. Java/C# style
Colors.white // Color constant
System.out.println("Blah blah blah");

When C++ COULD be like this:

class ColorsClass
const white = HEXRGB(255,255,255);
const black = HEXRGB(0, 0, 0);


const ColorsClass Colors;

Is there a general reason to this design? C# and Java seem very clear.
Everything is treated as an object. But C++ isn't so near.
Would it be wrong to code a C++ app in the style of C# or Java?
Or would that be a coding sin and I awaken in hot boiling lava?

Share this post

Link to post
Share on other sites
There's two ways I read your post. Here's my opinion on your question approached from both angles, and it's worth exactly what you paid for it.

"Cosmetic" Style:

I wrap all my enums with a namespace and typedef it kind of like what you did with a class. I tend to name like this: ClassNames, _param_vars, and local_vars. You won't see any of that in STL, but it's all opinion. Unlike C# or java, c++ doesn't have "accepted standards." If you look at a bunch of different c++ libraries, they all approach the nuts and bolts of the code differently. In some ways is unfortunate to not have consistency, but on the other hand, different people have different opinions. There is absolutely nothing built-in to the language to force you to go in a particular direction. I'd look at the API for stl and see how they do their naming, but I personally don't care for it and prefer my own "standard" I've settled on over my time with C++. The only thing I'd suggest is that you consider namespaces rather than classes to wrap your constants because it seems like a more natural fit to me. Not that it will really matter after the compiler gets done with your code. Do what feels comfortable.

"Functional" Style:

Java/C# treat memory completely differently from C++. They are meant to be more restrictive than C++ (i.e. you can't just dereference whatever memory you want at will.) There's plenty of good/bad aspects to this. But this abbreviated conclusion is that the optimal design in Java/C# that embraces OOP may not be the optimal design in C++. I'd say in general if you want to do something "like Java/C#" you can, but in C++ you may be able to work in a different paradigm more suited to your problem.

In particular if you're creating a library you'll want to be careful about how memory gets passed around. This can bite you, especially if you want to distribute it as a DLL. While Java/C# will make sure everything magically works, in C++ you need to be aware of what you're actually asking the computer to do (i.e. if you allocate memory in one DLL and free it in another, will it try to return that memory to the same heap?). If you want your design to succeed, carefully consider the problem AND the environment you're trying to solve it in!

Share this post

Link to post
Share on other sites
C++ is not C#. Nor is it Java.

You must take into account C++'s foibles when it come to memory and resource management, as well as many other areas. Failing to do so is indeed a sin.

It is less objectively important to use conventional C++ syntactical style, but I would say it is still a (lesser) sin to try and shoehorn Java/C# style syntax into C++. Whenever you find yourself working against the language's conventions, you're probably using the wrong language.

Share this post

Link to post
Share on other sites
It's a bad idea to instantiate objects at file scope. The c++ runtime has to run all their constructors at startup and it's very easy to code your way into cyclic dependence issues. I believe the static initialisation order is undefined so you'll create a mighty mess if you have one static constructor accessing another static object. And in c++, the dot operator is intended to access members of a class instance, not global content. The standard library uses namespaces so to avoid confusion you should too.

There are a couple of different options when creating a c++ utility library

1) Create a class with all static members. Access members with the scope operator (::). Using the library will always require fully qualifying the object names (MyUtilClass::someConstant) since you can't use the namespace "using" keyword. But otherwise the use of a static class member and a namespace member is basically identical.

2) Create a namespace with your utility functions and constants. The drawback is that your utility functions will probably call functions of their own, which you'll often want to make private. Namespaces don't support private members, however you can hide them by using the c++ linker model to your advantage. Declare the functions you want to keep private in the namespace's source (cpp) file, at file scope. People using the library won't see them as they can only access whatever is declared in the headers.
But sometimes this won't work as you'll have private functions you want to share across multiple source files. In that case, put the private functions in a nested namespace, which is often called "Detail" in certain conventions (e.g. Boost). It doesn't prevent people accessing them, but it is an indicator that they shouldn't.

Yeah I know, compared to other languages, the c++ linker and scope management model is insanely complicated.

Share this post

Link to post
Share on other sites
It's a feature that will do nothing but confuse people. Most people using a C++ library are not going to expect that sort of syntax. Nor is it likely to match with how they write their code, or other libraries they use.

But, go ahead if you want. It's your library, after all.

Share this post

Link to post
Share on other sites
Thanks for the replies and insight to my post.
I just like the way Java looks but use C Plus-plus the most.
I agree designing a library like that would not succeed.
But you have to admit it -may- be easier to read.

Share this post

Link to post
Share on other sites

But you have to admit it -may- be easier to read.

What if someone came to Java or C# and used C style - one single class with nothing but static functions and primitive types.

I know plenty of codebases like that.

And some are even absurdly efficient.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!