By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,124 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,124 IT Pros & Developers. It's quick & easy.

Calling static member function through object instance

P: n/a
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?

tia,
Joost Ronkes Agerbeek
Jul 22 '05 #1
Share this Question
Share on Google+
30 Replies


P: n/a
Joost Ronkes Agerbeek wrote:
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?


Static member functions primarily follow the invocation convention for
member functions. However as they may be invoked without constructing
any object the extra syntax (qualified id) is provided for this case.

Regards,
Janusz

Jul 22 '05 #2

P: n/a

"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:40*********************@news.xs4all.nl...
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?


I think the question might be answered if you asked a different question.
If I were to ask you why it should NOT be allowed, what would you say?
Jul 22 '05 #3

P: n/a
> I think the question might be answered if you asked a different question.
If I were to ask you why it should NOT be allowed, what would you say? I would say that, were it not allowed, it would always be clear whether a
call was to a static member or a non-static member.

Part of what brought up this question for me was a passage in A Programmer's
Introduction to C# by Eric Gunnerson regarding calls to static methods
(p.66): "This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."

By the way, I sent a reply to Janusz, but accidentely sent it to him in
person instead of to the newsgroup. Here is my reply and his reply to that
reply. (I removed the offending button in my newsreader ;-).)

Thanks for your insights.

Joost

Joost Ronkes Agerbeek wrote: Isn't it the other way around? Static member functions operate on the class, not the object, so you have to write object::static_function() but for
convenience you can also write instance.static_function(). I know C# doesn't allow invoking a member function on an object instance: you have to use the class so that it is always clear you are calling a static member function.
This triggered my original question: if there is an explicit reason the
designers of C# don't allow static member invocation through an instance,
then the designers of C++ must also have a reason why they do allow static
member invocation through an instance, but what is it?


Hard to speak in the name of C++ designers, however in the C++ standard
(9.4/2) one may find the following statement:

"A static member s of class X may be referred to using the qualified-id
expression X::s; it is not necessary to use the class member access
syntax. (5.2.5)"

5.2.5 says about . -> type access

Some paragraphs before discuss class member access in general and from
that perspective one may think of static members as a special case
following the general concept as close as possible.
AFAIK static member functions were not present in C++ from the beginning
and were added somewhat later. So they were expected not to introduce
too many changes.

So I think it may be the reason of differences between C++ (and Java I
think) and C#. The first attitude focuses on keeping language concepts
consistent, fitting static member functions in the scope of member
functions and the second keeps clean programming style in mind.

Regards,
Janusz



Jul 22 '05 #4

P: n/a

"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:40*********************@news.xs4all.nl...
I think the question might be answered if you asked a different question. If I were to ask you why it should NOT be allowed, what would you say?
I would say that, were it not allowed, it would always be clear whether a
call was to a static member or a non-static member.

Part of what brought up this question for me was a passage in A Programmer's Introduction to C# by Eric Gunnerson regarding calls to static methods
(p.66): "This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."


Bjarne Stroustrup wrote a book about the design choices for the C++
language - you might find a definitive answer there. I've thought for about
5 seconds and don't have an idea about a case where that confusion might
cause a problem, but there might well be one.
Jul 22 '05 #5

P: n/a
Joost Ronkes Agerbeek wrote:
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?
It is *not* allowed.
Is it just convenience?


Jul 22 '05 #6

P: n/a
It is allowed. For convenience is probably the best answer.

Example:
#include <iostream>

class A
{
public:
static void foo() { std::cout << "foobar" << std::endl; };
};

int
main()
{
A a;
a.foo();
}

Output: foobar

E. Robert Tisdale wrote:
Joost Ronkes Agerbeek wrote:
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?

It is *not* allowed.
Is it just convenience?



Jul 22 '05 #7

P: n/a
Even the following code works!

#include <cstdio>
class A {
public:
static void foo() { printf("Hello world.\n"); };
};
int main() {
A* a[10];
int i;
for(i=0; i<10; i++)
a[i] = 0;
a[5]->foo();
a[50]->foo();
}

Jul 22 '05 #8

P: n/a
Dario wrote:

Even the following code works!
But *this* code is illegal.

#include <cstdio>
class A {
public:
static void foo() { printf("Hello world.\n"); };
};
int main() {
A* a[10];
int i;
for(i=0; i<10; i++)
a[i] = 0;
a[5]->foo();
a[50]->foo();


you are dereferencing a 0-pointer.
And this is always a no-no, even if
your specific system does *not* crash.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9

P: n/a
Karl Heinz Buchegger wrote:
Dario wrote:
Even the following code works!

But *this* code is illegal.

#include <cstdio>
class A {
public:
static void foo() { printf("Hello world.\n"); };
};
int main() {
A* a[10];
int i;
for(i=0; i<10; i++)
a[i] = 0;
a[5]->foo();
a[50]->foo();

you are dereferencing a 0-pointer.
And this is always a no-no, even if
your specific system does *not* crash.


No !

When I evaluate
a[5]->foo()
a[5] is 0 but this does not matter
because foo is a static member;
a[50]->foo()
a[50] is whatever but this does not matter
because foo is a static member.

So my program is legal and works for every C++ compiler.

- Dario

Jul 22 '05 #10

P: n/a
Dario wrote:
you are dereferencing a 0-pointer.
And this is always a no-no, even if
your specific system does *not* crash.


No !


Yes.
Read your code.

When I evaluate
a[5]->foo()
a[5] is 0 but this does not matter
because foo is a static member;
a[50]->foo()
a[50] is whatever but this does not matter
because foo is a static member.

So my program is legal and works for every C++ compiler.


It is not legal, read the standard.
It doesn't matter if your compiler doesn't need
to dereference the pointer to call this function
under the hood. Your source code contains the
dereference and hence it is an illegal program.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11

P: n/a
In article <bu**********@grillo.cs.interbusiness.it>,
da***@despammed.com says...

[ ... ]
you are dereferencing a 0-pointer.
And this is always a no-no, even if
your specific system does *not* crash.
No !

When I evaluate
a[5]->foo()
a[5] is 0 but this does not matter
because foo is a static member;


This is utterly irrelevant -- 5.2.5/1 says "the expression before the
dot or arrow is evaluated". Footnote 58 explicitly states that "This
evaluation happens even if the result is unnecessary to determine the
value of the entire postfix expression, for example if the id-expression
denotes a static member."

As such, you're assured that this dereferences a null pointer, which
gives undefined behavior.
So my program is legal and works for every C++ compiler.


Your program contains undefined behavior. If your definition of
"works" includes the possibility of crashing the computer, reformatting
the hard drive or making demons fly out of your nose, then you're right:
it works for every C++ compiler. Anybody who has a stricter definition
of work will want to avoid this construct.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #12

P: n/a
On Thu, 15 Jan 2004 11:11:46 -0500, "jeffc" <no****@nowhere.com>
wrote:
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:40*********************@news.xs4all.nl...
> I think the question might be answered if you asked a differentquestion. > If I were to ask you why it should NOT be allowed, what would you say?

I would say that, were it not allowed, it would always be clear whether a
call was to a static member or a non-static member.

Part of what brought up this question for me was a passage in A

Programmer's
Introduction to C# by Eric Gunnerson regarding calls to static methods
(p.66): "This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."


Bjarne Stroustrup wrote a book about the design choices for the C++
language - you might find a definitive answer there. I've thought for about
5 seconds and don't have an idea about a case where that confusion might
cause a problem, but there might well be one.


It caught me out (shockingly recently), somehow I managed to miss the
whole call via instance mechanism for the 8+ years I've used c++, I
always used the :: form and I never saw anyone else do otherwise....

class BlahBlah
{
public:
BlahBlah& Instance(); // some sort of singleton i/f

private:
static void Eot( Client* client, int flags, void* uopt )
{
// Mistaken assumption: overload resolution calls
// the non-static member ( due to implicit 'this' )
// Actual behaviour: Recursive call & hang!
Instance().Eot( client, flags, uopt );
}
void Eot( Client* client, int flags, void* uopt );
};

In case you're wondering this was just code to interface
to a 3rd party C lib that used callbacks (yuk). Usually I would have
had different names for the two Eot() functions, but not this
time for some reason.

I would have thought the compiler might have determined
the Eot call to be amibiguous, but it didn't ( Compiler was a gcc
2.95.x derivitive.). Does a static member function rate higher
than an ordinary member function in overload resolution?

Jul 22 '05 #13

P: n/a
Andy Buchanan <ne**@globbits.co.uk> wrote in message news:<qu********************************@4ax.com>. ..
On Thu, 15 Jan 2004 11:11:46 -0500, "jeffc" <no****@nowhere.com>
wrote:
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message
news:40*********************@news.xs4all.nl...
> I think the question might be answered if you asked a different question. > If I were to ask you why it should NOT be allowed, what would you say? I would say that, were it not allowed, it would always be clear whether a
call was to a static member or a non-static member.

Part of what brought up this question for me was a passage in A Programmer's Introduction to C# by Eric Gunnerson regarding calls to static methods
(p.66): "This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."
Bjarne Stroustrup wrote a book about the design choices for the C++
language - you might find a definitive answer there. I've thought for about
5 seconds and don't have an idea about a case where that confusion might
cause a problem, but there might well be one.


The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?

It caught me out (shockingly recently), somehow I managed to miss the
whole call via instance mechanism for the 8+ years I've used c++, I
always used the :: form and I never saw anyone else do otherwise....

class BlahBlah
{
public:
BlahBlah& Instance(); // some sort of singleton i/f

private:
static void Eot( Client* client, int flags, void* uopt )
{
// Mistaken assumption: overload resolution calls
// the non-static member ( due to implicit 'this' )
// Actual behaviour: Recursive call & hang!
Instance().Eot( client, flags, uopt );
}
void Eot( Client* client, int flags, void* uopt );
};

In case you're wondering this was just code to interface
to a 3rd party C lib that used callbacks (yuk). Usually I would have
had different names for the two Eot() functions, but not this
time for some reason.

I would have thought the compiler might have determined
the Eot call to be amibiguous, but it didn't ( Compiler was a gcc
2.95.x derivitive.). Does a static member function rate higher
than an ordinary member function in overload resolution?


No. That declaration is illegal. From the standard:

"[class.static.mfct] 9.4.1 Static member functions

There shall not be a static and a nonstatic member function with the
same name
and the same parameter types (13.1)."

This has always been the rule. Without that rule, people could get
into all kinds of nasty problems.

It is always most anoying when a compiler fails to implement a clear
and simple language rule.

- Bjarne Stroustrup; http://www.research.att.com/~bs
Jul 22 '05 #14

P: n/a
> > When I evaluate
a[5]->foo()
a[5] is 0 but this does not matter
because foo is a static member;


This is utterly irrelevant -- 5.2.5/1 says "the expression before the
dot or arrow is evaluated". Footnote 58 explicitly states that "This
evaluation happens even if the result is unnecessary to determine the
value of the entire postfix expression, for example if the id-expression
denotes a static member."

As such, you're assured that this dereferences a null pointer, which
gives undefined behavior.


Your quote is irrelevant. "the expression before the dot or arrow"
is "a[5]". This is easy to evaluate: 0
You will have to look elsewhere in the Standard to find a definitive
answer.
Next question: what is the difference, if any, between:
a[5]->foo()
(*a[5]).foo()
Jul 22 '05 #15

P: n/a
On 19 Jan 2004 07:01:14 -0800, bs@research.att.com (Bjarne Stroustrup)
wrote:
Andy Buchanan <ne**@globbits.co.uk> wrote in message news:<qu********************************@4ax.com>. .. <!--Snipped for brevity-->
It caught me out (shockingly recently), somehow I managed to miss the
whole call via instance mechanism for the 8+ years I've used c++, I
always used the :: form and I never saw anyone else do otherwise....

class BlahBlah
{
public:
BlahBlah& Instance(); // some sort of singleton i/f

private:
static void Eot( Client* client, int flags, void* uopt )
{
// Mistaken assumption: overload resolution calls
// the non-static member ( due to implicit 'this' )
// Actual behaviour: Recursive call & hang!
Instance().Eot( client, flags, uopt );
}
void Eot( Client* client, int flags, void* uopt );
};

In case you're wondering this was just code to interface
to a 3rd party C lib that used callbacks (yuk). Usually I would have
had different names for the two Eot() functions, but not this
time for some reason.

I would have thought the compiler might have determined
the Eot call to be amibiguous, but it didn't ( Compiler was a gcc
2.95.x derivitive.). Does a static member function rate higher
than an ordinary member function in overload resolution?


No. That declaration is illegal. From the standard:

"[class.static.mfct] 9.4.1 Static member functions

There shall not be a static and a nonstatic member function with the
same name
and the same parameter types (13.1)."

This has always been the rule. Without that rule, people could get
into all kinds of nasty problems.


As indeed I did. I shant be making this mistake again even if the
compiler lets me. Thanks for the pointer.
It is always most anoying when a compiler fails to implement a clear
and simple language rule.
Alas the compiler in question has allowed a number of other
incorrect constructs. As a further alas, I'm pretty much stuck with
it for the rest of the project (and probably the next). <sigh>

- Bjarne Stroustrup; http://www.research.att.com/~bs


Jul 22 '05 #16

P: n/a
"Bjarne Stroustrup" <bs@research.att.com> wrote in message
news:18**************************@posting.google.c om...
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?


Thanks for the answer!

The C# design team thinks the user cares because it makes their code more
readable. I'm not partial to either view, I was merely curious.

Joost Ronkes Agerbeek
Jul 22 '05 #17

P: n/a
Bjarne Stroustrup <bs@research.att.com> wrote:
[...]
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?
For the same reason the users should care
about whether a member function is 'const'
or not: One will not change the object it
is called for while the other one might.
The only way to find out about this now
is from looking at the signature -- which
is not really all that bad. However, _if_
only the special syntax for static member
functions was allowed, it would be easier
to avoid that Murphy guy.
[...]
- Bjarne Stroustrup; http://www.research.att.com/~bs

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #18

P: n/a
"Hendrik Schober" <Sp******@gmx.de> wrote in message news:<bu**********@news1.transmedia.de>...
Bjarne Stroustrup <bs@research.att.com> wrote:
[...]
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?


For the same reason the users should care
about whether a member function is 'const'
or not: One will not change the object it
is called for while the other one might.
The only way to find out about this now
is from looking at the signature -- which
is not really all that bad. However, _if_
only the special syntax for static member
functions was allowed, it would be easier
to avoid that Murphy guy.


Would you also like the language prohibit calls of const member
functions for non-const objects? The general idea is to allow safe
operations, such as invoking const member functions on non-const
objects and static member functions for an object, while disallowing
potentially damaging ones, such as invoking a non-const member
function for a const object or a non-static member function without an
object.

I don't think Murphy enters into this.
[...]
- Bjarne Stroustrup; http://www.research.att.com/~bs

Schobi

Jul 22 '05 #19

P: n/a
Andy Buchanan <ne**@globbits.co.uk> wrote > >It is always most anoying
when a compiler fails to implement a clear
and simple language rule.


Alas the compiler in question has allowed a number of other
incorrect constructs. As a further alas, I'm pretty much stuck with
it for the rest of the project (and probably the next). <sigh>


For what it's worth, gcc 3.3.2 doesn't have that problem.

- Bjarne Stroustrup; http://www.research.att.com/~bs

Jul 22 '05 #20

P: n/a
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote in message news:<40*********************@news.xs4all.nl>...
"Bjarne Stroustrup" <bs@research.att.com> wrote in message
news:18**************************@posting.google.c om...
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?


Thanks for the answer!

The C# design team thinks the user cares because it makes their code more
readable. I'm not partial to either view, I was merely curious.

Joost Ronkes Agerbeek


Maybe. it's always hard to guess what a team thinks; usually we only
see what they do. In this case, I suspect that they simply didn't
think of the more general use of p-> (and obj. )

I don't think that (in general or in this case) prohibiting a safe
operations increases readability. If calling p->f() where the value of
p isn't use is bad, then how about calling p->f() and not using p (by
accessing only static members from within f())? Should that be
outlawed also? I think not. In that direction lies serious "nannyism".

- Bjarne Stroustrup; http://www.research.att.com/~bs
Jul 22 '05 #21

P: n/a
Bjarne Stroustrup <bs@research.att.com> wrote:
[...]
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?
For the same reason the users should care
about whether a member function is 'const'
or not: One will not change the object it
is called for while the other one might.
The only way to find out about this now
is from looking at the signature -- which
is not really all that bad. However, _if_
only the special syntax for static member
functions was allowed, it would be easier
to avoid that Murphy guy.


Would you also like the language prohibit calls of const member
functions for non-const objects? The general idea is to allow safe
operations, such as invoking const member functions on non-const
objects and static member functions for an object, while disallowing
potentially damaging ones, such as invoking a non-const member
function for a const object or a non-static member function without an
object.


You are right, the distinction is mostly
important in one way only, and this way
is already blocked.
I guess I didn't think enough about this.
I don't think Murphy enters into this.


Oh, he always finds a way. :)

[...]

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #22

P: n/a
Bjarne Stroustrup wrote:
"Hendrik Schober" <Sp******@gmx.de> wrote in message news:<bu**********@news1.transmedia.de>...
Bjarne Stroustrup <bs@research.att.com> wrote:
[...]
The reason for allowing p->static_member_function(); is exactly not to
require a user to remember if a function is static or not. Why should
the user care, except in the case where he/she wants to call without
an object?


For the same reason the users should care
about whether a member function is 'const'
or not: One will not change the object it
is called for while the other one might.
The only way to find out about this now
is from looking at the signature -- which
is not really all that bad. However, _if_
only the special syntax for static member
functions was allowed, it would be easier
to avoid that Murphy guy.

Would you also like the language prohibit calls of const member
functions for non-const objects? The general idea is to allow safe
operations, such as invoking const member functions on non-const
objects and static member functions for an object, while disallowing
potentially damaging ones, such as invoking a non-const member
function for a const object or a non-static member function without an
object.

I don't think Murphy enters into this.


Forgive my off-topic indulgence....

Perhaps this is a generalization of this rule :

"Avoid imposing arbitrary limitations to the utility of a system"

Far too often I see programmers/system designers that create very
specific designs that limit the usefullness of their labour for short
sighted reasons. Sometimes it is due to limitation of knowledge (which
we are all guilty of - mosly myself). However the most annoying reason
is the lack of logical thinking about a problem (which I am also guilty
as charged).

This is one area where I think the computer science profession still
needs some critical skill building.

Back-on-topic .... With all it's warts, C++ does seem to follow this
rule (on the most part) and it is refreshing to see.

p.s. the (on the most part) is because I still run into issues with the
STL - especially iterators - but that's another long story fo another day.
Jul 22 '05 #23

P: n/a

"Bjarne Stroustrup" <bs@research.att.com> wrote in message
news:18**************************@posting.google.c om...
Maybe. it's always hard to guess what a team thinks; usually we only
see what they do. In this case, I suspect that they simply didn't
think of the more general use of p-> (and obj. ) I'm not guessing. As I said earlier part of what brought up this question
for
me was a passage in A Programmer's Introduction to C# by Eric Gunnerson
regarding calls to static methods (p.66):
"This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."
Since Eric Gunnersion is currently on the C# language design team, I
think it's safe to assume that what he writes was the actual consideration
when making the decision not to allow access to static members through
instances.
I don't think that (in general or in this case) prohibiting a safe
operations increases readability. If calling p->f() where the value of
p isn't use is bad, then how about calling p->f() and not using p (by
accessing only static members from within f())? Should that be
outlawed also? I think not. In that direction lies serious "nannyism".

I can see where you're coming from and I think it's a valid rule of
thumb. Again, I'm not partial to either view. I'm a language user, not
a language designer so I tend to take a language as it is.

Of course I do find these kinds of discussions interesting and useful,
otherwise I wouldn't have brought it up. :-)

Joost Ronkes Agerbeek
Jul 22 '05 #24

P: n/a
Gianni Mariani <gi*******@mariani.ws> wrote
Bjarne Stroustrup wrote:
The general idea is to allow safe
operations, such as invoking const member functions on non-const
objects and static member functions for an object, while disallowing
potentially damaging ones, such as invoking a non-const member
function for a const object or a non-static member function without an
object.


Forgive my off-topic indulgence....

Perhaps this is a generalization of this rule :

"Avoid imposing arbitrary limitations to the utility of a system"


More like a special case of that rule.
Far too often I see programmers/system designers that create very
specific designs that limit the usefullness of their labour for short
sighted reasons. Sometimes it is due to limitation of knowledge (which
we are all guilty of - mosly myself). However the most annoying reason
is the lack of logical thinking about a problem (which I am also guilty
as charged).

This is one area where I think the computer science profession still
needs some critical skill building.
I agree
Back-on-topic .... With all it's warts, C++ does seem to follow this
rule (on the most part) and it is refreshing to see.
At least "C++" conciously tries to. For example, see my design rules
of thumb in "The Design and Evolution of C++"
p.s. the (on the most part) is because I still run into issues with the
STL - especially iterators - but that's another long story fo another day.


If you could express it briefly, it might be an interesting/useful
story.

- Bjarne Stroustrup; http://www.research.att.com/~bs
Jul 22 '05 #25

P: n/a
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> wrote
"Bjarne Stroustrup" <bs@research.att.com> wrote
Maybe. it's always hard to guess what a team thinks; usually we only
see what they do. In this case, I suspect that they simply didn't
think of the more general use of p-> (and obj. )

I'm not guessing. As I said earlier part of what brought up this question
for
me was a passage in A Programmer's Introduction to C# by Eric Gunnerson
regarding calls to static methods (p.66):
"This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."
Since Eric Gunnersion is currently on the C# language design team, I
think it's safe to assume that what he writes was the actual consideration
when making the decision not to allow access to static members through
instances.


Thanks. That's plausible, but not "a safe assumption". Much (most?) of
C# was designed before the the current design team was formed.

I still have no real clue what the "readability problems" were
supposed to be, and I hesitate to guess.

- Bjarne Stroustrup; http://www.research.att.com/~bs
Jul 22 '05 #26

P: n/a
Bjarne Stroustrup wrote:
Gianni Mariani <gi*******@mariani.ws> wrote
....
p.s. the (on the most part) is because I still run into issues with the
STL - especially iterators - but that's another long story fo another day.

If you could express it briefly, it might be an interesting/useful
story.


OK.

issue 1.

When using pointers, 0 is an intersting value because it can indicate
"not pointing to anything". For many types of function templates,
iterators and pointers are nearly (mostly) interchangeable types
(granted - some iterators lack all the features). However, one very
interesting feature is the "nil" or "not pointing to anything" iterator.
issue 2.

Container complexity - I come across the need for multi-faceted
containers (an object being organized in a map and a list - list for
fifo and map for searching). To use the STL you need to create
placeholder objects while if I was to write this by hand, I could make a
more efficient system whereby you could in essance create composite
containers. Admitdedly, this requires more complex interfaces but I
think it's possible to have the current interface and a lower level
interface.
Jul 22 '05 #27

P: n/a
Gianni Mariani wrote:

issue 1.

When using pointers, 0 is an intersting value because it can indicate
"not pointing to anything". For many types of function templates,
iterators and pointers are nearly (mostly) interchangeable types
(granted - some iterators lack all the features). However, one very
interesting feature is the "nil" or "not pointing to anything" iterator.


I don't get that. Are you saying that there should be a nil iterator?
Iterators are usually used to delimit a sequence, or to indicate a
single element, and a nil iterator wouldn't be useful for either. And of
course no standard algorithms or other functions expect such a thing. If
you need an iterator to indicate "no element" you should use
container.end(). Am I misunderstanding something?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #28

P: n/a
Kevin Goodsell wrote:
Gianni Mariani wrote:

issue 1.

When using pointers, 0 is an intersting value because it can indicate
"not pointing to anything". For many types of function templates,
iterators and pointers are nearly (mostly) interchangeable types
(granted - some iterators lack all the features). However, one very
interesting feature is the "nil" or "not pointing to anything" iterator.

I don't get that. Are you saying that there should be a nil iterator?
Iterators are usually used to delimit a sequence, or to indicate a
single element, and a nil iterator wouldn't be useful for either. And of
course no standard algorithms or other functions expect such a thing. If
you need an iterator to indicate "no element" you should use
container.end(). Am I misunderstanding something?


But ... sometimes you don't have access to a container.

Jul 22 '05 #29

P: n/a
Gianni Mariani wrote:
Kevin Goodsell wrote:
Gianni Mariani wrote:

issue 1.

When using pointers, 0 is an intersting value because it can indicate
"not pointing to anything". For many types of function templates,
iterators and pointers are nearly (mostly) interchangeable types
(granted - some iterators lack all the features). However, one very
interesting feature is the "nil" or "not pointing to anything" iterator.


I don't get that. Are you saying that there should be a nil iterator?
Iterators are usually used to delimit a sequence, or to indicate a
single element, and a nil iterator wouldn't be useful for either. And
of course no standard algorithms or other functions expect such a
thing. If you need an iterator to indicate "no element" you should use
container.end(). Am I misunderstanding something?

But ... sometimes you don't have access to a container.


Then what would your iterator refer to? OK, dumb question - you are
talking about an iterator that doesn't refer to anything. But I'm not
sure when such a thing would be useful.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #30

P: n/a
Kevin Goodsell wrote:
Gianni Mariani wrote:
Kevin Goodsell wrote:
Gianni Mariani wrote:
.... Then what would your iterator refer to? OK, dumb question - you are
talking about an iterator that doesn't refer to anything. But I'm not
sure when such a thing would be useful.

-Kevin

Here is an example of a smart "pointer ?" template - one using an
iterator and another using real pointers. It's a "fabricated" example
but similar to a RealWorld(TM) problem I had to solve in a galaxy far
far away.
template <typename w_iter>
struct iter_manager
{
w_iter m_managed_iter;

iter_manager( const w_iter & i_managed_iter )
: m_managed_iter( i_managed_iter )
{
}

iter_manager()
{
}

~iter_manager()
{
// OOPS - no way of knowing if iter is any good.
m_managed_iter->DoneFor();
}

};

template <typename w_pointer>
struct pointer_manager
{
w_pointer m_managed_pointer;

pointer_manager( const w_pointer i_managed_pointer = 0 )
: m_managed_pointer( i_managed_pointer )
{
}

~pointer_manager()
{
if ( i_managed_pointer )
{
m_managed_pointer->DoneFor();
}
}

};

Jul 22 '05 #31

This discussion thread is closed

Replies have been disabled for this discussion.