LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ Alternatives (https://www.linuxquestions.org/questions/programming-9/c-alternatives-774022/)

MTK358 12-07-2009 12:41 PM

C++ Alternatives
 
I wanted to to a few projects that need a compiled object-oriented language, so I tried C++ and can't say I like it. C is great but it is not OO. So I was windering if there is some other compiled language that is OO, has a free/open source compiler for Linux, and is preferably backward-compatible with C?

johnsfine 12-07-2009 12:58 PM

Quote:

Originally Posted by MTK358 (Post 3782981)
I tried C++ and can't say I like it. C is great but it is not OO.

I don't know if any other language could be a better fit for your requirements.

What didn't you like about C++?

Lots of people dislike C++ because of features that are similar to C. But you said "C is great".

Lots of people, who like C, dislike C++ because it was their first introduction to OO and they don't actually like OO. But you think you want OO.

Lots of people dislike C++ because of all the confusing detailed rules inside the obscure advanced features (such as partial specialization of templates). If that is what bothered you, you had a bad introduction to C++. If you don't need the power of such features, you can program in C++ without being bothered by their existence. If you do need the power, some hypothetical language with a more self consistent design for such features would be great if it existed. But it doesn't. If you need such features and good performance, C++ is the only choice.

Sergei Steshenko 12-07-2009 12:59 PM

Quote:

Originally Posted by MTK358 (Post 3782981)
I wanted to to a few projects that need a compiled object-oriented language, so I tried C++ and can't say I like it. C is great but it is not OO. So I was windering if there is some other compiled language that is OO, has a free/open source compiler for Linux, and is preferably backward-compatible with C?

I think you'd better look at languages not from "C" family.

Look at, say,
http://en.wikipedia.org/wiki/Eiffel_...mming_language .

MTK358 12-07-2009 01:17 PM

Quote:

Originally Posted by johnsfine (Post 3782994)
I don't know if any other language could be a better fit for your requirements.

What didn't you like about C++?

Lots of people dislike C++ because of features that are similar to C. But you said "C is great".

Lots of people, who like C, dislike C++ because it was their first introduction to OO and they don't actually like OO. But you think you want OO.

Lots of people dislike C++ because of all the confusing detailed rules inside the obscure advanced features (such as partial specialization of templates). If that is what bothered you, you had a bad introduction to C++. If you don't need the power of such features, you can program in C++ without being bothered by their existence. If you do need the power, some hypothetical language with a more self consistent design for such features would be great if it existed. But it doesn't. If you need such features and good performance, C++ is the only choice.

For example, I once wrote a derived class from another class that had different constructors, and C++ complained about it. But I don't want them to have the same constructors! In fact, the base class's constructors were completely irrelevant for the derived class!

Java's pass-by-reference makes much more sense and wastes less computing power. Usually when I want to copy an object I want a handle that can modify the original. This can be done in C++ with pointers, but you have to worry about the original going out of scope.

C++ references seem very confusing, how do I know if it's a reference or variable if the syntax is the same? Even if you get past this, where do I use references and where do I use pointers?

Mainly because the above things I said, it is very difficult to write a program that feels simple and solid, because there is either too much ways to do it or no way to do it. Programming in C++ feels more like some poor hack than making a solid program.

johnsfine 12-07-2009 01:48 PM

Quote:

Originally Posted by MTK358 (Post 3783009)
For example, I once wrote a derived class from another class that had different constructors, and C++ complained about it. But I don't want them to have the same constructors! In fact, the base class's constructors were completely irrelevant for the derived class!

I think you misunderstood the problem.

A derived class normally has different constructors than the base class. You cannot inherit constructors (I would like C++ better if you could).

A derived class must invoke the constructor of the base class (either explicitly or implicitly). But that should be true in any OO language.

Quote:

Java's pass-by-reference makes much more sense and wastes less computing power.
Java references are simpler to use, but they usually waste more computing power than C++ references.

Quote:

Usually when I want to copy an object I want a handle that can modify the original. This can be done in C++ with pointers, but you have to worry about the original going out of scope.
So why do you like C?

You just described one of the major reasons many programmers dislike both C and C++. But how can you dislike that in C++ but like C?

Quote:

C++ references seem very confusing, how do I know if it's a reference or variable if the syntax is the same?
The fact that the syntax is the same between value and reference is the strength of the reference feature of C++. But I can see how you might be uncomfortable with it before getting used to it.

In any non trivial programming language, the meaning of a chunk of code depends on things defined elsewhere. When I switched from C to C++, I was initially put off by the fact that meanings depend on distant definitions a lot more in typical C++ code than C and there are many more ways they can depend (such as whether some argument was declared by value or reference). But I soon got used to it, and appreciate the extra ways to localize design decisions.

Quote:

Even if you get past this, where do I use references and where do I use pointers?
A lot of the choice between pointers and references is a matter of style and preference. If C++ references bother you, I think you can use pointers in almost all cases without losing much. The difference between pointers and references exists primarily at compile time (not at run time).

In more advanced programming (such as overloading operators and defining templated methods) there are things that really need references

Quote:

there is either too much ways to do it or no way to do it.
If there is no way, you're either confused or what you want probably couldn't be done directly in other languages either.

There certainly are too many ways to do almost anything in C++ (and I expect you don't even know about half the ways yet). I think you can learn a good subset of C++ to work with.

ta0kira 12-07-2009 02:02 PM

One major consideration should always be the "social value" of the language. For example, you'll want a language used by lots of people if you want others to be involved eventually. Not only that, but the language should match the problems you're solving, as far as what other people might use. You don't have to "follow the crowd", but if you want help and you want people to gain from your source code, you should definitely take other programmers into consideration. If you're code is "yours forever" then you don't have to worry about this.

Along the same lines, you should look at the trends in language usage. I hate to say this, but Java's still on the rise, although C and C++ aren't going anywhere anytime soon. You might also want to look into the common languages used in Linux such as python, perl, lisp, etc.
Kevin Barry

MTK358 12-07-2009 03:10 PM

Quote:

Originally Posted by johnsfine (Post 3783034)
A derived class must invoke the constructor of the base class (either explicitly or implicitly). But that should be true in any OO language.

Not so in Java.

Quote:

Originally Posted by johnsfine (Post 3783034)
You just described one of the major reasons many programmers dislike both C and C++. But how can you dislike that in C++ but like C?

I guess that was more of a comparison to Java.

Quote:

Originally Posted by johnsfine (Post 3783034)
A lot of the choice between pointers and references is a matter of style and preference.

It still seems kind of strange to have two ways of doing exactly the same thing.

Quote:

Originally Posted by ta0kira
One major consideration should always be the "social value" of the language. For example, you'll want a language used by lots of people if you want others to be involved eventually. Not only that, but the language should match the problems you're solving, as far as what other people might use. You don't have to "follow the crowd", but if you want help and you want people to gain from your source code, you should definitely take other programmers into consideration. If you're code is "yours forever" then you don't have to worry about this.

Yeah, I was trying to avoid "obscure" languages because of this.

Quote:

Originally Posted by ta0kira
Along the same lines, you should look at the trends in language usage. I hate to say this, but Java's still on the rise, although C and C++ aren't going anywhere anytime soon. You might also want to look into the common languages used in Linux such as python, perl, lisp, etc.
Kevin Barry

The main reason I want something that is a subset of C is because many of my programs will rely on C libraries (e.g. Xlib). And it must be OO (this is almost the only reason I am not using C).

johnsfine 12-07-2009 03:23 PM

Quote:

Originally Posted by MTK358 (Post 3783124)
Not so in Java.

Of course it is true in Java. Either the derived class constructor explicitly invoked the based class constructor, or the language does it for you.
Java is a tiny bit less strict in its rules for invoking the base class constructor. But it still has to be executed.

Quote:

It still seems kind of strange to have two ways of doing exactly the same thing.
It isn't exactly the same thing. In more advanced programming, the power of references that cannot be duplicated by pointers comes into play.

Even in simple programming, the two different syntaxes for one behavior are a useful choice. Do you think C, Java, etc. should not have for loops because the meaning is the same as a while with a couple extra lines? Should C not have X[n] syntax because the meaning is the same as *(X+n)?

MTK358 12-07-2009 03:28 PM

Quote:

Originally Posted by johnsfine (Post 3783136)
Of course it is true in Java. Either the derived class constructor explicitly invoked the based class constructor, or the language does it for you.
Java is a tiny bit less strict in its rules for invoking the base class constructor. But it still has to be executed.

So it calls the superclass's constructor automatically or what? if so, how does it know the parameters?

I always thought that the superclass's constructor is not executed unless you call super() in the subclass's constructor.

Quote:

Originally Posted by johnsfine (Post 3783136)
It isn't exactly the same thing. In more advanced programming, the power of references that cannot be duplicated by pointers comes into play.

Even in simple programming, the two different syntaxes for one behavior are a useful choice. Do you think C, Java, etc. should not have for loops because the meaning is the same as a while with a couple extra lines? Should C not have X[n] syntax because the meaning is the same as *(X+n)?

Maybe it will make sense, I probably haven't got to that level yet.:)

johnsfine 12-07-2009 04:07 PM

Quote:

Originally Posted by MTK358 (Post 3783141)
So it calls the superclass's constructor automatically

Yes. See 8.8.7 in Java Language specification, especially the sentence starting "If a constructor body does not begin with an explicit constructor invocation".

http://java.sun.com/docs/books/jls/t...ses.html#8.8.7

It appears something I used and appreciated long ago in Java was incorrect. So Java is not less strict than C++ about the base class constructor.

It was very convenient in Java to call super() after doing some initial processing of the calling arguments of the current constructor, even selecting an overload of super() at run time inside the current constructor based on argument values (not types). I have wished for that feature in C++.

But Java the language specification says you can't do that, so I guess the Java implementation I was using when I was able to do a little work before calling super() was wrong.

The semantic rules for calling the base class constructor are much more similar between C++ and Java than I thought. The syntax is not similar.

MTK358 12-07-2009 04:41 PM

If it is called automatically, how does it know what parameters to use if there isn't a no-parameter constructor for the base class?

Tinkster 12-07-2009 04:52 PM

And off of the java slant this is taking: have you considered
"Objective C"?


Not that I know much about it ...

MTK358 12-07-2009 05:06 PM

It's highly dependent on this nextstep/openstep/gnustep stuff, I don't understand that. Maybe it's OK.

ta0kira 12-07-2009 05:07 PM

Quote:

Originally Posted by MTK358 (Post 3783124)
It still seems kind of strange to have two ways of doing exactly the same thing.

The two are quite a bit different actually, although the underlying structure is essentially identical.

You can nest pointers as far as you want (*******) but you can't do so with references; a reference modifier can only be used once, and it must come last. References can never change (using defined behavior, anyway,) which is why they can be used as if they were an object. References require either a valid object or a dereference (good or bad) to create, whereas a pointer isn't initialized automatically, meaning it could point anywhere. This means a reference is in general more valid, although the object it points to can disappear, just like with a pointer. Other than that, you really need to perform a bad cast or dereference a bad pointer to get a bad reference. Lastly, a bad dynamic_cast of a pointer returns NULL, but it causes an exception with a bad reference cast.
Kevin Barry

rizwanrafique 12-08-2009 03:38 AM

Quote:

Originally Posted by MTK358 (Post 3783218)
If it is called automatically, how does it know what parameters to use if there isn't a no-parameter constructor for the base class?

Can't remember exactly how C++ doesn now but it's an OO principle so I'll be very surprised if it different across languages.

Only "default" constructors are called automatically. That is, constructors with no parameters. If don't provide an implementation of such a constructor the compiler creates one for you if there is no other constructor available for that class. If you do provide an implementation but don't invoke the paren't default constructor then one is invoked for you. And that is always the first call in child's constructor.

If there is a constructor with parameters in parent but you haven't invoked it explicitly then compiler will complain that it can't find a default constructor and you haven't invoked one manually. This mind you is "exactly" how java behaves. As I said, I haven't tested it in C++ in a long time but I'll be very surprised if its different in principle (except some implementation details).


All times are GMT -5. The time now is 01:24 AM.