Virtual functions, inheritance and stuff...

Started by
5 comments, last by Rudan 21 years, 5 months ago
This will probably be a stupid question, but since I''ve never really used inheritance and virtual functions I''m pretty stuck here. Here we go: Let''s say I got some code similiar to this: (Yeah, stupid example, I know...)

#include <iostream>
#include <string>
//#include <stdio.h>

using namespace std;

class Base
{
public:
	virtual void Print() = 0;
};

class Test1: public Base
{
public:
	void Print()
	{
		cout << "Test1" << endl;
	}
};

class Test2: public Base
{
public:
	void Print()
	{
		cout << "Test2" << endl;
	}
};
 
Now, what if I want to have an array where I can save instances of either Test1 OR Test2? I''m not sure I did a very good job explaining this, but if someone know what I mean, I''d appreciate some help. ----------------------------- Reporter: Are they slow-moving, chief? Sheriff: Yeah, they''re dead. They''re all messed up. -Night of the living dead
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
Advertisement
Use an array of pointers to their common base class, Base.
Dobbs is correct. But in case you don''t understand why...

Base is an abstract class (indicated by the fact that it has one or more abstract functions) so no instances of it can be created, thats why we need an array of pointers to Base instead of an array of Base. LSP specifies that a child class can be used wherever a parent class is expected, scince our array of pointers expects objects of class Base, we may substitute any child of base (namely, test1 and test2 objects).
Just Expanding on what Dobbs just said. You can set up the array like this

Base *BaseArray[10]; //declare an array of 10 Base classes

then when you want to add in a class say test1

BaseArray[0] = (Base*)new Test1;

and for Test2

BaseArray[1] = (Base*)new Test2;

The (Base*) Thing basically tells c++ to treat the pointer as if it''s a pointer to a class of type Base. This allows you to put it in the array

Now when you execute Print.

BaseArray[0]->Print( );
Output:
Test1

BaseArray[1]->Print( );
Output:
Test2

Because the pointer to a class in BaseArray[0] is a pointer to a Test1 class and the pointer to a class in BaseArray[1] is a pointer to a Test2 class.

Hope that told you what you want to know
Well, that''s what I thought. I''m glad I wasn''t completely out of line.

However, I seem to be unable to get it working. Could you show me a quick example?

(I haven''t done any real programming for about a year now, so I''m a bit rusty...)

-----------------------------
Reporter: Are they slow-moving, chief?
Sheriff: Yeah, they''re dead. They''re all messed up.
-Night of the living dead
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
Don''t mind my last post. Moran gave me an example while I was writing that. Thanks.

But I''m did a misstake in my first post. Actually, I don''t want the size of the array to be predefined, and that''s where I''m stuck right now.

I know how new and delete and those things work, I can''t just get it to work properly with the pointers to the base class

Thanks for your help, anyway...

-----------------------------
Reporter: Are they slow-moving, chief?
Sheriff: Yeah, they''re dead. They''re all messed up.
-Night of the living dead
-----------------------------Reporter: Are they slow-moving, chief?Sheriff: Yeah, they're dead. They're all messed up.-Night of the living dead
Right reasearch into linked lists and stl. You don''t actually need to know how linked lists work to use something like stl::list but I think it''s always a good idea to know things. Use Google to find information on linked lists and stl

This topic is closed to new replies.

Advertisement