Archived

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

inherited members not accessable through an stl vector?

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

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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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*>

Share this post


Link to post
Share on other sites