Differences between "class::member" & "object.member"

Discussion in 'C++' started by Jian H. Li, Feb 13, 2004.

  1. Jian H. Li

    Jian H. Li Guest

    Hello,

    What's the essential differences between the two ways of "class::member"
    & "object.member"(or object_pointer->member)?

    class C{
    public:
    void f() {}
    int i;
    };

    int main(){
    C::f(); // "class::member"
    C::i; // "class::member"

    C obj;
    obj.f(); // "object.member"
    }

    Can use the syntax "class::member" to invoke a class member(non static)?
    (Obviously true, if the class member is qualified by static. I think)

    What's the essential differences between the two ways of "class::member"
    & "object.member"(or object_pointer->member)?

    Is "class::member" used more frequently than "object.member" in program text?

    Your Sincerely
    Jianhua Li
    Jian H. Li, Feb 13, 2004
    #1
    1. Advertising

  2. "Jian H. Li" <> wrote...
    > What's the essential differences between the two ways of "class::member"
    > & "object.member"(or object_pointer->member)?


    "Essential"? They have different purposes. The former specifies access
    to the member in terms of class, the other in terms of class instance.

    >
    > class C{
    > public:
    > void f() {}
    > int i;
    > };
    >
    > int main(){
    > C::f(); // "class::member"


    This is not going to compile. You cannot call non-static member function
    that way.

    > C::i; // "class::member"


    Again, it's not going to compile. C::i does not represent a valid
    expression.
    You need an instance of the class to access non-static members.

    >
    > C obj;
    > obj.f(); // "object.member"
    > }
    >
    > Can use the syntax "class::member" to invoke a class member(non static)?


    Yes, but only from within another member function (where 'this' is assumed).

    > (Obviously true, if the class member is qualified by static. I think)


    Obviously nonsense, since you just put the 'non-static' requirement in the
    question, so you cannot answer the question violating that requirement.

    >
    > What's the essential differences between the two ways of "class::member"
    > & "object.member"(or object_pointer->member)?


    Different syntax for different purposes.

    >
    > Is "class::member" used more frequently than "object.member" in program

    text?

    Is 'for' used more frequently than 'if'? Are there more men named John
    than men named Michael? What a useless question...
    Victor Bazarov, Feb 13, 2004
    #2
    1. Advertising

  3. Jian H. Li

    John Carson Guest

    "Jian H. Li" <> wrote in message
    news:
    > Hello,
    >
    > What's the essential differences between the two ways of
    > "class::member" & "object.member"(or object_pointer->member)?
    >
    > class C{
    > public:
    > void f() {}
    > int i;
    > };
    >
    > int main(){
    > C::f(); // "class::member"
    > C::i; // "class::member"
    >
    > C obj;
    > obj.f(); // "object.member"
    > }
    >
    > Can use the syntax "class::member" to invoke a class member(non
    > static)? (Obviously true, if the class member is qualified by static.
    > I think)
    >
    > What's the essential differences between the two ways of
    > "class::member" & "object.member"(or object_pointer->member)?
    >
    > Is "class::member" used more frequently than "object.member" in
    > program text?
    >
    > Your Sincerely
    > Jianhua Li



    Non-static members can only be accessed via an object of the class, either
    directly or indirectly (access indirectly via an object of the class occurs
    when a non-static member is accessed within a non-static member function of
    the same class). Thus you CANNOT access a member with C::f() or C::i inside
    main().

    When accessing a member via an object of the class, you are allowed to used
    a fully qualified name if you want, e.g., given

    C obj;

    both

    obj.f();

    and

    obj.C::f();

    are allowed. A more likely use of this syntax is when you have a Base and
    Derived class, both of which define f(). From the Derived class, you might
    use Base::f() in order to get the Base class version of the function, e.g.,

    Derived d;

    d.f(); // gives Derived class version of f()
    d.Derived::f(); // gives Derived class version of f()
    d.Base::f(); // gives Base class version of f()

    The same basic mechanism works within member functions of Derived.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Feb 13, 2004
    #3
  4. > [...]
    > Is 'for' used more frequently than 'if'? Are there more men named John
    > than men named Michael? What a useless question...


    There there. No need to frown, is there?

    Kristian
    Kristian Dupont, Feb 13, 2004
    #4
  5. Jian Li provided the following class:
    class C {
    public:
    void f() {}
    int i;
    };

    John Carson wrote:
    > Non-static members can only be accessed via an object of the class, either
    > directly or indirectly (access indirectly via an object of the class occurs
    > when a non-static member is accessed within a non-static member function of
    > the same class). Thus you CANNOT access a member with C::f() or C::i inside
    > main().


    Sometimes you may want to access the method C::f itself. First, here's
    how you can do it explicitly, using a typedef for clarity:

    typedef void (C::*CMethod)(void);
    CMethod method = &C::f;

    Now method is referring to the C::f method. To apply it to an instance
    directly, use the .* or ->* operators:

    C x;
    (x.*method)();

    This code invokes x.f().

    Here's a practical example, in which clist is an STL container of C
    instances (e.g., std::vector<C>, std::list<C>, etc.):

    std::for_each(clist.begin(), clist.end(), std::mem_fun_ref(&C::f));

    You can thus apply method f conveniently to every C instance in clist
    without typing much.

    The notation is similar for &C::i. You won't use it unless, say, you're
    writing a templatized adapter for a family of C-language structures.
    When the time comes, you'll think, "I wish I could just access an
    arbitrary C field of a given type. Oh, wait, I can!"

    -- Jonathan T. Higa, Ph.D. ()
    Jonathan Higa, Feb 13, 2004
    #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. Rick
    Replies:
    0
    Views:
    954
  2. Rick
    Replies:
    0
    Views:
    875
  3. E11
    Replies:
    1
    Views:
    4,755
    Thomas Weidenfeller
    Oct 12, 2005
  4. Home_Job_opportunity
    Replies:
    0
    Views:
    498
    Home_Job_opportunity
    Jan 8, 2009
  5. Home_Job_opportunity
    Replies:
    0
    Views:
    585
    Home_Job_opportunity
    Jan 14, 2009
Loading...

Share This Page