yet another virtual functions question...

J

jjsavage

Hi everyone,
Ok, I've got a base class called attribute, with a virtual print()
function. Attributes are never really instantiated, because it has to
be a continuous_attribute or a nominal_attribute (the derived classes).
But I need a list of attributes, and list<attribute> crashes if the
print() function (or any function) is pure virtual. Continuous and
nominal both have their own print() function, overriding the base
print(). So I fill my list<attribute> with continuous or nominal
attributes, but when I iterate through the list and print() each
element, all I ever get is the base attribute's print()! Here's the
snippets:

class attribute
{
public:
virtual void Print(void) { cout << "should never see this\n";}
};
class continuous_attribute: public attribute
{
public:
void Print(void) { cout << "continuous\n"; }
};
class nominal_attribute: public attribute
{
public:
void Print(void) { cout << "nominal\n"; }
};
list<attribute> schema;
for (list<attribute>::iterator iter = schema.begin(); iter !=
schema.end(); iter++)
{
iter->Print();
}

And all I see are "should never see this". What am I doing wrong?

Thanks,
John Savage
 
I

Ian Collins

Hi everyone,
Ok, I've got a base class called attribute, with a virtual print()
function. Attributes are never really instantiated, because it has to
be a continuous_attribute or a nominal_attribute (the derived classes).
But I need a list of attributes, and list<attribute> crashes if the
print() function (or any function) is pure virtual. Continuous and
nominal both have their own print() function, overriding the base
print(). So I fill my list<attribute> with continuous or nominal
attributes, but when I iterate through the list and print() each
element, all I ever get is the base attribute's print()! Here's the
snippets:

class attribute
{
public:
virtual void Print(void) { cout << "should never see this\n";}
better to have
virtual void Print() = 0;
};
class continuous_attribute: public attribute
{
public:
void Print(void) { cout << "continuous\n"; }
The (void) is a hangover from C what we don't use in C++>
};
class nominal_attribute: public attribute
{
public:
void Print(void) { cout << "nominal\n"; }
};
list<attribute> schema;

Don't use polymorphic types by value in standard containers, use a
pointer or smart pointer type.
for (list<attribute>::iterator iter = schema.begin(); iter !=
schema.end(); iter++)
{
iter->Print();
}

And all I see are "should never see this". What am I doing wrong?
It's called slicing, the list will only include the attribute part of
whatever derived class you insert.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top