473,890 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

learn C++ or C#

If I haven't made substantial investment in either C++ or C#, which language
would the experts recommend I become well acquainted with?

Daniel
Jul 17 '08
151 4570
But hey, if you guys want to keep wasting time arguing about pointless
differences, be my guest.
http://en.wikipedia.org/wiki/Hypocrisy
Jul 24 '08 #101
Peter Duniho wrote:
This distinction has nothing to do with the _language_. If the OS was
entirely garbage collected, then these "non-memory resources" wouldn't
be an issue. They would be managed the same way memory is in .NET and
you'd never have to worry about disposing them.

Now, with certain types of objects you'd still have to deal with
closing, flushing, etc. But that's not something that RAII inherently
solves; it just happens that C++ classes can take advantage of that to
handle those operations. The OS API itself isn't based on C++ and
requires the program to deal with managing those operations.

In C++, RAII provides a convenient way to deal with that, and in C#, the
"using" statement does the same. You may prefer one syntax over the
other, but I personally don't see a difference between the two that
would justify an argument of superiority one way or the other. Larry's
claim of "vastly superior" seems particularly baseless. Vastly? Pure
hyperbole.
Not only is the "using" mechanism less convenient/elegant for the client of the
class than RAII (not to mention the possibility of forgetting to do it), but a
C# Dispose'd object can still be used (unlike a C++ object that goes out of
scope or is delete'd). This places an additional burden on the writer of an
IDisposable class.

IMHO, any use of a Dispose'd object should automatically cause a
"ObjectIsDeadEx ception" rather than relying on the author to test and throw an
ObjectDisposedE xception in every method.

--
David Wilkinson
Visual C++ MVP
Jul 24 '08 #102
On Thu, 24 Jul 2008 11:23:49 -0700, David Wilkinson
<no******@effis ols.comwrote:
Not only is the "using" mechanism less convenient/elegant for the client
of the class than RAII (not to mention the possibility of forgetting to
do it), but a C# Dispose'd object can still be used (unlike a C++ object
that goes out of scope or is delete'd).
I was comprehending your post until "or is delete'd". There's nothing in
C++ to stop someone from trying to use an object that's been deleted, and
depending on how the memory was allocated and what the class does in its
destructor, doing so might even work for awhile.
This places an additional burden on the writer of an IDisposable class.
I don't see how it's any more burden than already exists in C++. There's
no requirement that a disposable class actually catch post-disposal uses.
It's nicer, but then so too would it be nicer for a C++ class to catch
uses that occur after destruction. And I think it's easier for a C# class
to do so, because the object is still actually allocated and the code in
the class can behave predictably; in C++, sometimes using an object after
destruction will fail immediately, and sometimes it won't.
IMHO, any use of a Dispose'd object should automatically cause a
"ObjectIsDeadEx ception" rather than relying on the author to test and
throw an ObjectDisposedE xception in every method.
I don't disagree that RAII allows for an object to simply "disappear" when
the scope is exited, but this is how "using" works in C# too. Variables
declared in the "using" statement are valid only for the scope of the
block of that statement.

And just as in C#, the reference to the variable declared in the "using"
statement could "escape" by being passed to something else, so too could a
pointer to a stack-allocated C++ object escape. RAII in C++ doesn't
actually guarantee that you can't use the object reference after the
object itself has been destroyed. It just ensures that the object is
destroyed outside a particular scope, just as "using" does.

If anything, at least with a garbage collecting system, you know that if
you have a reference to something, that's a valid reference. The object
itself might be in an unusable state, but it will always be able to tell
you that one way or the other.

Again, in terms of any practical difference between the two, any
difference is negligible and not a point worth worrying about in terms of
comparing the languages. One might save a little typing over the other
(and there are other examples going the other direction), but in the
greater scheme of things, that's just not important.

Pete
Jul 24 '08 #103
Larry Smith wrote:
>>>Multi-threading, GUI development is much easier in most of the other
languages compared to the efforts you have to take in C++, because they
support it out of the box. Fortunately C++ has Boost.
To be fair, this has nothing to do with C++ as a language (which has no
support for multi-threading or GUI whatsoever). You have to separate the
tools from the language itself.
I don't think so. What would C++ be without the standard library ?

It would be another language since it must include the library by
definition. Contrast this to C# whose basic support is primitive (see the
standard for yourself). In any case, the claim that GUI development is
"easier" in C# compared to C++ has nothing to do with the languages
The RAD tools have to parse the source code, which is more complex in
C++ and therefore there aren't that much (good) RAD tools for C++ or
they feel somewhat sluggish.
So let's restrict the "easier" part on development of good RAD tools not
on using them.
themselves. It's the rich set of classes in MSFT's framework combined with
better development tools that make C# easier (noting that C# is a natural
fit for the framework unlike C++). Provide an equivalent C++ library and
better tools and it will be just as easy (not taking into account the actual
language differences).
With easier I meant the whole development experience - so it was perhaps
somewhat misleading to use the word "easier".
With development experience I mean Intellisense / refactoring / fast
compilation and so on.

There are GUI frameworks supported by different languages including C++:

C++ (/CLI): WinForms, VCL
C# : WinForms
Delphi : VCL

But IMHO the development experience in C#, Delphi is better than in C++,
just because of the compilation speed. It makes a huge difference if you
wait multiple minutes after having touched the GUI or just a few seconds.

Andre

Jul 24 '08 #104
Peter Duniho wrote:
On Thu, 24 Jul 2008 11:23:49 -0700, David Wilkinson
<no******@effis ols.comwrote:
[...]
I don't disagree that RAII allows for an object to simply "disappear"
when the scope is exited, but this is how "using" works in C# too.
Variables declared in the "using" statement are valid only for the scope
of the block of that statement.
[...]
For scoped resource handling I think "using" is a good alternative for
RAII. But I miss RAII sometimes in C#:

E.g:

list (a) - holds a file object (f)
list (b) - holds the same file object (f)

When I remove (f) from list (a) I have to check if list (b) holds the
file object too or rely on GC to close it - but I think that wouldn't be
a good idea, since the file would be open, till the GC has disposed the
object.

In C++ I can use RAII smart pointers to ensure that the file is closed
as soon as it's not used anymore. In C# I have no automatism to handle
this.

I see that there are benefits in GC over RC = reference counting, but
IMHO both are relevant:

GC - for memory
RC - for resources
Andre
Jul 24 '08 #105
Peter Duniho wrote:
>Not only is the "using" mechanism less convenient/elegant for the
client of the class than RAII (not to mention the possibility of
forgetting to do it), but a C# Dispose'd object can still be used
(unlike a C++ object that goes out of scope or is delete'd).

I was comprehending your post until "or is delete'd". There's nothing
in C++ to stop someone from trying to use an object that's been deleted,
and depending on how the memory was allocated and what the class does in
its destructor, doing so might even work for awhile.
>This places an additional burden on the writer of an IDisposable class.

I don't see how it's any more burden than already exists in C++.
There's no requirement that a disposable class actually catch
post-disposal uses. It's nicer, but then so too would it be nicer for a
C++ class to catch uses that occur after destruction. And I think it's
easier for a C# class to do so, because the object is still actually
allocated and the code in the class can behave predictably; in C++,
sometimes using an object after destruction will fail immediately, and
sometimes it won't.
>IMHO, any use of a Dispose'd object should automatically cause a
"ObjectIsDeadE xception" rather than relying on the author to test and
throw an ObjectDisposedE xception in every method.

I don't disagree that RAII allows for an object to simply "disappear"
when the scope is exited, but this is how "using" works in C# too.
Variables declared in the "using" statement are valid only for the scope
of the block of that statement.

And just as in C#, the reference to the variable declared in the "using"
statement could "escape" by being passed to something else, so too could
a pointer to a stack-allocated C++ object escape. RAII in C++ doesn't
actually guarantee that you can't use the object reference after the
object itself has been destroyed. It just ensures that the object is
destroyed outside a particular scope, just as "using" does.

If anything, at least with a garbage collecting system, you know that if
you have a reference to something, that's a valid reference. The object
itself might be in an unusable state, but it will always be able to tell
you that one way or the other.
<snip>

Pete:

Well, I think C++ programmers (even bad ones) are generally aware of lifetime
issues, whereas C# programmers can be lulled into a sense of false security by
the "garbage collector will take care of everything" mindset.

I just find this notion of the "half dead" object rather disturbing. Surely it
would have been possible for the C# language to be defined such that use of a
Dispose'd object automatically throws an exception?

--
David Wilkinson
Visual C++ MVP
Jul 24 '08 #106
On Thu, 24 Jul 2008 12:30:34 -0700, Andre Kaufmann
<an************ *********@t-online.dewrote:
[...]
When I remove (f) from list (a) I have to check if list (b) holds the
file object too or rely on GC to close it - but I think that wouldn't be
a good idea, since the file would be open, till the GC has disposed the
object.

In C++ I can use RAII smart pointers to ensure that the file is closed
as soon as it's not used anymore. In C# I have no automatism to handle
this.
But you can include reference counting in the C# object if you want. You
can even implement a "smart pointer" class that uses Dispose() to
decrement the counter and call Dispose() on the wrapped object if it
reaches 0.

Personally, I've had enough headaches with reference counting in my
lifetime, and I try to avoid it when I can, smart pointers
notwithstanding . I prefer to not create designs in the first place where
unique ownership of the object is unclear. But there's nothing about C#
that precludes using that technique if you find yourself in a situation
where you think it's useful or needed.

Pete
Jul 24 '08 #107
On Thu, 24 Jul 2008 12:41:17 -0700, David Wilkinson
<no******@effis ols.comwrote:
Well, I think C++ programmers (even bad ones) are generally aware of
lifetime issues, whereas C# programmers can be lulled into a sense of
false security by the "garbage collector will take care of everything"
mindset.
I have seen too much bad C++ code (especially code with rampant memory
leaks) in my life to have the high opinion of bad C++ programmers you
apparently have. Conversely, part of being proficient with C# is
understanding how the garbage collection works, and how that interacts
with the non-garbage-collecting parts of the platform. Only bad C#
programmers are "lulled into a sense of false security".
I just find this notion of the "half dead" object rather disturbing.
Surely it would have been possible for the C# language to be defined
such that use of a Dispose'd object automatically throws an exception?
Well, first of all, to some extent it's a question of what the CLR
requires, not what C# requires. And sure, it could. But why?
IDisposable has a specific goal in mind, and no part of that goal involves
requiring that an object become unusable after Dispose() is called.

Inasmuch as there may be classes out there for which it makes sense to
still be able to use the object after it's been disposed, why should the
language or run-time impose an arbitrary restriction on that ability?

As an example: the Form class does not dispose instances when the Form is
closed if it was shown using ShowDialog(), even though it does if the Form
was shown using Show(). This inconsistent behavior is to allow retrieval
of values from the controls in the form after it's been closed. But one
can easily imagine an implementation that completely separates the
unmanaged aspects from the managed aspects, copying any data from the
unmanaged resources upon disposal so that they are still accessible. Then
closing the modal form could be the same as closing the non-modal form: in
both cases, closing implies disposal.

But one could only make that more consistent behavior as long as the
run-time allows a disposed object to be used after Dispose() is called.

Just because an object is done with its unmanaged resources, that doesn't
mean to me that it's "half-dead". It's just done with the unmanaged
resources. If a class wants to allow disposal prior to actually being
done with the object and still permit certain operations on the object
after disposal, that should be its right.

Pete
Jul 24 '08 #108
Peter Duniho <Np*********@nn owslpianmk.comw rote:

<snip>
Just because an object is done with its unmanaged resources, that doesn't
mean to me that it's "half-dead". It's just done with the unmanaged
resources. If a class wants to allow disposal prior to actually being
done with the object and still permit certain operations on the object
after disposal, that should be its right.
MemoryStream is one example, by the way - you can still call ToArray on
it after closing it as a stream.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 24 '08 #109
A simple example:
>
How do I ensure in C++ that I have successfully overridden a base
class virtual function and that the compiler throws an error if the
base class implementation has changed ?
You can't do that in any language I'm aware of, so it's a non-issue. Did
you mean "error if the signature of the function in the base class has
changed"?
>
Andre

Jul 24 '08 #110

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3442
by: Rob | last post by:
i know javascript, vbscript, asp css and alot more and im only 14 i was wondering which is easier to learn php or cgi. any help?
42
3738
by: Bicho Verde | last post by:
I have now free time and money to do what I want :-) I have some basic skills in programming (C, Pascal, Macromedia Actionscript) but don't know exactly what to do in the world of programming. And also I don't know exactly why would I learn Python rather than C#, C++ or Perl. Basicaly I don't know where to start, if there is much to do or if it is has it seems and there is software to everything nowadays and so doesn't make sense to spend...
55
46035
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I know PHP, BASIC, and of course HTML. I'll be 15 years old in September. I am interested in programming GUI applications. I am also interested in programming games. I know that I should learn C++ to program games, but would learning Java make the...
30
9445
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with Java very often. I have no real C++ experience and not much C experience for that matter. However, the core Java statements are "borrowed" from C and C++ has often been called "C with classes". It seems to me that it shouldn't take very long to...
8
4055
by: Hermawih | last post by:
Hello , I want your opinion about this . In order to say it clearly , I think I have to describe it in long sentences . I could consider myself as Intermediate/Advance Access Developer ; Intermediate/Advanced Database designer . Because of the requirements , I must create Web Application . Access Pages is not suitable for that so I think about learning VB Net / ASP Net . I am
21
2875
by: TAM | last post by:
Hi, I read that ASP.NET uses VB.NET instead of VBScript. I also read that ASP.NET is a subset of VB.NET. So if I learn VB.NET first then do I have the knowledge for programming ASP.NET applications or do I need to learn both VB.NET and ASP.NET. Thank you. TAM
85
4194
by: abhi | last post by:
hi everybody am new to this group and help me to learn C
31
2642
by: anand devarajan | last post by:
hi friends, im anand im just a beginner in c learning for the past two weeksnow i can write simple prgs can anyone help me to get well known to c lang so that i should able to write even tough prgs in c
34
3173
by: pandit | last post by:
hai all, i want to become a good programmer. one of my friends ( named "arnuld", he posts here infrequently), taught me Lisp. so i am not a programming beginner. i have heard these 2 points. i want to know how : 1. C gives you a strong base of Procedural style of programming which forms the basis of learning other paradigms e.g OOP
65
5325
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
0
9970
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9612
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7153
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6031
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.