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

static const members

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.