473,503 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

polymorphism; dummy parameters

Say you have 3 child classes A, B, C deriving from a parent class P
with pure virtual function f.

Suppose A::f(x1); // A::f depends only on x1
Suppose B::f(x1, x2); // B::f depends on x1 and x2
Suppose C::f(x1, x2, x3); // C::f depends on x1, x2, x3

Is it bad form to define P::f(x1, x2, x3), and then A::f and B::f just
ignore the parameters it does not need?

In some sense this could be justified; you could say that for A::f, x2
and x3 are just "zero" or some identity value that doesn't actually
affect the output.

Any thoughts on this?

I think it is okay occasionally if not abused often, but I would like
the opinion of the C++ programming community.

For those that want some context, basically I have a Light class and I
derive:

DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time. I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.

May 3 '07 #1
3 2543
vsgdp wrote:
Say you have 3 child classes A, B, C deriving from a parent class P
with pure virtual function f.

Suppose A::f(x1); // A::f depends only on x1
Suppose B::f(x1, x2); // B::f depends on x1 and x2
Suppose C::f(x1, x2, x3); // C::f depends on x1, x2, x3

Is it bad form to define P::f(x1, x2, x3), and then A::f and B::f just
ignore the parameters it does not need?
Yes. But you'll be better off asking about it in 'comp.object'.
Here, in the multi-paradigm, "do as I please as long as it's legal",
land, it's perfectly OK, if it works for you.
In some sense this could be justified; you could say that for A::f, x2
and x3 are just "zero" or some identity value that doesn't actually
affect the output.

Any thoughts on this?
Not many, no. Depends on the problem you're solving, I guess.
I think it is okay occasionally if not abused often, but I would like
the opinion of the C++ programming community.

For those that want some context, basically I have a Light class and I
derive:

DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time.
So, it seems that you need a specific "set my stuff" function, which
should probably be very generic and implement some kind of "property"
thing.
I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.
Take a look at "properties". If you give your properties names, by
which you will be able to differenciate them, you will be able to set
them independently. You can have a single 'set(string, void*)' function
or add another 'set_many(vector<string>,vector<void*>)'.

I am not sure it's better than stuffing the base class with the arguments
it's not going to use (that's true for any pure virtual function, no?);
at least it's a bit more generic (and you can probably find some more
or less generalized implementations out there in the open).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 3 '07 #2
On May 4, 3:18 am, vsgdp <cloud00...@yahoo.comwrote:
DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time. I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.
These things are much easier with a concrete example :-)

A normal OO approach would be to move the management of the changes
into a separate hierarchy (I guess that you're changing the parameters
during the course of animating the model).

You can now pass the manager (think of it like a puppet master where
the light is the puppet it is controlling) into the method, but in C++
you're going to need a downcast which feels icky.

A better way is to have all interaction occur within the puppet master
objects which then set the model elements for each frame. Now instead
of creating a model element on its own you always create a pair of
model element and its puppet master. The user interacts with the
puppet master, but the model elements can still be hived off for
rendering of individual frames (and the rendering code will be the
same, only the animation control will change).
K

May 4 '07 #3
"vsgdp" <cl********@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
Say you have 3 child classes A, B, C deriving from a parent class P
with pure virtual function f.

Suppose A::f(x1); // A::f depends only on x1
Suppose B::f(x1, x2); // B::f depends on x1 and x2
Suppose C::f(x1, x2, x3); // C::f depends on x1, x2, x3

Is it bad form to define P::f(x1, x2, x3), and then A::f and B::f just
ignore the parameters it does not need?

In some sense this could be justified; you could say that for A::f, x2
and x3 are just "zero" or some identity value that doesn't actually
affect the output.

Any thoughts on this?

I think it is okay occasionally if not abused often, but I would like
the opinion of the C++ programming community.

For those that want some context, basically I have a Light class and I
derive:

DirectionalLight, PointLight, SpotLight, AreaLight. The area light
implementation requires some special data the others do not; this data
also varies over time so I can't just set it at construction time. I
can't set the area light's specific data at runtime either without
knowing its specific type (defeats polymorphism). So I am thinking
the best solution would be to just pass the data to the pure virtual
function, even though only area lights will use the data.
It sounds to me like P:f(x1, x2, x3) should actually be in the constructor,
no? Okay, you don't now it's type, but if you could set it in the
constructor, that would be the way to go, right? So, do it in the
constructor. Have each derived classes' constructor initialize as required.

Let me try to give an example. The following compiles.

#include <iostream>
#include <string>

struct Vector3D
{
double x;
double y;
double z;
};

class Light
{
public:
Light( unsigned long Color, Vector3D Pos ): Color_( Color ), Pos_( Pos )
{}
virtual ~Light() {}
virtual void Render() { std::cout << "Rendering from Light\n"; }
protected:
unsigned long Color_;
Vector3D Pos_;
};

class DirectionalLight: public Light
{
public:
DirectionalLight( unsigned long Color, Vector3D Pos, Vector3D Aim ):
Light( Color, Pos ), Direction_( Aim ) {}
void Render() { std::cout << "Rendering from DirectionLight\n"; }
private:
Vector3D Direction_;
};

class PointLight: public Light
{
public:
PointLight( unsigned long Color, Vector3D Pos, double Radius ): Light(
Color, Pos ), Radius_( Radius ) {}
private:
double Radius_;
};

class SpotLight: public Light
{
public:
SpotLight( unsigned long Color, Vector3D Pos, Vector3D Aim, double
Radius ): Light( Color, Pos ), Direction_( Aim ), Radius_( Radius ) {}
private:
Vector3D Direction_;
double Radius_;
};

class AreaLight: public Light
{
public:
AreaLight( unsigned long Color, Vector3D Pos, double Radius ): Light(
Color, Pos ), Radius_( Radius ) {}
private:
double Radius_;
};

int main()
{
Vector3D Center = { 10.0, 20.0, 30.0 };
Vector3D Aim = { 5.0, 5.0, 5.0 };
unsigned int Color = 0xFF00FFFF;
Light* Light1 = new Light( Color, Center );
Light* Light2 = new DirectionalLight( Color, Center, Aim );
Light* Light3 = new PointLight( Color, Center, 20.5 );
Light* Light4 = new SpotLight( Color, Center, Aim, 10.2 );
Light* Light5 = new AreaLight( Color, Center, 5.5 );

delete Light1;
delete Light2;
delete Light3;
delete Light4;
delete Light5;
}

Now, if your'e going to need more information in Light (Radius seems to be
largly common, I would probably move it there and initialize it to 0.0 for
lights that don't need it).

This should be enough for you to get started on. If you have any questions
let us know.
May 5 '07 #4

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

Similar topics

13
7864
by: Gurtaj | last post by:
Hello! I am working with PHP and MySQL in a proyect with some partners of our university, and we are thinking about building some classes and use inheritance and polymorphism. The problem is...
7
4527
by: rashkatsa | last post by:
Hi all ! I have written a little module called 'polymorph' that allows to call different methods of a class that have the same name but that have not the same number of arguments. *args are...
3
3659
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
4
2385
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
4
4408
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
30
1969
by: Richard Tappenden | last post by:
Hi! I'm struggling a bit with Polymorphism in vb.net. There is something I know I can do in C++, but I cannot seem to do it in vb.net (or c# for that matter). Basically, I have an abstract...
2
735
by: glen stark | last post by:
I have a templated function template<class T> void update_E(T& updateable) { .... } What I would like to do is store a vector of updateable objects, and for each of them call the update...
17
3829
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
7198
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
7072
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
7319
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...
1
6979
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
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
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 ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.