Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > [Newbie Question] Static members

Reply
Thread Tools

[Newbie Question] Static members

 
 
Manuel
Guest
Posts: n/a
 
      12-26-2005
Hi!
Anyone can explain me why this work:

--------------------------
class Rect
{
public:
Rect()
{num ++;}

~Rect()
{num --;}

static void pippo(){num ++;};

protected:
static int num;

};
int num = 0;

main(){}
-------------------------

and this not work?

-------------------------
class Rect
{
public:
Rect()
{num ++;}

~Rect()
{num --;}

static void pippo();

protected:
static int num;

};
int num = 0;
void Rect:ippo() {num ++;}
main(){}

---------------------------

Thanks!
 
Reply With Quote
 
 
 
 
Marchello
Guest
Posts: n/a
 
      12-26-2005
> and this not work?
> -------------------------
> class Rect
> {
> public:
> Rect()
> {num ++;}
>
> ~Rect()
> {num --;}
>
> static void pippo();
>
> protected:
> static int num;
>
> };
> int num = 0; // Maybe errors occurs here ?!
> void Rect:ippo() {num ++;}
> main(){}


try to write:
int Rect::num = 0;


 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      12-26-2005
"Manuel" <(E-Mail Removed)> wrote in message
news:43afb6e7$0$1065$(E-Mail Removed). ..
> Hi!
> Anyone can explain me why this work:
>
> --------------------------
> class Rect
> {
> public:
> Rect()
> {num ++;}
>
> ~Rect()
> {num --;}
>
> static void pippo(){num ++;};
>
> protected:
> static int num;
>
> };
> int num = 0;
>
> main(){}
> -------------------------
>
> and this not work?
>
> -------------------------
> class Rect
> {
> public:
> Rect()
> {num ++;}
>
> ~Rect()
> {num --;}
>
> static void pippo();
>
> protected:
> static int num;
>
> };
> int num = 0;
> void Rect:ippo() {num ++;}
> main(){}
>
> ---------------------------
>
> Thanks!


Actually, it's fairly simple. Rect:ippo() is static. Which means it's
only supposed to be used on static variables. They have no this pointer.
Neighter of those should work.

change num to static and it should work.

static int num;

then outside the class

int Rect::num = 0;

I can not tell you why your first version compiled. It shouldn't of as far
as I'm aware.


 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      12-26-2005
Marchello wrote:

>
> try to write:
> int Rect::num = 0;
>
>



It work!
Thanks you very much!
 
Reply With Quote
 
majianan
Guest
Posts: n/a
 
      12-26-2005
I have seen the first program in the <Effective C++> : Item 14:Make
sure base classes have virtual destructors.

 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      12-26-2005
majianan wrote:
> I have seen the first program in the <Effective C++> : Item 14:Make
> sure base classes have virtual destructors.
>


What's <Effective C++> ?
A template for debug?

 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      12-26-2005
Jim Langston wrote:

> change num to static and it should work.
>
> static int num;


This was already done...or you mean I should write this in some other
places?

>
> then outside the class
>
> int Rect::num = 0;



Yes. This was the error!
Thanks,

Manuel
 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-26-2005
On Mon, 26 Dec 2005 12:55:05 +0100, Manuel
<(E-Mail Removed)> wrote:

>majianan wrote:
>> I have seen the first program in the <Effective C++> : Item 14:Make
>> sure base classes have virtual destructors.
>>

>
>What's <Effective C++> ?
>A template for debug?


One of several very useful books by Scott Meyers.

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Manuel
Guest
Posts: n/a
 
      12-26-2005
Bob Hairgrove wrote:

>
> One of several very useful books by Scott Meyers.
>



Ooops...sorry for stupid question...
 
Reply With Quote
 
bvatsa@gmail.com
Guest
Posts: n/a
 
      12-26-2005
U must use Class scope, while acessing Static class members,...
I think u need to brush-up c++ once...








Manuel wrote:
> Bob Hairgrove wrote:
>
> >
> > One of several very useful books by Scott Meyers.
> >

>
>
> Ooops...sorry for stupid question...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments