Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any cast that will cast Derived[] to Base[] 2

Status
Not open for further replies.

areza123

Programmer
Jul 4, 2002
60
IN
class Base {
public:
virtual void f(){ cout << &quot;in f() of Base...&quot; << endl;};
};

class Derived : public Base {
public:
private:
int i_;
};

void userCode(Base* arrayOfBase) {
arrayOfBase[1].f();
}

int main()
{
Derived arrayOfDerived[10];
userCode(arrayOfDerived);
}

The problem is that the array created is of 80 bytes
(each object of Derived is 8 and Base is 4).
Hence arrayOfBase[1] would point only 4 bytes ahead of arrayOfBase when actually the 2nd object starts 8 bytes ahead.

One easy way to solve this is to rename the signature
void userCode(Base*) to
void userCode(Derived*)

but is there some other solution in which I can retain
void userCode(Base* ) ?
Any casts that will cast Derived[] to Base[]
 
Is there some specific reason you don't use a template based collection for your solution?

-pete
 
I'm not sure I understand well what would you like but I see there some kind of java programming style.
You can use C++ style, as palbano said:

class Base {
public:
virtual void f(){ cout << &quot;in f() of Base...&quot; << endl;};
};

class Derived : public Base {
public:
private:
int i_;
};

template<class T>void userCode(T* arrayOfBase, int i) {
arrayOfBase.f();
}

int main()
{
Derived arrayOfDerived[10];
userCode(arrayOfDerived);
}


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
You could have an array of pointers instead.
Code:
typedef Base* BasePtr;
void userCode(BasePtr* arrayOfBasePtr) {
arrayOfBasePre[1]->f();
}

In RealLife(tm) I would however not implement my own collection thingie, but rather use the ones in STL.

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
I would agree and disagree with you PerFnurt. Agree because I ended up using the stl approach (though personally I feel the template is better) disagree because using array of pointers means I initialize each element of the array to point to the appropriate object of Derived array (I think this is what u meant right)

My stl based approach gives a compilation error atleast. It is as follows:

class Base {
public:
virtual void f(){ cout << &quot;in f() of Base...&quot; << endl;};
};

class Derived : public Base {
public:
private:
int i_;
};
void userCode(vector<Derived> arrayOfBase) {
arrayOfBase[5].f();
}

int main()
{
vector<Derived> arrayOfDerived(10);
cout << &quot;Size of derived &quot; << sizeof(Derived *) << endl;
cout << &quot;Size of base &quot; << sizeof(Base *) << endl;
userCode(arrayOfDerived);
const char * const str = &quot;&quot;;
}


 
Ok, you could say what error it gives.

Perhaps missing constructors?

Anyway, I'd definately pass vector by reference rather than passing the entire vector itself.

void userCode(vector<Derived>& arrayOfBase)




/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Ah ok the reference thats right. When I said compilation errors I meant... had I declared

void userCode(vector<Base> & arrayOfBase) {...}
and tried passing

vector<Derived> arrayOfDerived(10);

the compiler would have flagged an error.

This is always better than
void userCode(Base* arrayOfBase) {arrayOfBase[1].f()}
and the program compiling perfectly but crashing @ run time when I pass

Derived arrayOfDerived[10];
userCode(arrayOfDerived);

Thanks all I have not only a better solution but better options now.

Cheers
 
You have to realize you can't have 1 array with multiple element types, either its an array of Base or an array of Derived, or an array of something else.

What I think you're after is an array of Base*, but with elements that are instantiated as either Base or Derived.

Code:
typedef vector<Base*> BaseArray;
  .
  .
void userCode(BaseArray& array) 
{
  array[5]->f();
}
  .
  .
{
  BaseArray array(10,NULL);
  // Elements initially NULL
  array[2] = new Base(....);
  array[5] = new Derived(....);
  userCode(array);
}

/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top