[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

considering taking the plunge

Sean McIlroy

9/15/2008 9:22:00 PM

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
5 Answers

Victor Bazarov

9/15/2008 9:50:00 PM

0

Sean McIlroy 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?

Yes. You are allowed to return objects from functions, if that's the
correct understanding of your question.

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

Yes, pointers are objects. Even further, you can return references from
functions even though references are not objects.

> 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 good book and you can probably undo most of the damage done by
your instructor.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

red floyd

9/15/2008 9:51:00 PM

0

On Sep 15, 2:21 pm, Sean McIlroy <sean_mcil...@yahoo.com> 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.

Sean McIlroy

9/16/2008 2:07:00 AM

0

On Sep 15, 2:51 pm, red floyd <redfl...@gmail.com> wrote:
> On Sep 15, 2:21 pm, Sean McIlroy <sean_mcil...@yahoo.com> 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

James Kanze

9/16/2008 8:27:00 AM

0

On Sep 15, 11:21 pm, Sean McIlroy <sean_mcil...@yahoo.com> 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:james.kanze@gmail.com
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

newbarker

9/16/2008 2:44:00 PM

0

On 15 Sep, 22:21, Sean McIlroy <sean_mcil...@yahoo.com> 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