473,320 Members | 1,881 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,320 software developers and data experts.

Style question involving inheritance

I have a program set up something like this:

class A {
//(...)
}

class B : public A {
//(...)
public:
void not_in_class_A();
}

class usesA {
//(...)
protected:
A A_to_be_used;
}

class usesB : public usesA {
//(...)
void init(B B_to_be_used) {
A_to_be_used = B_to_be_used;
}

void do_something () {
A_to_be_used.not_in_class_A(); //error!
}
}

Assume that usesB::init(B_to_be_used) is the only method that modifies
the A A_to_be_used in a usesB object.

The above error can be easily fixed, if I'm not mistaken, using a cast:

((B) A_to_be_used).not_in_class_A();

but Bjarne Stroustrup says "casts are generally best avoided"
(http://www.research.att.com/~bs/bs_f...l#static-cast). I'm trying
to improve my C++ style, so I'm looking for either another solution to
the problem (namely, call A_to_be_used.not_in_class_A() somehow) or
assurance that this is an instance in which casting is stylistically
appropriate.

Thanks in advance.

-Russell Silva

Jul 19 '05 #1
1 1672
Alf P. Steinbach wrote:
(1)
An init-method instead of a constructor is very bad style.
Thank you, I didn't know that. A resulting question, however: I often
use init() methods when an object might need to be reinitialized (i.e.
most or all of its member variables reassigned). Is this still poor style?
(7)
The problem is something else. To find what the problem is (I don't
know what it is), think about _why_ you want that A_to_be_used object
defined high up in the hierarchy. A more concrete example might
help, since you've abstracted a technical detail of a non-solution.


*sighs* I had a feeling that my problem extended beyond one line of code.

Here's what I'm doing: (edited for relevance)

class Imp_DrawableBase { //formerly known as class A
protected:
vector<Imp_SpriteFrame> images;
void add(Imp_SpriteFrame& to_add);

public:
SDL_Surface* get_image(string to_get);
SDL_Surface* get_image(int to_get);
}

class Imp_SpriteBase : Imp_DrawableBase {//formerly known as class B
protected:
vector<Imp_SpriteSequenceBase> sequences;
void add(Imp_SpriteSequenceBase& to_add);

public:
Imp_SpriteSequenceBase& get_sequence(string to_get);
Imp_SpriteSequenceBase& get_sequence(int to_get); }

class Imp_Drawable { //formerly known as class usesA
protected:
SDL_Rect destrect; //position
Imp_DrawableBase* drawablebase;

int current_imagenum;
SDL_Surface* current_image;

bool initialized;

public:
virtual int draw();
int set_image(int imagenum);
Imp_Drawable();
virtual void init (Imp_DrawableBase& spritebase);
};
class Imp_Sprite : Imp_Drawable { //formerly known as class usesB
protected:
Imp_SpriteSequence current_sequence;

public:
int draw();
void set_sequence(string set_to);
Imp_Sprite();
void init (Imp_SpriteBase* spritebase);
};

This is an approximation; the code is not yet finished and it still
suffers from (as I just learned) stylistically inappropriate init()
methods. Still, the general pattern is there.

What I'm doing is creating classes for the display of static images and
sprites. Both sprites and static images require the same image
information (some number of images to display), but sprites also require
information on how to display the images in some order to form an animation.

Now I may want to display a given static image or sprite at multiple
places on the screen simultaneously. I thus create classes with suffix
Base that cache the information needed for display, and separate classes
to actually display the images. Thus, I can instantiate many objects
that all rely on the same Base information, saving memory. The
Imp_DrawableBase class contains a vector of the different images I wish
to display. The Imp_SpriteBase class, child of Imp_DrawableBase,
contains a vector of Imp_SpriteSequenceBase (not shown) that contain
information on how I wish the sprite to animate.

An Imp_Drawable object uses the images cached in a Imp_DrawableBase
object to draw static images on the screen. An Imp_Sprite object uses
both the cached images (from Imp_DrawableBase) and the cached sequences
(from Imp_SpriteBase) to draw animating sprites on the screen.

Which brings me to my original problem: I need Imp_Sprite objects to be
able to call get_sequence from Imp_SpriteBase. Some sort of cast would
work, especially since I use a pointer in my code, solving both the
slicing and the cast problem. But as has been pointed out,
stylistically I likely have already transgressed, but I'm not sure how
or what a good way to correct it might be.

If I've crossed the line into offtopic (from c++ style to 2d image
manipulation/gamemaking) limbo, my apologies.

Thanks in advance.

-Russell Silva

Jul 19 '05 #2

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

Similar topics

6
by: Andrew | last post by:
Hi, To satisfy my curiosity I was wondering if anyone knew if this behaviour was intentional? Is there a specific reason why exceptions are not allowed to be new style classes? Python 2.3...
12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
1
by: Flavian Mwasi | last post by:
I'm a novice programmer just beginning to learn the new C# language. I'm a bit confused about the way Inheritance and Interfaces are constructed in C#. The following examples may help clarify...
3
by: Jeff Stewart | last post by:
I've been working with the JavaScript Shell in Firefox on a mad-scientist problem I've had in my head. Assume a function named 'object' that is designed to create an object based on a prototype...
2
by: mjansen.merge.emed | last post by:
Is there a way to override inline within the <bodya style of an element but not do it with a style attribute on the element? I know CSS Inheritance works for some styles, but doesn't appear to...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
13
by: stephenpas | last post by:
We are trying to monkey-patch a third-party library that mixes new and old-style classes with multiple inheritance. In so doing we have uncovered some unexpected behaviour: <quote> class Foo:...
11
by: allendowney | last post by:
Hi All, I am working on a revised edition of How To Think Like a Computer Scientist, which is going to be called Think Python. It will be published by Cambridge University Press, but there...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.