473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static variables in abstract base classes

Are there any reasons one should avoid using static variables in
abstract base classes?

I was considering to the following:

#include <iostream>

using namespace std;

enum { SZ = 10 };

class AbstractBaseClass
{
protected:
static int a[SZ];
public:
AbstractBaseClass() { }
virtual void foo() const = 0;
static void bar(int i) { ++a[i]; }
};

int AbstractBaseClass::a[SZ];

class Derived: public AbstractBaseClass
{
public:
Derived(): AbstractBaseClass() { }
void foo() const
{
int i = 0;
cout << "Derived::foo(): a[" << i << "]: " << a[i] << endl;
}
};

Nov 17 '06 #1
1 2104
pasa_1 wrote:
Are there any reasons one should avoid using static variables in
abstract base classes?

I was considering to the following:

#include <iostream>

using namespace std;

enum { SZ = 10 };

class AbstractBaseClass
{
protected:
static int a[SZ];
public:
AbstractBaseClass() { }
virtual void foo() const = 0;
static void bar(int i) { ++a[i]; }
};

int AbstractBaseClass::a[SZ];

class Derived: public AbstractBaseClass
{
public:
Derived(): AbstractBaseClass() { }
void foo() const
{
int i = 0;
cout << "Derived::foo(): a[" << i << "]: " << a[i] << endl;
}
};
There's no inherent problem with them, but you should initialize them
when you define them. Also consider using a container rather than a raw
array (http://www.parashift.com/c++-faq-lit...html#faq-34.1).

Cheers! --M

Nov 17 '06 #2

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

Similar topics

33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
19
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
2
by: Mark P | last post by:
Still being relatively new to C++ I like to go back to review and rethink old designs to see where I could do things better. The issue I'm currently pondering is static vs. dynamic polymorphism,...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
7
by: Brybot | last post by:
Apparently it is not possible for a static class to extend an abstract class? I was wondering how else I might be able to go about my problem here? I have a base class Parent which has a static...
0
by: pasa_1 | last post by:
Are there any reasons one should avoid using static variables in abstract base classes? Thanks in advance. I was considering to do the following: #include <iostream>
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.