this doesn't want to compile....
class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element &elem(*iter);
if (Key == k)
{
return(elem);
}
}
throw("Not found");
}
};
I think the reason this isn't working is due to the const declaration of
the
method.
So what am I to do? 15 1679
JustSomeGuy wrote: this doesn't want to compile....
Always post the full error message.
class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter); if (Key == k) { return(elem); } } throw("Not found"); } };
I think the reason this isn't working is due to the const declaration of the method.
It is due to you trying to use a non-const-iterator on a const
container. Use const_iterator instead of iterator.
JustSomeGuy wrote: this doesn't want to compile....
class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter); if (Key == k) { return(elem); } } throw("Not found"); } };
I think the reason this isn't working is due to the const declaration of the method. So what am I to do?
Don't inherit std::list<>.
Generally, only inherit to override a virtual method. STL does not use any
virtuals.
If many clients used the image class, they might start playing with all the
different functions which std::list<> provides. When the time came to
replace the list with something else, those clients would resist that
change. Library classes, such as std::list<>, should have very wide
interfaces, but application-specific classes must be very narrow.
BTW the actual fix is image::const_iterator.
--
Phlip http://industrialxp.org/community/bi...UserInterfaces
Phlip wrote: Don't inherit std::list<>.
Generally, only inherit to override a virtual method. STL does not use any virtuals.
Earlier I asked a question about inheriting std::priority_queue<>. It
seems that priority_queue is designed to be inherited (as it has
protected members), even though it has no virtuals.
There are many reasons to inherit, even if something has no virtuals,
such as adding new behavior, even though it doesn't modify existing
(Note that I was using private inheritance).
Blanket bans may not be a good thing.
Rolf Magnus wrote: JustSomeGuy wrote:
this doesn't want to compile....
Always post the full error message.
class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter); if (Key == k) { return(elem); } } throw("Not found"); } };
I think the reason this isn't working is due to the const declaration of the method.
It is due to you trying to use a non-const-iterator on a const container. Use const_iterator instead of iterator.
I Can't use const_iterator because tof the overhead involved
element elem = *iter
make an entire copy of the object for every object in the list
(i.e. at every iteration of the for loop)
This is very inefficient...
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.ca... Rolf Magnus wrote:
JustSomeGuy wrote:
this doesn't want to compile.... Always post the full error message.
class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter); if (Key == k) { return(elem); } } throw("Not found"); } };
I think the reason this isn't working is due to the const declaration of the method.
It is due to you trying to use a non-const-iterator on a const container. Use const_iterator instead of iterator.
I Can't use const_iterator because tof the overhead involved
element elem = *iter
cont_iterator does NOT force you to do a value copy. Just return (*iter).
Why add the element& anyway? If you continue to insist on an local scoped
reference, it needs to be a const reference.
const element& elem(*iter);
or
element const& elem(*iter);
Your getElement member function is specified 'const', so that's why you need
to use const_iterator.
make an entire copy of the object for every object in the list (i.e. at every iteration of the for loop) This is very inefficient...
In the above code, probably not if 'Key == k' considering 'k' never changes
in this scope, and I assume Key is a glo or a data member, and is not
modified in this code.
Jeff F
Jeff Flinn wrote: "JustSomeGuy" <No***@ucalgary.ca> wrote in message news:40***************@ucalgary.ca... Rolf Magnus wrote:
JustSomeGuy wrote:
> this doesn't want to compile....
Always post the full error message.
> class image : public std::list<element> > { > element getElement(key k) const > { > image::iterator iter; > for (iter=begin(); iter != end(); ++iter) > { > element &elem(*iter); > if (Key == k) > { > return(elem); > } > } > throw("Not found"); > } > }; > > I think the reason this isn't working is due to the const declaration > of the method.
It is due to you trying to use a non-const-iterator on a const container. Use const_iterator instead of iterator. I Can't use const_iterator because tof the overhead involved
element elem = *iter
cont_iterator does NOT force you to do a value copy. Just return (*iter). Why add the element& anyway? If you continue to insist on an local scoped reference, it needs to be a const reference.
const element& elem(*iter);
or element const& elem(*iter);
Your getElement member function is specified 'const', so that's why you need to use const_iterator.
make an entire copy of the object for every object in the list (i.e. at every iteration of the for loop) This is very inefficient...
In the above code, probably not if 'Key == k' considering 'k' never changes in this scope, and I assume Key is a glo or a data member, and is not modified in this code.
Sorry that was supposed to be:
if (elem.Key == k) Jeff F
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.ca... Rolf Magnus wrote:
JustSomeGuy wrote:
this doesn't want to compile....
Always post the full error message.
class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter); if (Key == k) { return(elem); } } throw("Not found"); } };
I think the reason this isn't working is due to the const declaration of the method.
It is due to you trying to use a non-const-iterator on a const container. Use const_iterator instead of iterator.
I Can't use const_iterator because tof the overhead involved
element elem = *iter
make an entire copy of the object for every object in the list (i.e. at every iteration of the for loop) This is very inefficient...
So just change
element &elem(*iter);
to
const element &elem(*iter);
and use a const iterator.
If const made any difference at all to the efficiency of a C++ program it
wouldn't be part of the language.
john
On Fri, 18 Jun 2004 20:11:33 GMT, red floyd <no*****@here.dude> wrote: Phlip wrote:
Don't inherit std::list<>.
Generally, only inherit to override a virtual method. STL does not use any virtuals.
Earlier I asked a question about inheriting std::priority_queue<>. It seems that priority_queue is designed to be inherited (as it has protected members), even though it has no virtuals.
There are many reasons to inherit, even if something has no virtuals, such as adding new behavior, even though it doesn't modify existing (Note that I was using private inheritance).
Blanket bans may not be a good thing.
Why inherit from a class which you cannot use polymorphically in the
first place?
Since the base class destructor is not virtual, it is not called when
your derived class is destroyed. I suppose one could call it
explicitly from the derived class' destructor, but this is hardly good
design. Likewise, since the member functions in std::list are not
virtual, you must not override them in the derived class because that
would hide the base class functions.
It is quite possible to implement what you want to do using
delegation, or containment -- your class has a data member of type
std::list, and you can write forwarding functions for any of the
std::list methods which you would like to use in your own class. I did
this once in order to extend std::string, which also has no virtual
destructor. It is a bit tedious to write all the one-liner functions
to forward to std::list, but at least you end up with something that
works properly.
Really, you shouldn't inherit from this one. Some STL classes are
designed for inheritance, such as std::exception (which DOES have a
virtual destructor).
--
Bob Hairgrove No**********@Home.com
Bob Hairgrove wrote: Earlier I asked a question about inheriting std::priority_queue<>. It seems that priority_queue is designed to be inherited (as it has protected members), even though it has no virtuals.
There are many reasons to inherit, even if something has no virtuals, such as adding new behavior, even though it doesn't modify existing (Note that I was using private inheritance).
Blanket bans may not be a good thing. Why inherit from a class which you cannot use polymorphically in the first place?
Because you might want to inherit functionality from some class but
don't want to use it polymorphically.
Since the base class destructor is not virtual, it is not called when your derived class is destroyed.
That's wrong. Things only go wrong if a derived object is dynamically
allocated _and_ destroyed through a pointer to the base class. In this
case, the derived part isn't properly destroyed. Any other way of
creating/destroying the object will just work fine.
I suppose one could call it explicitly from the derived class' destructor, but this is hardly good design. Likewise, since the member functions in std::list are not virtual, you must not override them in the derived class because that would hide the base class functions.
Hiding and overriding are two different concepts. Anyway, if you do
that, then hiding the base class's function is exactly what you want.
It is quite possible to implement what you want to do using delegation, or containment -- your class has a data member of type std::list, and you can write forwarding functions for any of the std::list methods which you would like to use in your own class.
And if you want to use most functions of std::list, you will have the
boring task to write a whole lot of forwarding functions. What is the
advantage over just inheriting them and not needing to write any code
for them?
Note that when using containment and forwarding functions, you still
cannot use it as a polymorphic replacement for std::list, so by
deriving from std::list, you didn't lose _any_ functionality. The only
thing that's different is that you have less work when deriving.
On Sat, 19 Jun 2004 11:29:24 +0200, Rolf Magnus <ra******@t-online.de>
wrote: Bob Hairgrove wrote:
Earlier I asked a question about inheriting std::priority_queue<>. It seems that priority_queue is designed to be inherited (as it has protected members), even though it has no virtuals.
There are many reasons to inherit, even if something has no virtuals, such as adding new behavior, even though it doesn't modify existing (Note that I was using private inheritance).
Blanket bans may not be a good thing.
Why inherit from a class which you cannot use polymorphically in the first place?
Because you might want to inherit functionality from some class but don't want to use it polymorphically.
I overlooked the fact that he was using private inheritance. Since the base class destructor is not virtual, it is not called when your derived class is destroyed.
That's wrong. Things only go wrong if a derived object is dynamically allocated _and_ destroyed through a pointer to the base class. In this case, the derived part isn't properly destroyed. Any other way of creating/destroying the object will just work fine.
Of course, with private inheritance this isn't a problem anymore. I suppose one could call it explicitly from the derived class' destructor, but this is hardly good design. Likewise, since the member functions in std::list are not virtual, you must not override them in the derived class because that would hide the base class functions.
Hiding and overriding are two different concepts. Anyway, if you do that, then hiding the base class's function is exactly what you want.
It is quite possible to implement what you want to do using delegation, or containment -- your class has a data member of type std::list, and you can write forwarding functions for any of the std::list methods which you would like to use in your own class.
And if you want to use most functions of std::list, you will have the boring task to write a whole lot of forwarding functions. What is the advantage over just inheriting them and not needing to write any code for them? Note that when using containment and forwarding functions, you still cannot use it as a polymorphic replacement for std::list, so by deriving from std::list, you didn't lose _any_ functionality. The only thing that's different is that you have less work when deriving.
--
Bob Hairgrove No**********@Home.com
Bob Hairgrove wrote: Since the base class destructor is not virtual, it is not called when your derived class is destroyed.
The base class destructor is always called when an object of a derived
type is destroyed.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com)
Rolf Magnus wrote: In this case, the derived part isn't properly destroyed.
Not exactly true (if one wants to be pendantic).
Destroying a derived class though a pointer to a base class with no virtual
destructor results in *undefined behaviour*, which means anything can
happen.
Max
On Sat, 19 Jun 2004 08:48:05 -0400, Pete Becker <pe********@acm.org>
wrote: Bob Hairgrove wrote: Since the base class destructor is not virtual, it is not called when your derived class is destroyed.
The base class destructor is always called when an object of a derived type is destroyed.
Yes. I seem to have gotten this backwards.
Sorry for the FUD.
--
Bob Hairgrove No**********@Home.com
Bob Hairgrove wrote: Why inherit from a class which you cannot use polymorphically in the first place?
Because you might want to inherit functionality from some class but don't want to use it polymorphically.
I overlooked the fact that he was using private inheritance.
Me too ;-)
Rolf Magnus wrote: Bob Hairgrove wrote:
Why inherit from a class which you cannot use polymorphically in the first place?
Because you might want to inherit functionality from some class but don't want to use it polymorphically.
I overlooked the fact that he was using private inheritance.
Me too ;-)
I'd also point out that std::priority_queue<> has *protected* members,
which implies that it was designed to be inherited from.
I essentially had:
class MyData { ... };
class MyQueue : private std::priority_queue<MyData>
{
public:
MyQueue() : std::priority_queue<MyData>() { }
~MyQueue() { }
using std::priority_queue<MyData>::push;
// other using declarations to bring the entire interface forward
void my_additional_functionality(); // that uses protected
// members of priority_queue<>
}; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ree |
last post by:
I need to use a function from a wininet library, it requires the use of
const TCHAR.
The problem is the value needs to be calculated, so at the end I have this
string but I am having troubles...
|
by: Grey Plastic |
last post by:
I do not understand why this code fails to compile (under gcc):
#include <map>
using namespace std;
class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f);...
|
by: Dave C |
last post by:
I've written the following code that won't compile, trimmed down to
just the pertinent stuff:
--- WindowClass.hxx ----------------------------------------------------
#include <set>
class...
|
by: Dave |
last post by:
Hello NG,
It is well known that memory-allocating definitions should not be put in a
header file. I believe, however, that this does not apply to const
definitions. For example:
#ifndef...
|
by: James Aguilar |
last post by:
Take the following code example:
class Array {
double *m_array;
public:
Array() { m_array = new double; }
double *begin() const {return m_array;}
};
int main() {
|
by: Peter Ammon |
last post by:
Often times, I'll have some malloc()'d data in a struct that need not
change throughout the lifetime of the instance of the struct.
Therefore, the field within the struct is declared a pointer to...
|
by: Adrian Hawryluk |
last post by:
Hi all,
What is everyone's opinion of const inheriting? Should the object that
a pointer is pointing at inherit the constness of the pointer? Such as
in the case of a class having a pointer...
|
by: PVBHANU |
last post by:
Hi,
I am using DB2 V9.1 windows , can any one please tell me how to complile and execute a stored procedure.
I followed Alldatabse->database->Application Object->stored procedure...But No...
|
by: Long March |
last post by:
hi all,
How to fix the error C2893 complile with VC++6.0?
Thank you.
Code below:
#include <iostream>
using namespace std;
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |