473,385 Members | 1,769 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,385 software developers and data experts.

static const members

Hi,

Is this standard C++:

enum A{a,b,c};

class D
{
static const A f = a; // is this allowed with enumerated types?
};

Thanks

Bond, James Bond.

Jul 23 '05 #1
6 1671
u.********@gmail.com wrote:
Is this standard C++:

enum A{a,b,c};

class D
{
static const A f = a; // is this allowed with enumerated types?
};


Yes. You still need to define it outside the class if it's used elsewhere
in the program. The definition outside the class in that case shall not
have an initialiser.

V
Jul 23 '05 #2
Victor Bazarov wrote:
u.********@gmail.com wrote:
Is this standard C++:

enum A{a,b,c};

class D
{
static const A f = a; // is this allowed with enumerated types?
};


Yes. You still need to define it outside the class if it's used elsewhere
in the program. The definition outside the class in that case shall not
have an initialiser.


Ah, so D::func(){A = D::f; // this is ok
}
but mynamespace::func()
{A = D::f; // this requires a defn
}

Jul 23 '05 #3
u.********@gmail.com wrote:
Victor Bazarov wrote:
u.********@gmail.com wrote:
Is this standard C++:

enum A{a,b,c};

class D
{
static const A f = a; // is this allowed with enumerated types?
};
Yes. You still need to define it outside the class if it's used elsewhere
in the program. The definition outside the class in that case shall not
have an initialiser.

Ah, so D::func(){A = D::f; // this is ok


That is a syntax error.
}
No. It would be OK if D::func()'s body were inside the definition of 'D'.
As soon as you try using it outside of the curly braces that surround the
definition of 'D', the data member has to be defined at the namespace
level.
but mynamespace::func()
{A = D::f; // this requires a defn
}


V
Jul 23 '05 #4
On Fri, 22 Jul 2005 21:51:50 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:
u.********@gmail.com wrote:
Is this standard C++:
enum A{a,b,c};
class D
{
static const A f = a; // is this allowed with enumerated types?
};


Yes. You still need to define it outside the class if it's used
elsewhere
in the program.


More precisely, if the address of a is taken somewhere. If it's not, you
don't need to.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #5


Maxim Yegorushkin wrote:
On Fri, 22 Jul 2005 21:51:50 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:
u.********@gmail.com wrote:
Is this standard C++:
enum A{a,b,c};
class D
{
static const A f = a; // is this allowed with enumerated types?
};


Yes. You still need to define it outside the class if it's used
elsewhere
in the program.


More precisely, if the address of a is taken somewhere. If it's not, you
don't need to.


Can you explain why my compiler seemed to put an external reference
into an object file to D::f???? I would have expected it to replace the
usage of D::f with a...

Thanks

Jul 24 '05 #6
On Sun, 24 Jul 2005 09:54:43 +0400, <u.********@gmail.com> wrote:
Maxim Yegorushkin wrote:
On Fri, 22 Jul 2005 21:51:50 +0400, Victor Bazarov
<v.********@comAcast.net> wrote:
> u.********@gmail.com wrote:
>> Is this standard C++:
>> enum A{a,b,c};
>> class D
>> {
>> static const A f = a; // is this allowed with enumerated types?
>> };
>
> Yes. You still need to define it outside the class if it's used
> elsewhere
> in the program.


More precisely, if the address of a is taken somewhere. If it's not, you
don't need to.


Can you explain why my compiler seemed to put an external reference
into an object file to D::f???? I would have expected it to replace the
usage of D::f with a...


Because you take its address somewhere. Either by operator& or by
initializing a reference with D::f.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 24 '05 #7

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

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
2
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
4
by: Rakesh Sinha | last post by:
I am having this code here. static const float PI = 3.14159; static const float INC = 0.4f * PI; When I compile my program, I get the following error, error: `MyClass::PI' cannot appear...
1
by: Old Wolf | last post by:
I tried this code: #include <iostream> #include <string> template<typename T> struct enum_properties { static const long max; static const std::string name;
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...

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.