James Kanze
9/16/2008 8:27:00 AM
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