| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
23 Aug 2006 08:48:49 AM |
| Object: |
Question about static's |
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
Thanks, Luc
.
|
|
| User: "=?ISO-8859-15?Q?Juli=E1n?= Albo" |
|
| Title: Re: Question about static's |
23 Aug 2006 02:54:58 PM |
|
|
wrote:
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
I think the risk of confusion lies in the bad name of the member function. A
verb is more adequate in general for functions that does some action. If
you call it for example 'create....' few people will think that it creates
an object that already exists.
--
Salu2
.
|
|
|
| User: "" |
|
| Title: Re: Question about static's |
24 Aug 2006 07:38:28 AM |
|
|
Juli=E1n Albo schreef:
luc.holtkamp@googlemail.com wrote:
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
I think the risk of confusion lies in the bad name of the member function=
.. A
verb is more adequate in general for functions that does some action. If
you call it for example 'create....' few people will think that it creates
an object that already exists.
--
Salu2
When I wrote the code, I realized it could also be done with a
constructor taking an int, that might be a better choice. But I wrote
this after chasing down a bug that used such a construction from the Qt
library. Many Qt classes have such static members (such as
QString::fromStdString ()).
I think the argument 'changing a function from member to static breaks
your code' is not very strong, I even want to turn it around: I would
WANT the compiler to find the places where it uses my (new) static with
a member, because it is probably not what I intended anyway.
Same goes for the evaluation 'trick'. No coding guideline should accept
such obfucated constuctions...
But anywhay, I guess it just something we have to live with ;-) I think
I repeat the question in std.c++, see what they think
Luc
.
|
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Question about static's |
23 Aug 2006 08:57:15 AM |
|
|
wrote:
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
The 'why' questions should be addressed in 'comp.std.c++'. My answer
(a guess, really) would be "why not?" (although answering with a question
is not always polite). The code is misleading not becuase 'fromInt' is
a static member. It's because you might not have all the information.
What if 'fromInt' *were* non-static, but did nothing to the object or even
with the information contained in the object? Wouldn't it be misleading
as well? So, static or non-static is really of no consequence.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Question about static's |
23 Aug 2006 09:09:41 AM |
|
|
In article <1156340929.278747.11550@b28g2000cwb.googlegroups.com>,
<luc.holtkamp@googlemail.com> wrote:
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
It used to not be allowed, in versions of C++ before Standard C++
that is, for a reason like you say. I can't recall the situation
that caused it to become allowed, but I seem to recall it being
a strong reason that some people felt it should be like other member
functions, and probably had something to do with templates. Anyway,
yes, there may be a difference in observable side effects by using
the object to get to some static member's vs not doing so.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Question about static's |
23 Aug 2006 09:11:54 AM |
|
|
In article <echnj5$as1$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <1156340929.278747.11550@b28g2000cwb.googlegroups.com>,
<luc.holtkamp@googlemail.com> wrote:
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
It used to not be allowed, in versions of C++ before Standard C++
that is, for a reason like you say. I can't recall the situation
that caused it to become allowed, but I seem to recall it being
a strong reason that some people felt it should be like other member
functions, and probably had something to do with templates. Anyway,
yes, there may be a difference in observable side effects by using
the object to get to some static member's vs not doing so.
Oops, should say "recall it NOT being"
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Question about static's |
23 Aug 2006 09:33:54 AM |
|
|
In article <echnna$mg9$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <echnj5$as1$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <1156340929.278747.11550@b28g2000cwb.googlegroups.com>,
<luc.holtkamp@googlemail.com> wrote:
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
It used to not be allowed, in versions of C++ before Standard C++
that is, for a reason like you say. I can't recall the situation
that caused it to become allowed, but I seem to recall it being
a strong reason that some people felt it should be like other member
functions, and probably had something to do with templates. Anyway,
yes, there may be a difference in observable side effects by using
the object to get to some static member's vs not doing so.
Oops, should say "recall it NOT being"
I just realized that I really put my foot in my mouth here.
To back up: Calling a static member with an object was always allowed.
The difference was that previously the object was not evaluated even
though it had to be sytactically correct. Now, it does get
evaluated. And that sound right. It may not matter much with
A::fromInt(....) but it would if instead you had something like
foo()->fromtInt(...), many people want to see the side effect
of what foo() does to actually occur.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
| User: "Philip Potter" |
|
| Title: Re: Question about static's |
23 Aug 2006 10:11:58 AM |
|
|
"Greg Comeau" <comeau@panix.com> wrote in message
news:echp0i$pfb$1@panix3.panix.com...
To back up: Calling a static member with an object was always allowed.
The difference was that previously the object was not evaluated even
though it had to be sytactically correct. Now, it does get
evaluated. And that sound right. It may not matter much with
A::fromInt(....) but it would if instead you had something like
foo()->fromtInt(...), many people want to see the side effect
of what foo() does to actually occur.
I can't imagine a situation where this would be useful, except for clever
syntactic tricks, and ioccc-like code.
Philip
.
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Question about static's |
23 Aug 2006 10:29:14 AM |
|
|
In article <echr7v$qpk1@cliff.xsj.xilinx.com>,
Philip Potter <philip.potter@xilinx.com> wrote:
"Greg Comeau" <comeau@panix.com> wrote in message
news:echp0i$pfb$1@panix3.panix.com...
To back up: Calling a static member with an object was always allowed.
The difference was that previously the object was not evaluated even
though it had to be sytactically correct. Now, it does get
evaluated. And that sound right. It may not matter much with
A::fromInt(....) but it would if instead you had something like
foo()->fromtInt(...), many people want to see the side effect
of what foo() does to actually occur.
I can't imagine a situation where this would be useful, except for clever
syntactic tricks, and ioccc-like code.
More is coming to light :) One issue is that if you were to
change a non-static member to a static member
you would not want to chace down all the calls/uses to it.
However, any calls with side-effects probably would continue
to want those side-effects. This -- both the new and old behavior
-- is probably one of those things that have an it's a feature,
it's a bug duality to it.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
|
| User: "Greg Comeau" |
|
| Title: Re: Question about static's |
23 Aug 2006 09:36:31 AM |
|
|
In article <echp0i$pfb$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <echnna$mg9$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <echnj5$as1$1@panix3.panix.com>,
Greg Comeau <comeau@comeaucomputing.com> wrote:
In article <1156340929.278747.11550@b28g2000cwb.googlegroups.com>,
<luc.holtkamp@googlemail.com> wrote:
I have a simple question:
A static member can be called with the scope operator:
class A {
public:
A (int a) : m_a (a) {}
static A fromInt (int x) { return A (x); }
private:
int m_a;
};
int main () {
A a = A::fromInt (3); // <-- calling static
return 0;
}
However you can also call it with a member:
int main () {
A a;
a.fromInt (3);
return 0;
}
In the last case, the code is misleading, because the call of the
static has no side-effect on the
object (a static never has). The compiler does not generate any
warnings, so this is a nasty bug to find.
I was wondering: why C++ allows this?
It used to not be allowed, in versions of C++ before Standard C++
that is, for a reason like you say. I can't recall the situation
that caused it to become allowed, but I seem to recall it being
a strong reason that some people felt it should be like other member
functions, and probably had something to do with templates. Anyway,
yes, there may be a difference in observable side effects by using
the object to get to some static member's vs not doing so.
Oops, should say "recall it NOT being"
I just realized that I really put my foot in my mouth here.
To back up: Calling a static member with an object was always allowed.
The difference was that previously the object was not evaluated even
though it had to be sytactically correct. Now, it does get
evaluated. And that sound right. It may not matter much with
A::fromInt(....) but it would if instead you had something like
foo()->fromtInt(...), many people want to see the side effect
of what foo() does to actually occur.
Phewy. "It may not matter much with a.fromInt(...) because
a is just a and no harm tossing it, but..."
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
.
|
|
|
|
|
|
|

|
Related Articles |
|
|