Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Good idea or gimmick: Go-style OO-programming in C++ ?

Reply
Thread Tools

Good idea or gimmick: Go-style OO-programming in C++ ?

 
 
jeti789@web.de
Guest
Posts: n/a
 
      02-22-2013
Hello,

I lately had a short look at Google's Go language which has only simple support for OOP. My first thought was that a thin OO-layer as in Go on top of C would have been the right size. C++ in some ways comes to heavy on "OO-ness", at least IMHO. Made me think how this kind of thin OO-layer style programming as in Go could be applied to C++.

Sometimes I can get too excited about some "big idea" and after a while I have to realize that I spent too much time on something that wasn't really worth all that fuss. Therefore I would like to ask what other people think about how to do Go-style OO-programming in C++ compared to full-fledged big hammer C++ OO-programming, which appears to me to be oversized (at least for system programming).

Regards, Oliver
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      02-22-2013
On Feb 22, 10:11*am, (E-Mail Removed) wrote:
> I lately had a short look at Google's Go language which has only
> simple support for OOP. My first thought was that a thin OO-layer as
> in Go on top of C would have been the right size. C++ in some ways
> comes to heavy on "OO-ness", at least IMHO.


Nobody forces you to write big class hierachries in C++. Just go [no
pun intended] with a modern C++ style. Check out Stroustrup's C++11
style talk. Check out Sean Parent's "concept-based polymorphism" talk
for a twist.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-22-2013
On Fri, 2013-02-22, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello,
>
> I lately had a short look at Google's Go language which has only
> simple support for OOP. My first thought was that a thin OO-layer as
> in Go on top of C would have been the right size. C++ in some ways
> comes to heavy on "OO-ness", at least IMHO.


What do you mean, more exactly? OO is a term which seems to mean
different things to different people.

It's also a problem that you don't describe the features of Go! you
want us to have opinions on; it's probably something I'm familiar
with, but since I haven't studied Go! specifically I don't know what
"thin OO-layer" means.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
jeti789@web.de
Guest
Posts: n/a
 
      02-25-2013
> It's also a problem that you don't describe the features of Go! you
>
> want us to have opinions on; it's probably something I'm familiar
>
> with, but since I haven't studied Go! specifically I don't know what
>
> "thin OO-layer" means.
>
>
>
> /Jorgen
>


A class in Go:

type Point struct {
x, y float64
}

Adding a method to a class works like this:

func (self Point) Length() float {
return math.Sqrt(self.x*self.x + self.y*self.y);
}

Note that Length() is defined outside struct Point and the this pointer is handed over as a parameter (named self). A method in Go is public if the first letter is upercase, otherwise it is private.

Inheritance in Go is achieved through delegation, which is built into the language:

type Engine interface {
Start()
Stop()
}

type Car struct {
Engine
}

func GoToWorkIn(c Car) {
c.Start();
c.Stop();
}

In other words: there is no inheritance in Go. The sample code is taken from here: http://www.infoq.com/articles/google-go-primer

All right. You cannot mimic this in C++ and there is no point in bending a language like that. But the question remains whether more than that is really needed - at least for systems programming. In that respect, using C++ asa "C with classes" seems appropriate to me and not just a poor man's way to code in C++. I belive many C/C+ systems develpoer just do it like that without writing a book about it ...

-- Oliver
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      02-25-2013
On Friday, 22 February 2013 11:11:28 UTC+2, (E-Mail Removed) wrote:
> Sometimes I can get too excited about some "big idea" and after a while
> I have to realize that I spent too much time on something that wasn't
> really worth all that fuss.


That happens with everybody. I have written software over 20 years and
still get excited about some new idiom or some old idea under new angle
I had not thought about. It helps if to make plan at morning "what I will
do today?" and to stick to hit.

> Therefore I would like to ask what other
> people think about how to do Go-style OO-programming in C++ compared
> to full-fledged big hammer C++ OO-programming, which appears to me to
> be oversized (at least for system programming).


A language that *requires* garbage collection is bit over-sized for
system programming. C++ programmers tend to (not all of us) prefer RAII
instead.

Go does not have type inheritance, method overloading and/or generic
programming. I can well see how system programming can be done without
those but OO would feel like OO in C. I can do it if I want to but I
feel under-supported by language.
 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      02-25-2013
On 02/25/2013, Juha Nieminen wrote:
[snip]
> If you want a concrete example of a popular "C with OO" which some people
> consider "better than C++", try Objective-C. It consumes more memory and
> everything related to classes is slower than in C++. (According to my
> measurements calling a member function is approximately 6 times slower
> than calling a virtual function in C++. While it's impressive that they
> have succeeded in making the calls that fast, considering that they use
> messaging, each member function call is still 6 times slower.)


I thought that the invokation mechanism of member functions under
ObjectiveC actually depends on the number of members, so it is not even
constant time but O(log(n)) because ObjectiveC has to use a LUT for
member functions (they can be added at run-time).

However bad ObjectiveC may be, there is one point where it has IMHO an
advantage over C++: I can invoke member functions on objects in debug
mode. So if I have an ObjectiveC container, I can ask it easily for its
current size. This is much more cumbersome under C++.

BTW, I would not say that ObjectiveC is a real programming language as
such. I rather see it as a library for C (or more precisely for C++).

Regards,
Stuart

 
Reply With Quote
 
jeti789@web.de
Guest
Posts: n/a
 
      02-25-2013

> I thought that the invokation mechanism of member functions under
>
> ObjectiveC actually depends on the number of members, so it is not even
>
> constant time but O(log(n)) because ObjectiveC has to use a LUT for
>
> member functions (they can be added at run-time).


That's right. Objective-C has a runtime engine that carries out method dispatch dynamically. Thus Objective-C has in that way true message passing like Smalltalk, it is modeled after, and no function invokation as in C/C++ and Java (in Java only virtual methods are dispatched at runtime).

-- Oliver
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      02-25-2013
(E-Mail Removed) wrote:

> Hello,
>
> I lately had a short look at Google's Go language which has only simple
> support for OOP. My first thought was that a thin OO-layer as in Go on top
> of C would have been the right size. C++ in some ways comes to heavy on
> "OO-ness", at least IMHO. Made me think how this kind of thin OO-layer
> style programming as in Go could be applied to C++.


Nothing stops you from writing C++ code that follows that particular style.
Or even C.


<snip/>
> Therefore I would like to ask what other people think
> about how to do Go-style OO-programming in C++ compared to full-fledged
> big hammer C++ OO-programming, which appears to me to be oversized (at
> least for system programming).


There is no difference. Go doesn't support inheritance, and therefore it's
limited in that regard. That's it.


Rui Maciel
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      02-25-2013
Tiib <(E-Mail Removed)> writes:
>A language that *requires* garbage collection is bit over-sized for
>system programming. C++ programmers tend to (not all of us) prefer RAII
>instead.


RAII supports automatic storage duration,
but not dynamic storage duration. So it has
another scope.

 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      02-25-2013
On Monday, 25 February 2013 14:40:43 UTC+2, Stefan Ram wrote:
> �� Tiib <(E-Mail Removed)> writes:
> >A language that *requires* garbage collection is bit over-sized for
> >system programming. C++ programmers tend to (not all of us) prefer RAII
> >instead.

>
> RAII supports automatic storage duration,
> but not dynamic storage duration. So it has
> another scope.


The smart pointers (like std::auto_ptr, std::unique_ptr and std::shared_ptr)
are for controlling ownership of dynamically allocated objects.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
VMWARE for simulation - good idea? Kevin MCSE 21 03-11-2005 01:40 PM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM
Re: Self-training good idea? Mark Smith MCSE 0 07-22-2003 04:51 PM



Advertisments