|
-
March 29th, 2004, 10:07 AM
#1
static array of base class pointers to point to derived classes dynamically ?
Hey guys,
here is the problem:
I have to keep an static array of pointers of type base class , and I should be able to assign each of these pointers to any of the derived class..
here is the code...
class Base
{
public:
static Base* basePtr[10];
}
class derived1 : public Base
{ }
class derived2 : public Base
{}
now in my main.cpp I want to be able to create objects of derived1 or derived2 and assign the basePtr to point to them....
how do I do that???
.....
...
derived1 d1
Base::basePtr[1] = &d1 // I tried this approach, the problem is...does this mean I have to have 10 different classes of d1 or d2 and then assign each of their references to the basePtr ??? Is there way to do this dynamically so that I do some thing like the following
Base::basePtr[1] = new derived1;
Pls help..
-
March 29th, 2004, 11:49 AM
#2
Re: static array of base class pointers to point to derived classes dynamically ?
Originally posted by LudaLuda
now in my main.cpp I want to be able to create objects of derived1 or derived2 and assign the basePtr to point to them....
how do I do that???
Didn't you just do it in your example here?
Code:
derived1 d1;
Base::basePtr[1] = &d1;
//...
Base::basePtr[1] = new derived1;
Both are valid.
I tried this approach, the problem is...does this mean I have to have 10 different classes of d1 or d2 and then assign each of their references to the basePtr ???
Now that is a different question. Also, there are no references in your example, only pointers. The problem you should be concerned about is whether the pointer will go out of scope, thereby making the Base array have invalid pointers stored.
By storing the address of a (potential) local variable (I don't know in what scope d1 is declared), that address is valid until d1 is destroyed when it leaves scope. Then your Base array will have a pointer to an invalid address. If you create an object dynamically, then it won't go out of scope until you explicitly delete it.
Regards,
Paul McKenzie
-
March 29th, 2004, 12:58 PM
#3
To solve these problems that Paul pointed, you´ll need to save the derived object order in the base class array and when the object goes out ouf scope you aNULLate it via derived destructor.The problem now is, as the derivated class are aleatory instantiate and destroyed the array will become with NULL pointer without any order. Then you´ll need to develop a function to get not the next free pointer on the array top, but the first free pointer. You´ll also need to get the order number of this free entry in the array, so you can save it in the derivated class.
I develop a small program missing these two last funcionalities:
Code:
#include <iostream>
class Base
{
static int QObjects;
protected:
static Base* basePtr[10];
public:
int getQObjects() { return QObjects;};
Base* getbasePtr(int n) { return basePtr[n];};
Base();
};
Base::Base()
{
if (QObjects == 0) for(int i = 0; i < 10; i++) basePtr[i] = NULL;
if (QObjects < 10) basePtr[QObjects] = this;
QObjects++;
};
class derived1 : public Base
{
public:
derived1() { n = getQObjects() -1;};
~derived1() { basePtr[n] = NULL;};
void test1()
{
std::cout << "test1\n";
}
};
class derived2 : public Base
{
int n;
public:
derived2() { n = getQObjects() -1;};
~derived2() { basePtr[n] = NULL;};
void test2()
{
std::cout << "test2\n";
}
};
int Base::QObjects = 0;
Base* Base::basePtr[10] = {0,0,0,0,0,0,0,0,0,0};
int main()
{
derived1 dd;
((derived1*)(dd.getbasePtr(0)))->test1();
{
derived2 dd2;
((derived2*)(dd.getbasePtr(1)))->test2();
}
}
Regards
Rabelo
Last edited by Rabelo; March 29th, 2004 at 01:06 PM.
-
March 30th, 2004, 02:56 AM
#4
Code:
int getQObjects() { return QObjects;};
Base* getbasePtr(int n) { return basePtr[n];};
have to declare the function getQObjects() and getbasePtr() to be static to access the static member variables? I am now confused with java and c++
:P
-
March 30th, 2004, 01:39 PM
#5
I don´t know about Java but in C/C++ a static function means that that function is only visible in the same translation unit it was declared.
static variables members of a class means that all objects of that class will share the same variable.
So There is no connection between a static variable and a static function. You can acess a static member from any function, provide you have grants to do that.
Regards
Rabelo
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|