473,545 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Differences between "class::mem ber" & "object.mem ber"

Hello,

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

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

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

C obj;
obj.f(); // "object.mem ber"
}

Can use the syntax "class::mem ber" 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::mem ber"
& "object.member" (or object_pointer->member)?

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

Your Sincerely
Jianhua Li
Jul 22 '05 #1
4 2479
"Jian H. Li" <jo****@xinhuan et.com> wrote...
What's the essential differences between the two ways of "class::mem ber"
& "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::mem ber"
This is not going to compile. You cannot call non-static member function
that way.
C::i; // "class::mem ber"
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.mem ber"
}

Can use the syntax "class::mem ber" 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::mem ber"
& "object.member" (or object_pointer->member)?
Different syntax for different purposes.

Is "class::mem ber" used more frequently than "object.mem ber" 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****@xinhuan et.com> wrote in message
news:c9******** *************** ***@posting.goo gle.com
Hello,

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

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

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

C obj;
obj.f(); // "object.mem ber"
}

Can use the syntax "class::mem ber" 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::mem ber" & "object.member" (or object_pointer->member)?

Is "class::mem ber" used more frequently than "object.mem ber" 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)(v oid);
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(c list.begin(), clist.end(), std::mem_fun_re f(&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
6714
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 with "first-class airplane ticket". I just find the name a bit strange. Also, if there are first-class objects, what would the second-class objects or...
3
2076
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
1841
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 AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
5
3326
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
2166
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 (internal error). See the following example program, a CLR console app. Of course the obvious workaround is to refrain from using "Equals". But...
2
2503
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 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing...
5
2576
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 determine wether a class contains a member. My understanding is that if a template function has an error during resolution of the function types, it is...
28
2821
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 apply. Can somebody tell me why? Thanks, Jess
4
1618
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 to leave, if possible, refs inst->m_member in the code, but make compiler translate it to inst->Get() invisily, and without using macros. Let's...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.