473,651 Members | 2,549 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:

DirectionalLigh t, 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 2547
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:

DirectionalLigh t, 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(vecto r<string>,vecto r<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...@yah oo.comwrote:
DirectionalLigh t, 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********@yah oo.comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.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:

DirectionalLigh t, 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 DirectionalLigh t: public Light
{
public:
DirectionalLigh t( 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 DirectionalLigh t( 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
7879
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 that we are not sure of PHP implementing polymorphism and we couldn't find info about it. I will really appreciate if someone could give us some recommendations about some books to read about it and/or pages to look for some info. Thanks for all.
7
4540
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 supported. You could find the module attached to this message. As in python, you could not define two method with same name (the second one override the first one), i have choosen to design it through private prefix '__' (defined method names and...
3
3681
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example program #include <list>
4
2396
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 hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
4
4421
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 reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
30
2009
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 base class that deals with adding, deleting, editing another class. I have written an implementation of that class, and also a derived version
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 function. What and how many updateable objects area actually created is determined at runtime.
17
3867
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
8345
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8789
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8693
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7293
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5603
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2694
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 we have to send another system
2
1584
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.