Jump to content
  • Advertisement

Archived

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

fireking

lovely virtual functions...

This topic is 5335 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

class oh_a
{
public:
    int a;
    virtual void test(void);
};
void oh_a::test(void)
{
    a=100;
}
class oh_b : public oh_a
{
public:
    int b;
    virtual void test(void);
};
void oh_b::test(void)
{
    b=200;
}
now, how do i define the test function from base class a, since its virtual, we should be able to change what it does, but how?

Share this post


Link to post
Share on other sites
Advertisement
quote:
how do i define the test function from base class a

It''s already defined:

void oh_a::test(void)
{
a=100;
}

quote:
..., since its virtual, we should be able to change what it does, but how?

You have changed what it does.

oh_a* pa = new oh_a();
oh_a* pb = new oh_b();
pa->test(); // calls oh_a::test(), assign 100 to a
pb->test(); // calls oh_b::test(), assign 200 to b

Share this post


Link to post
Share on other sites
so basically, if you have a virtual function that has the same name in two classes, and you inherit from one of those class with opposite class, the new virtual function overrides the original virtual function

correct?

[edited by - fireking on November 30, 2003 7:23:43 AM]

Share this post


Link to post
Share on other sites
Yes.
You can also do that:

oh_b b;
oh_a *a = &b;
a->test();

This will run uh_b::test ...

[edited by - noVum on November 30, 2003 7:25:11 AM]

Share this post


Link to post
Share on other sites
what if two classes each had a function of the same name. Lets say the first class uses this function to initialize the array, and lets say the class that is inheriting from first class just sets a variable to false or something. Will the array still be initialized?

(note the functions are not virtual)

Share this post


Link to post
Share on other sites
Well, you should initalize arrays in constructor. Then, when you're inheriting, you should call that inherited constructor in constructor that you supply with new class.

EDIT: sorry for misunderstanding. In that case, function from class B hides function from class A. So, A.func() calls first function, and B.func() calls the second.

[edited by - Koshmaar on November 30, 2003 8:01:59 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by fireking
Will the array still be initialized?

(note the functions are not virtual)


No.

Share this post


Link to post
Share on other sites
so how would you implement a creation/deletion system

events such as

OnCreate//called before its created, in case user wants to do something
OnCreated//called after creation
OnDestroy//called before destroying (might wanna save some values!)
OnDestroyed//called after destroyed (update stuff if you like)

since virtual doesnt apply here, because they are overridden as soon as you inherit it (and almost every major class would have these functions)

regular functions dont apply either, unless you can do something like

void myclass:Create(void)
{
OnCreate();
myclass::inheritedclass::OnCreate();
...
}

but thats really unethical, this stuff should happen automatically

i was thinking function pointers, but now that i think about it, how do you call a create function in class a when class b has the same function name (maybe same parameters) and inherits class a...

bleh

Share this post


Link to post
Share on other sites
regular functions dont apply either, unless you can do something like [some code]

someclass::somefunction(); will force the choice of a given version of the function.

but thats really unethical, this stuff should happen automatically

No. If it did happen automatically, what would you do when it''s not appropriate. Overriding base class functions means you replace the original version entirely. If you want to call it in the process, do so explicitely.

Only for constructors and destructors does it make sense to do it automatically.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

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!