开发者

Does C++ programmer simulate features of Java? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 12 years ago.

As I read in Thinking in java,

Interface and inner class provide more sophisiticated ways to organize and control the objects in your system. C++, for example, does not contain such mechanisms, although the clever programmer may simulate them.

Is it true that C++ programmer simluate features that java owns,e.g. interface and constraint themself not to step over the boundary,e.g. including non-static-final(non-const) data member within the simluated interface?

Are these features that Java provided natural way for developing software.So if C++ programmers could,they should code and think like a Java programmer?

EDIT: I know that every programming langauge have its own characteristics and its application area and the design of programming language is making tradeoffs.But what I want to know is whether something java introduced for ex开发者_运维技巧ample interface,are better way to help/force programmer think throughly and produce good class design?so C++ programmers would like to simulate some of these features?

thanks.


This is kinda subjective, but:

if C++ programmers could,they should code and think like a Java programmer?

No. They should code and think like C++ programmers. C++ has lots of idioms and techniques which do not exist in Java (and vice versa). You should use the idioms and thought patterns appropriate to the language, not try to write Java (or Pascal, or Fortran) with C++ syntax.

(That doesn't mean don't borrow tricks from other languages, of course...!)


C++ interfaces are easy: they're simply classes with all pure virtual methods, including a pure virtual default destructor. (Thanks to itowlson for the correction.)


You're looking at this from a Java-centric perspective. Consider for example that Java doesn't support the "RAII" style, which is well-supported by C++. The languages are different, and support different styles in different ways.


I have to say "good" Java design is almost uniformly terrible. I've never seen so much code duplication, ridiculous layered levels of abstraction (but almost never the abstractions that would have actually made sense in the situation) as when looking at Java code.

There are many good features of C++ that Java has no equivalent for. Features that enable cleaner, more robust and more elegant designs.

C++ programmers should code like C++ programmers. For two very good reasons:

  • They have to respect the weaknesses and flaws of the language. Trying to pretend you're in a garbage-collected language when you're not is a recipe for disaster. And trying to implement GC-like semantics on top of a language like C++ is probably even worse.
  • And just as importantly, they should exploit every strength of the language. When you can get truly generic collection classes with zero overhead and a sophisticated iterator implementation, why on Earth would you throw it away? When you've got one single sorting function that works for any container and even more generally, for any sequence of objects of any type, why would you throw it away?

Since you mention interfaces as an example of a feature C++ programmers should emulate, there are two important counterpoints:

  • C++ has interfaces in the form of abstract classes. The semantics are slightly different, but they can be used to serve the same purpose.
  • C++ doesn't need interfaces as much, because it has templates and concepts relying on compile-time ducktyping. Rather than having every iterator derive from an IIterator interface, we can simply define how an "iterator" should behave, and write a class that supplies members with the same names. As long as it "looks like" an iterator, it can be used as an iterator. Template metaprogramming tricks even make it possible to adapt existing classes to "retrofit" them to support a concept they weren't designed for. For example, raw pointers (as taken from C) work as perfectly valid iterators, despite missing a few typedef members. And without even being classes in the first place.

Of course, C++ has plenty of weaknesses too. But it is not simply an "inferior Java". It is a different language. There is no need for C++ programmers to emulate Java features.

As I read in Thinking in java

Java books are rarely good sources of information about C++. ;)


Probably the most important difference between C++ and Java is that Java has a garbage collector and C++ does not. This means that in C++, whenever you create an object on the heap, you need to know how you will be able to clean it up later. In Java you can always safely return an object knowing that the garbage collector will sort out its deletion.

So, if you code C++ like you code Java you will have leaks galore.


C++ has a single powerful concept (classes + multiple inheritance) to achieve a set of goals. Java has two less powerful concepts (classes + single inheritance and interfaces + multiple inheritance); if you combine them, you can do some of the things that C++ can do, but not all of them (e.g. interfaces with default implementations). So saying that Java is "more sophisticated" in this respect sounds a bit weird to me.

The better question is: Is the additional power that C++ gives you a good thing or not?


This is quite a subjective question, but, if I may share my opinion, no, C/C++ programmers should not think like "Java programmers", nor should Java programmers think like "C/C++ programmers"; Each language does one thing better and other things in its on way. However, both should think like programmers and like "Object Oriented Programmers".

Object Oriented Analysis makes us see the world as a collection of objects and their methods, the classes that group them, their common interfaces, etc. Your language may vary in the way those constructs are described, but in the end, the goal is the same. Once you understand WHAT you will implement and choose the language, then you start thinking of HOW to implement it, which is the same as telling you to choose between thinking as a "Java Programmer", "C/C++ Programmer" or a "C# programmer".


There are many Java features which are difficult for the C++ programmer to reproduce. Interfaces and inner classes are not among them, but in Java the compiler enforces that you can't have code in interfaces, whereas in C++ the programmer just has to write classes without any code. In Java inner classes you have the secret built-in parent.this field, whereas in a C++ nested class you have to do the typing and make that member explicit. C and C++ are both rather more DIY than Java.

So it depends what you think the feature is. If you think the Java interface feature is, "I can use polymorphic interfaces in my design", then that's trivial in C++. If you think the Java interface feature, "it is not possible to define method bodies in interfaces", then C++ doesn't have it.

If you want to precisely reproduce the features of Java, and "think like a Java programmer", then the proper thing to do is to program in Java...


I can't think of anything immediately, so I'm going to answer a firm NO, and dare the internet to provide counter examples.

Based on my interpretation of the 20:33:27 edit, we're looking for something that

  • (Good) C++ programmers were not already doing
  • ... that is supported by popular implementations
  • ... and is appropriate for the problem domains where C++ is an appropriate language choice

Tough parlay, that.

Some possible places to look for counter examples: Unit Testing Synchronization

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜