Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > considering taking the plunge

Reply
Thread Tools

considering taking the plunge

 
 
Sean McIlroy
Guest
Posts: n/a
 
      09-15-2008
hi all

i'm thinking of "diving into c++", but first i have a couple of
general questions which hopefully somebody here will be kind enough to
answer. my programming background is that i'm fairly familiar with
python, and i once took a single-semester course in c++, although that
was some time ago. here are the questions:

*) are class-instances first class, in the sense of being permissible
arguments for a function and also permissible return-values for a
function?

*) same question as above, except substitute "pointers" for "class-
instances"

hopefully these questions aren't too retarded. unfortunately my c++
instructor was something of a chucklehead; i never managed to see the
big picture, so most of the knowledge subsequently leaked out of my
ears. anyway, thanks if you can help.

peace
stm
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      09-15-2008
On Sep 15, 2:21*pm, Sean McIlroy <(E-Mail Removed)> wrote:

> *) are class-instances first class, in the sense of being permissible
> arguments for a function and also permissible return-values for a
> function?


Yes

> *) same question as above, except substitute "pointers" for "class-
> instances"


Yes. And the same goes for references as well.


> hopefully these questions aren't too retarded. unfortunately my c++
> instructor was something of a chucklehead; i never managed to see the
> big picture, so most of the knowledge subsequently leaked out of my
> ears. anyway, thanks if you can help.


Get a copy of "Accelerated C++" by Koenig and Moo. It's a great book
for beginners who have some programming experience.

 
Reply With Quote
 
 
 
 
Sean McIlroy
Guest
Posts: n/a
 
      09-16-2008
On Sep 15, 2:51*pm, red floyd <(E-Mail Removed)> wrote:
> On Sep 15, 2:21*pm, Sean McIlroy <(E-Mail Removed)> wrote:
>
> > *) are class-instances first class, in the sense of being permissible
> > arguments for a function and also permissible return-values for a
> > function?

>
> Yes
>
> > *) same question as above, except substitute "pointers" for "class-
> > instances"

>
> Yes. *And the same goes for references as well.
>
> > hopefully these questions aren't too retarded. unfortunately my c++
> > instructor was something of a chucklehead; i never managed to see the
> > big picture, so most of the knowledge subsequently leaked out of my
> > ears. anyway, thanks if you can help.

>
> Get a copy of "Accelerated C++" by Koenig and Moo. *It's a great book
> for beginners who have some programming experience.


i'll look for it. thanks
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-16-2008
On Sep 15, 11:21 pm, Sean McIlroy <(E-Mail Removed)> wrote:
> i'm thinking of "diving into c++", but first i have a couple
> of general questions which hopefully somebody here will be
> kind enough to answer. my programming background is that i'm
> fairly familiar with python, and i once took a single-semester
> course in c++, although that was some time ago. here are the
> questions:


> *) are class-instances first class, in the sense of being permissible
> arguments for a function and also permissible return-values for a
> function?


Although two people have already replied saying yes, I'm not
really sure what you mean by "class-instances". In C++, a class
is a type, and there is not a corresponding "object" which
represents the type. You can have instances of the class, which
have the type defined by the class, and these are first-class
"objects", with full value semantics (unless you inhibit it).
Unlike most OO languages, there are no hidden indirections
involved.

On the other hand, "class-instance" could be interpreted to
means an instance (of some other type) which "is" the class in
some way, e.g. an instance of std::type_info which represents
the class. The support for this in C++ is very, very limited,
and std::type_info is not copiable, and so cannot be returned
from a function.

> *) same question as above, except substitute "pointers" for
> "class- instances"


In C++, pointers are first class objects, just like any other
object. They have a value, can be copied, etc. A variable with
pointer type also has an address, so you can have pointers to
pointers, etc.

With regards to what can be an argument or a return value of a
function, the key to this in C++ is copiability: in order to be
an argument or a return value, something must be copiable.
Although by default, C++ uses value semantics and copies, it's
possible in a class to inhibit copiability: among the classes in
the standard library, for example, you cannot copy istream,
ostream or type_info. (There are lot's of things that you
probably don't want copied, that have "identity".) On the other
hand, although not an object, references are copiable, so you
can use references as function arguments and return types.

> hopefully these questions aren't too retarded.


Just the opposite. They involve issues that can be very, very
complex at times: the distinction between copy and reference
semantics, object lifetime, etc.

> unfortunately my c++ instructor was something of a
> chucklehead; i never managed to see the big picture,


The problem is that learning a "language" almost always tends to
concentrate on the details, because languages are so full of
them, and they are different from one language to the next.
Where as the big picture is largely language independent:
distinctions between whether an object is a value or an entity
(i.e. whether its identity is significant), for example, or
object lifetime. All too often, such essential issues tend to
get lost in the details.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
newbarker@gmail.com
Guest
Posts: n/a
 
      09-16-2008
On 15 Sep, 22:21, Sean McIlroy <(E-Mail Removed)> wrote:
> *) are class-instances first class, in the sense of being permissible
> arguments for a function and also permissible return-values for a
> function?


Instances of classes are, yes.

> *) same question as above, except substitute "pointers" for "class-
> instances"


I'm not overly familiar with the terminology but I know how these
things work so can say the pointers themselves can be copied, assigned
to, etc just like an int or char can, so are first class. The objects
the pointers point to do not get copied when you copy the pointer.

I was reading the Modern C++ Design book last night by Andrei
Alexandrescu (second print). Section 7.2. There's a comparison between
simple pointers and smart pointers and he says:

"An object with value semantics is an object that you can copy and
assign to. Type int is the perfect example of a first-class object
....
Consequently pointers to allocated objects do not have value semantics
- you cannot copy and assign to them at will."

So could you define what you mean by first class, as pointers may or
may not be first class depending on your definition.

Regards,

Pete
 
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
Editorial: Taking the SLI Plunge – Part 2 @ Virtual-Hideout Silverstrand Front Page News 0 02-20-2006 10:35 PM
Getting a new DVD recorder/player (taking the plunge!) The Man Behind The Curtain DVD Video 18 08-03-2005 01:23 PM
Taking the Plunge with VoIP Silverstrand Front Page News 0 07-01-2005 07:15 PM
Taking the plunge... Ripley7173 Digital Photography 0 11-10-2003 12:36 AM
Taking the plunge - first .net question D. Shane Fowlkes ASP .Net 2 11-06-2003 05:52 PM



Advertisments