You are not logged in.

#1 2009-07-27 02:35:15

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

[SOLVED] C++, virtual data? (different hash table for derived classes)

If I have a base class Base and a derived class Derived, I can override Base's functions with new implementations in Derived.

It does not seem I can do the same with data members. Please correct me if I'm wrong. In my situation, I want to write a Base class which performs some calculations based on a certain hash table (256 by 256 char). I plan to have a great many derived classes, each with the same code but different hash tables. The hash table contains pre-calculated static values which are populated on initialization.

A workaround I can think of is to have a virtual function which returns the values, something like:-

char Derived::gethashvalue(int x,int y) {
return MyOwnHash[x,y];
}

Would this work? Is there a better way to implement this (templates, maybe?)?

Last edited by ngoonee (2009-07-27 06:01:20)


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#2 2009-07-27 05:50:35

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: [SOLVED] C++, virtual data? (different hash table for derived classes)

You should first make clear in which way the hash tables of the derived classes differ from
the Base class' hash table. If they differ in type, use templates:

#include <hash>

template <class HashTableEntry>
class Base
{
// ...
private:
  std::hash<HashTableEntry> MyOwnHash;
// ...
};

If they differ in size and not in type, you can either use templates or just parametrize your
base class accordingly.

The template way:

#include <hash>

template <size_t HashSize>
class Base
{
// ...
private:
  std::hash<HashTableEntry> MyOwnHash;
// ...
};

Base<HashSize>::Base()
{
  MyOwnHash.resize(HashSize);
}

The parameter way:

#include <hash>

class Base
{
public:
  Base( const size_t hashSize );
// ...
private:
  std::hash<HashTableEntry> MyOwnHash;
// ...
};

Base::Base( const size_t hashSize );
{
  MyOwnHash.resize(hashSize);
}

If only the values differ, then I have written all of this in vain because you're already
done. Just write the code to fill the hash in each of your derived classes' initialization
function (e.g. the constructor) and - like you suggested - offer an access method for
it in the base class.

Offline

#3 2009-07-27 06:00:37

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: [SOLVED] C++, virtual data? (different hash table for derived classes)

Ah, I realize now that its actually quite simple. Thank you for your help!


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB