473,471 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Static class members - how

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?

--
TIA,
Lilith
Aug 4 '06 #1
5 1263
Lilith wrote:
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

Aug 4 '06 #2

Lilith wrote:
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.

Aug 4 '06 #3
mlimber wrote:
Lilith wrote:
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
// Need this, too
#include "A.hpp"
int A::i_ = 42; // define and initialize

Cheers! --M
Aug 4 '06 #4
On 4 Aug 2006 12:36:07 -0700, "mlimber" <ml*****@gmail.comwrote:
>Lilith wrote:
>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
Many thanks. Looks more like it's a point in and of itself.
>Cheers! --M

--
Lilith
Aug 4 '06 #5
On 4 Aug 2006 12:48:18 -0700, "Noah Roberts" <ro**********@gmail.com>
wrote:
>
Lilith wrote:
>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.
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.

--
Lilith
Aug 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
13
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
6
by: Matt | last post by:
All of a sudden all my C# apps require the keyword static on all global fields and methods that I create. Even in the simplest of console apps. Can someone tell me what I have inadvertenly set in...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.