473,770 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

yet another virtual functions question...

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_attr ibute or a nominal_attribu te (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_attr ibute: public attribute
{
public:
void Print(void) { cout << "continuous \n"; }
};
class nominal_attribu te: 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

May 10 '06 #1
2 1502
jj******@gmail. com wrote:
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_attr ibute or a nominal_attribu te (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_attr ibute: 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_attribu te: 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.

--
Ian Collins.
May 10 '06 #2
Hmm... slicing, eh? Interesting. Thanks!

May 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4428
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A = 4 bytes class B :public A = 4 bytes class...
15
10642
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but I've never used one. ( I'm an experienced software developer and have used C++ for more than 10 years) Anybody found the use of one of these guys necessary or useful. Curious minds need to know for the next curve ball question coming my
25
5421
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use a lot are the ones that are virtual and thus will slow down the calculation, at least that is what what I have read on several places on the internet. It makes sense the program has to work with some kind off lookup table for the virtual...
6
5834
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
3
11839
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone use private virtuals?", and the author answered with "almost never, but there are reasons". So, I am asking, what are the reasons. It seems illogical to have one; the point of a virtual function is to be overriden in a derived class, and the...
5
1990
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
4
2149
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
7
2116
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from BufferBase, and customized in order to be able to a type of data (of any kind, such as chunks of audio samples from a sound card). The processor classes are derived from ProcessorBase, and are customized to handle BufferBase-derived objects. Each...
14
4211
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
2479
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it has nothing to do with making multiple arguments in a declaration like:
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.