considering taking the plunge

S

Sean McIlroy

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
 
R

red floyd

*) 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.
 
S

Sean McIlroy

Yes.  And the same goes for references as well.


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
 
J

James Kanze

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.
 
N

newbarker

*) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top