Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JD

Empty virtuals - broken OO

This topic is 5436 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does it makes sense to send an object a msg and have the object not act on it? Did anyone else noticed this little derail from OO. I''ve noticed Bjarne abuse this as well, maybe because there is no way around it short of splitting the interface but then that defeats the purpose of abstract programming. You thoughts on this? My view, live with it. Wrong or right?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by JD
Does it makes sense to send an object a msg and have the object not act on it?


Perhaps an example is in order...

Share this post


Link to post
Share on other sites
Do you mean pure abstract methods? Meaning virtual void f() = 0; ?
And on another hand, I have seen patterns that employ this ''send an object a msg and have the object not act on it''. Some call it the Null Object pattern, whereby the object is an empty object that consumes all methods but do nothing significant. Just a design pattern

Share this post


Link to post
Share on other sites
It sortof makes sense now that I think of it. Since the abstract interface has virtuals that do nothing, saying that I inherited from it also means that I can do nothing as well?

struct Shape
{
virtual void rotate()=0;
};

struct Box : Shape
{
void rotate(){do a rotation}
};

struct Circle : Shape
{
void rotate(){empty since for circle rotation doesn''t make sense}
};

struct Rotator
{
vector itsShapes;
void RotateShapes(){for each shape do shape.rotate()}
};

Now, only boxes will rotate while circles won''t. What does it mean to send a circle(shape?) a rotate msg? I like to adhere to open/close principle so no dyn.casts to find out whether a shape is a circle or a box then invoke rotate() based on that. Thoughts?

Share this post


Link to post
Share on other sites
Sorry for the derailing rant, but how could that possibly be considered a unique design pattern?

Why would have an object with void methods, unless we can somehow remove that object, or hook-in other objects that actually do something. It’s a small implementation node of an actual design pattern. If we talked about how it is actually used, we would in all likelyhood find that it is an implementation of an interface described in one of the GoF behavioral patterns (e.g. Proxy) (or we would have have to uncover another atomic behavior - an actual behavioral design pattern).

So many people have thoughtless and irresponsibly jumped on board the ''design pattern'' bandwagon, that they have trampled the fledging idea that was actually worth something.

Please don''t call it a design pattern (even though the author does 0.o). Sure it''s a pattern, everything can be categorized and abstracted into a pattern, but it''s not what one means when the talk about the atomic design patterns as described by the GoF.

Share this post


Link to post
Share on other sites


class shape{public:virtual void draw()=0;};

class rotating_shape : public shape{public:virtual void rotate())=0;};

class none_rotating_shape : public shape{};

class sphere: public none_rotating shape{...};

class square : public rotating_shape{...};



Problem Solved?

Share this post


Link to post
Share on other sites
quote:

class shape{public:virtual void draw()=0;};

class rotating_shape : public shape{public:virtual void rotate())=0;};

class none_rotating_shape : public shape{};

class sphere: public none_rotating shape{...};

class square : public rotating_shape{...};


That doesn''t make any sense unless you are trying to categorize shapes on whether or not they can rotate. There is nothing wrong with implementing a pure virtual function with nothing in it.

Share this post


Link to post
Share on other sites
Ok, guys. So it seems like everyone agrees here that it''s ok to have empty virtual overrides. Thanks for your thoughts

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!