473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Aug 23 '06 #1
9 1284
lu**********@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?
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
Aug 23 '06 #2
In article <11*********************@b28g2000cwb.googlegroups. com>,
<lu**********@googlemail.comwrote:
>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?
Aug 23 '06 #3
In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>In article <11*********************@b28g2000cwb.googlegroups. com>,
<lu**********@googlemail.comwrote:
>>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?
Aug 23 '06 #4
In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>>In article <11*********************@b28g2000cwb.googlegroups. com>,
<lu**********@googlemail.comwrote:
>>>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?
Aug 23 '06 #5
In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>>In article <ec**********@panix3.panix.com>,
Greg Comeau <co****@comeaucomputing.comwrote:
>>>In article <11*********************@b28g2000cwb.googlegroups. com>,
<lu**********@googlemail.comwrote:
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?
Aug 23 '06 #6
"Greg Comeau" <co****@panix.comwrote in message
news:ec**********@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

Aug 23 '06 #7
In article <ec*********@cliff.xsj.xilinx.com>,
Philip Potter <ph***********@xilinx.comwrote:
>"Greg Comeau" <co****@panix.comwrote in message
news:ec**********@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?
Aug 23 '06 #8
lu**********@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
Aug 23 '06 #9

Julián Albo schreef:
lu**********@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

Aug 24 '06 #10

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

Similar topics

3
by: Amadelle | last post by:
Hi All and thanks in advance, I wanted to know when is a good idea to use a static class (with static constructor) and when to use instance classes? I have read couple of articles on line and...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
5
by: pitdog | last post by:
Hello, I am unable to find information on about this issue so I thought I would post and ask. I have a class that is using the sigleton pattern as it has an internal static instance of...
8
by: maneeshkhare | last post by:
I have a doubt regarding the architecture, and working of the ASP.NET framework. I haven't been able to satisfy myself with any answer. I do understand that for each request for a resource...
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.