Static class members - how

L

Lilith

I'll admit that I haven't even attempted something like this since
I've never had a need to before. Basically the class I'm creating is
a screen graphic. Many of the member functions need to know what
surface, the one that represents the screen display, to draw to. I'm
trying to avoid having to rely on a global surface with a specific
name declared in order to make the system work.

For now I've included a pointer to the screen surface, represented by
a structure, in each object and initializing each of them to point to
the screen surface. Maybe I don't understand the usage but I tried to
make the pointer a static member so I'd only have to initialize it
once to make all class object know where the screen was. But on
compiling I get a complaint that there's a problem resolving an
external address, namely the pointer I'm referring to.

Advice please?
 
M

mlimber

Lilith said:
I'll admit that I haven't even attempted something like this since
I've never had a need to before. Basically the class I'm creating is
a screen graphic. Many of the member functions need to know what
surface, the one that represents the screen display, to draw to. I'm
trying to avoid having to rely on a global surface with a specific
name declared in order to make the system work.

For now I've included a pointer to the screen surface, represented by
a structure, in each object and initializing each of them to point to
the screen surface. Maybe I don't understand the usage but I tried to
make the pointer a static member so I'd only have to initialize it
once to make all class object know where the screen was. But on
compiling I get a complaint that there's a problem resolving an
external address, namely the pointer I'm referring to.

Advice please?

You need to *declare* the member static in your class (sounds like
you've done that) and then *define* it somewhere outside the class
(probably what you're missing). E.g.,

// In A.hpp
class A
{
static int i_; // declare
};

// In A.cpp
int A::i_ = 42; // define and initialize

Cheers! --M
 
N

Noah Roberts

Lilith said:
I'll admit that I haven't even attempted something like this since
I've never had a need to before. Basically the class I'm creating is
a screen graphic. Many of the member functions need to know what
surface, the one that represents the screen display, to draw to. I'm
trying to avoid having to rely on a global surface with a specific
name declared in order to make the system work.

For now I've included a pointer to the screen surface, represented by
a structure, in each object and initializing each of them to point to
the screen surface. Maybe I don't understand the usage but I tried to
make the pointer a static member so I'd only have to initialize it
once to make all class object know where the screen was. But on
compiling I get a complaint that there's a problem resolving an
external address, namely the pointer I'm referring to.

Advice please?

You could look into the singleton pattern.

Really, to know why it is breaking we need some code. Refer to faq on
posting questions to this group.
 
M

mlimber

mlimber said:
You need to *declare* the member static in your class (sounds like
you've done that) and then *define* it somewhere outside the class
(probably what you're missing). E.g.,

// In A.hpp
class A
{
static int i_; // declare
};

// In A.cpp

// Need this, too
#include "A.hpp"
 
L

Lilith

You need to *declare* the member static in your class (sounds like
you've done that) and then *define* it somewhere outside the class
(probably what you're missing). E.g.,
// In A.hpp
class A
{
static int i_; // declare
};
// In A.cpp
int A::i_ = 42; // define and initialize

Many thanks. Looks more like it's a point in and of itself.
 
L

Lilith

You could look into the singleton pattern.
Really, to know why it is breaking we need some code. Refer to faq on
posting questions to this group.

Thanks. I'm looking into the singleton pattern.

Point of fact, I started to paste in some of the code. But the more I
looked at it the less crucial anything but the one statement looked.
And that one line looked like a simple restatement of the question. So
I left it out.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top