confused with "static"

W

WW

Alexander said:
Thank you Ron

I got it now, I just thought all the time the error was caused by
another behaivior of
static where it is used as an "anti-extern".
BTW Do I have to define it in every file I want to acces it?

No. Look at example #4 #5
 
A

Alexander Mahr

Nope. You did not define the static data member. I told you I was only
looking at your code from the point of view of the access of the member.

Thank you very much WW. Now I know what you meant but I didn't got it at
once
because I'm not very expiered in programming.

Thank you for your help

Cu Alexander
 
A

Alexander Mahr

WW said:
No. Look at example #4 #5

I will do this at once and save these really helpfull examples at once.
Thank you once again for your Help and your time.

Cu Alexander
 
J

Jerry Coffin

[email protected] says... said:
Does this mean you would suggest compiling the code below
shouldn't result in an error/ link error

I don't know what he would suggest, but your code is NOT correct, and an
error is to be expected with it.
########## File 1: CTest.h

class CTest
{
public:
static int static_data_member;

This declares, but does NOT define static_data_member.
CTest(){};
virtual ~CTest(){};
void memberfunction();
};

########## File 2: CTest.cpp

#include"CTest.h"

void CTest::memberfunction()
{
static_data_member=1;
}

Somewhere here (i.e. at namespace scope) you need to add a definition of
the variable:

int CTest::static_data_member;

Now the variable is defined, and the error you were getting should be
eliminated.

[ ... ]
BTw if you have to much time copy the code and save it into the files
CTest.h and CTest.cpp and try compiling and linking CTest.cpp I would
like to know if there is only my copmiler / linker is defective

VC++ 6 is far from perfect, but the problem you're encountering is in
your own code, not the compiler or linker.
 
J

jeffc

Alexander Mahr said:
Now I know what I got wrong all the time. As you said I forgot to define the
static member,
because I thought I would have done this already in the class decleration.
When then the error
appeared I browsed MSDN and google for an explenation and I found something
which tells
static (which is really a bit overloaded) causes variables to be "not
extern" and since I used
two files and accesd the static data member from the other file I thought
this was the reason
for the lnk error.

This is just my opinion, but this is one of the weaknesses of C++. In an
effort to avoid adding new keywords which might make some C programs be not
compatible, in the long run they probably wasted far more time with all the
confusion these sorts of things have caused programmers, such as yourself.
Yes, it's called "code", but that doesn't mean it really has to be
enigmatic. (This isn't a perfect example, because the confusing meaning of
static already existed in C. For a better example, would the world really
have stopped spinning if they added a purevirtual keyword? Or abstractclass
for that matter?)
 
W

WW

jeffc wrote:
[SNIP]
This is just my opinion, but this is one of the weaknesses of C++.
In an effort to avoid adding new keywords which might make some C
programs be not compatible, in the long run they probably wasted far
more time with all the confusion these sorts of things have caused
programmers, such as yourself. Yes, it's called "code", but that
doesn't mean it really has to be enigmatic. (This isn't a perfect
example, because the confusing meaning of static already existed in
C. For a better example, would the world really have stopped
spinning if they added a purevirtual keyword? Or abstractclass for
that matter?)

static was already 3 times overloaded in C. I do not see severe added
complexity in C++ and the C++ use of static aligns with the C one. For me
it took 5 minutes to get it, but I was using C for longer time at that time.
 
J

jeffc

WW said:
jeffc wrote:
[SNIP]
This is just my opinion, but this is one of the weaknesses of C++.
In an effort to avoid adding new keywords which might make some C
programs be not compatible, in the long run they probably wasted far
more time with all the confusion these sorts of things have caused
programmers, such as yourself. Yes, it's called "code", but that
doesn't mean it really has to be enigmatic. (This isn't a perfect
example, because the confusing meaning of static already existed in
C.

static was already 3 times overloaded in C. I do not see severe added
complexity in C++ and the C++ use of static aligns with the C one.

That's what I just said. Nice of you to add another useless flame though.
 
W

WW

jeffc said:
WW said:
jeffc wrote:
[SNIP]
This is just my opinion, but this is one of the weaknesses of C++.
In an effort to avoid adding new keywords which might make some C
programs be not compatible, in the long run they probably wasted far
more time with all the confusion these sorts of things have caused
programmers, such as yourself. Yes, it's called "code", but that
doesn't mean it really has to be enigmatic. (This isn't a perfect
example, because the confusing meaning of static already existed in
C.

static was already 3 times overloaded in C. I do not see severe
added complexity in C++ and the C++ use of static aligns with the C
one.

That's what I just said. Nice of you to add another useless flame
though.

Stop trolling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top