considering taking the plunge

Discussion in 'C++' started by Sean McIlroy, Sep 15, 2008.

  1. Sean McIlroy

    Sean McIlroy Guest

    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
    Sean McIlroy, Sep 15, 2008
    #1
    1. Advertising

  2. Sean McIlroy

    red floyd Guest

    On Sep 15, 2:21 pm, Sean McIlroy <> 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.
    red floyd, Sep 15, 2008
    #2
    1. Advertising

  3. Sean McIlroy

    Sean McIlroy Guest

    On Sep 15, 2:51 pm, red floyd <> wrote:
    > On Sep 15, 2:21 pm, Sean McIlroy <> 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
    Sean McIlroy, Sep 16, 2008
    #3
  4. Sean McIlroy

    James Kanze Guest

    On Sep 15, 11:21 pm, 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?


    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:
    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
    James Kanze, Sep 16, 2008
    #4
  5. Sean McIlroy

    Guest

    On 15 Sep, 22:21, Sean McIlroy <> 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
    , Sep 16, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randy Thelen
    Replies:
    9
    Views:
    1,019
    Stephen Williams
    Apr 17, 2004
  2. D. Shane Fowlkes

    Taking the plunge - first .net question

    D. Shane Fowlkes, Nov 6, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    318
    D. Shane Fowlkes
    Nov 6, 2003
  3. vincent wehren
    Replies:
    6
    Views:
    396
  4. Jim Cain
    Replies:
    1
    Views:
    199
    Yukihiro Matsumoto
    Jul 18, 2003
  5. Replies:
    18
    Views:
    210
    Dave Angel
    Jan 1, 2013
Loading...

Share This Page