473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why won't this complile??? const troubles...

this doesn't want to compile....

class image : public std::list<eleme nt>
{
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?

Jul 22 '05 #1
15 1691
JustSomeGuy wrote:
this doesn't want to compile....
Always post the full error message.
class image : public std::list<eleme nt>
{
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.

Jul 22 '05 #2
JustSomeGuy wrote:
this doesn't want to compile....

class image : public std::list<eleme nt>
{
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_it erator.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Jul 22 '05 #3
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_q ueue<>. 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.
Jul 22 '05 #4
Rolf Magnus wrote:
JustSomeGuy wrote:
this doesn't want to compile....


Always post the full error message.
class image : public std::list<eleme nt>
{
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...

Jul 22 '05 #5

"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:40******** *******@ucalgar y.ca...
Rolf Magnus wrote:
JustSomeGuy wrote:
this doesn't want to compile....
Always post the full error message.
class image : public std::list<eleme nt>
{
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
Jul 22 '05 #6
Jeff Flinn wrote:
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:40******** *******@ucalgar y.ca...
Rolf Magnus wrote:
JustSomeGuy wrote:

> this doesn't want to compile....

Always post the full error message.

> class image : public std::list<eleme nt>
> {
> 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


Jul 22 '05 #7

"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:40******** *******@ucalgar y.ca...
Rolf Magnus wrote:
JustSomeGuy wrote:
this doesn't want to compile....


Always post the full error message.
class image : public std::list<eleme nt>
{
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
Jul 22 '05 #8
On Fri, 18 Jun 2004 20:11:33 GMT, red floyd <no*****@here.d ude> 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_q ueue<>. 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**********@Ho me.com
Jul 22 '05 #9
Bob Hairgrove wrote:
Earlier I asked a question about inheriting std::priority_q ueue<>. 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.

Jul 22 '05 #10

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

Similar topics

7
20925
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 converting it into a const TCHAR so I can use it in the function. TIA
4
1445
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); } };
3
1495
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 Window; class WindowClass { public:
15
4169
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 MY_HEADER #define MY_HEADER const int FOO = 42;
4
1206
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() {
16
12267
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 const . But then free() complains when I pass in the pointer because free() is declared as void free(void*). So I have to cast. Why is it not...
17
2426
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 and then dereferencing that pointer. Should the dereferenced pointer have the same constness of the pointer as the pointer has the same constness as...
1
2063
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 Use... i have following simple stored procedure.. create procedure test(in value1 int,in value2, out value3) begin
0
1124
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;
0
8327
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...
1
7935
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...
0
8193
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...
0
6579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
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...
0
5374
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3843
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2333
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
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.