inherited members not accessable through an stl vector?

Started by
2 comments, last by eldee 21 years, 7 months ago
topic pretty much covers it... a generalization of what i'm trying to do:

struct customtype
{
   int someint;
   float somefloat;
}
class Parent
{
public:
   customtype ParentWidget
};
class Child : public Parent
{
public:
   customtype ChildWidget
};
    
the problem i'm having is this:
  
vector<Child> ChildObject(5); // anybody know how to do an angle bracket without using the source tag? :)

   
i can access anything defined in the child class, but nothing derived from the parent class. ie:

ChildObject[1].ChildWidget.someint = 1234; // this is okay
ChildObject[1].ChildWidget.somefloat = 1.2345; // this is also okay
ChildObject[1].ParentWidget.someint = 1234; // this is not okay.. i get a compiler error saying ParentWidget is undefined.
    
is this supposed to happen? could it be that i haven't initialized my ParentWidget through a constructor? templates are supposed to encourage reusability, as is inheritance.. so my obvious thought is that they should work together naturally... i hope im just doing something terribly stupid here.. [edit: damn html codes] -eldee ;another space monkey; [ Forced Evolution Studios ]

::evolve:: [edited by - eldee on September 8, 2002 2:58:05 PM] [edited by - eldee on September 8, 2002 2:58:44 PM]

-eldee;another space monkey;[ Forced Evolution Studios ]
Advertisement
I get no errors at all using your example and a small driver.

Can you post original source instead of a generalization? You mention templates, but you didn''t use any in your example...

I''m using MSVS .Net to compile

Here''s what I used with no errors...


  struct customtype{   	int someint;   	float somefloat;};class Parent{public:   	customtype ParentWidget;};class Child : public Parent{public:   	customtype ChildWidget;};#include <vector>#include <iostream>using namespace std;int main(char argv, char* argc[]){	vector<Child> myVec(10);	Child	myChild;	myChild.ParentWidget.somefloat = 34.2f;	myChild.ParentWidget.someint   = 3;	myChild.ChildWidget.somefloat = 23.4f;	myChild.ChildWidget.someint   = 9;	myVec[0].ParentWidget.somefloat = 34.2f;	myVec[0].ParentWidget.someint   = 3;	cout << myVec[0].ParentWidget.somefloat << endl;	return 0;}  
Beware that you would not be able to store parentWidgets in this vector. If you''re going to treat things polymorphically (e.g. store different types in the container via base class), you need to declare it by pointer, not by object:
vector<Parent*>
You can use html to display < & >
& l t ;
& g t ;
(without the spaces)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement