Skip to main content
GameDev.net gamedev.net
🔒 Locked

static class members

Started by Luctus Feb 26, 2003 at 9:51 AM 5 replies 350+ views
Original Post
Luctus
Luctus

        
class cTest
{
public:
	int obj;
};

int main( void )
{
	cTest test1;
	test1.obj = 5;

	return 0;
}
   
compiles and runs as expected.
  
class cTest
{
public:
	static int obj;
};

int main( void )
{
	cTest test1;
	test1.obj = 5;

	return 0;
}
        
won't compile though
quote:
Compiling... main.cpp H:\dokument\test\main.cpp(12) : warning C4101: 'test1' : unreferenced local variable Linking... main.obj : error LNK2001: unresolved external symbol "public: static int cTest::obj" (?obj@cTest@@2HA) Debug/main.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. main.exe - 2 error(s), 1 warning(s)
Can someone please explain why?
-Luctus Codito, Ergo Sum (Courtesy of ThinkGeek) [Mail][DreggSoft] [edited by - Luctus on February 26, 2003 10:53:24 AM]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Nanven
Nanven
Under your class put the line:

cTest::int obj=0;

Makes it work.
-Going to Westwood College for my Associates in Computer Programming
Nanven
Nanven
Make sure you don''t put static when doing that though, you WANT:

cTest::int obj=0;

Exactly. No changes, don''t worry, it''ll make it work like you''re wanting it to.
-Going to Westwood College for my Associates in Computer Programming
Luctus
Luctus
you mean

int cTest::obj = 0;

right?

Yes, I know it works when I do that, I was wondering _why_ it doesn''t work the other way.


-Luctus
Codito, Ergo Sum (Courtesy of ThinkGeek)

[Mail][DreggSoft]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Dobbs
Dobbs
As Nanven said you need to initialize it somewhere. Also since it''s a static class member when you want to access it you don''t refer to it as instance.staticMember but as className::staticMember.
Nanven
Nanven
Oh ya, int CTest::obj=0 is right, been a long time since ive done that.
-Going to Westwood College for my Associates in Computer Programming
Yann L
Yann L
quote:

Yes, I know it works when I do that, I was wondering _why_ it doesn't work the other way.


A standard class member is physically and logically part of the class: it is accessed through an instance of the class, since it is physically stored in that object. Each object has a proper memory instance of that member.

A static class member is only logically part of the class. It is subject to all access protection mechanisms, but it is not physically part of a class instance. It is the same chunk of memory for each instance of a class object. Since it is not stored in the object itself, you need to define it somewhere else, outside of the class, so that the compiler can allocate memory for it.


[edited by - Yann L on February 26, 2003 12:21:07 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.