472,958 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

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
Jul 22 '05 #1
4 2385
"Jian H. Li" <jo****@xinhuanet.com> 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...
Jul 22 '05 #2
"Jian H. Li" <jo****@xinhuanet.com> wrote in message
news:c9**************************@posting.google.c om
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)
Jul 22 '05 #3
> [...]
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
Jul 22 '05 #4
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. (hi****@comcast.net)

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
3
by: Randy Yates | last post by:
Hi, Is there a way to write a class member function that does not require an instantiation of the object to be invoked? For example, class MYCLASS { bool CheckIfMYCLASS(string &teststring);...
3
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
2
by: Phil Davidson | last post by:
/* (Posted to microsoft.public.dotnet.languages.vc) In C++/CLI under Visual Studio 2005, creating an "enum class" member called "Equals" can cause compiler error C1060 (out of memory) and C1063...
2
by: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: viki | last post by:
We have zillion of instances inf instance->m_member in the code. We are going introduce the 'accessors' Get() and Set() for m_member, and m_member going private (and renamed, too). We would like...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.