converting pointer to const ref 
July 19th, 2005, 07:12 PM
| | | converting pointer to const ref
I can't figure out why this code won't work, or how to fix it:
list<MyClass> l;
int main() {
for (int i=0;i<5;i++) {
MyClass* myClass = new MyClass;
myClass -> someInitMethod();
l.push_back(myClass);
}
}
The compiler errors on "l.push_back(myClass)", saying that it can't convert
MyClass* to const MyClass&.
Two questions:
1. Why can't it do this conversion?
2. How do you make this code work?
thanks,
cppaddict | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
cppaddict <cppaddict@yahoo.com> wrote in message
news:MoKib.892$vc4.637@newssvr27.news.prodigy.com. ..[color=blue]
> I can't figure out why this code won't work, or how to fix it:
>
> list<MyClass> l;
> int main() {
> for (int i=0;i<5;i++) {
> MyClass* myClass = new MyClass;
> myClass -> someInitMethod();
> l.push_back(myClass);
> }
> }
>
> The compiler errors on "l.push_back(myClass)", saying that it can't[/color]
convert[color=blue]
> MyClass* to const MyClass&.
> Two questions:
>
> 1. Why can't it do this conversion?[/color]
Well, there's really no need. If you have a vector of pointers, then you
push back a pointer. If you have a vector of objects (non-pointer objects
that is), then push back an object. Why pass a pointer and expect the
compiler to do a conversion if you can easily pass the correct type?
[color=blue]
> 2. How do you make this code work?[/color]
l.push_back(*myClass);
DW | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
"cppaddict" <cppaddict@yahoo.com> wrote in message
news:MoKib.892$vc4.637@newssvr27.news.prodigy.com[color=blue]
> I can't figure out why this code won't work, or how to fix it:
>
> list<MyClass> l;
> int main() {
> for (int i=0;i<5;i++) {
> MyClass* myClass = new MyClass;
> myClass -> someInitMethod();
> l.push_back(myClass);
> }
> }
>
> The compiler errors on "l.push_back(myClass)", saying that it can't
> convert MyClass* to const MyClass&.
> Two questions:
>
> 1. Why can't it do this conversion?[/color]
Why should it? C++ is a typed language, which means that variables of
different types are not interchangeable (some limited conversions/casts are
possible, but conversions are not universally available). myClass is a
pointer to a MyClass object, but the list does not store pointers, it stores
MyClass objects.
[color=blue]
> 2. How do you make this code work?[/color]
Either change the list so it stores pointers (i.e., use list<MyClass*> l) or
add objects rather than add pointers to the list (i.e., use
l.push_back(*myClass) ). Whether either option represents the best design is
another issue.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
"cppaddict" <cppaddict@yahoo.com> wrote in message
news:MoKib.892$vc4.637@newssvr27.news.prodigy.com. ..[color=blue]
> I can't figure out why this code won't work, or how to fix it:
>
> list<MyClass> l;
> int main() {
> for (int i=0;i<5;i++) {
> MyClass* myClass = new MyClass;
> myClass -> someInitMethod();[/color]
//l.push_back(myClass);
l.push_back(*myClass); // dereference the pointer
[color=blue]
> }
> }
>
> The compiler errors on "l.push_back(myClass)", saying that it can't[/color]
convert[color=blue]
> MyClass* to const MyClass&.
> Two questions:
>
> 1. Why can't it do this conversion?[/color]
Because there is a type missmatch. Pointer is not a reference. Function
expects a const reference, but instead it gets a pointer... bad.
[color=blue]
> 2. How do you make this code work?[/color]
Look above.
[color=blue]
>
> thanks,
> cppaddict
>
>
>
>
>[/color] | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
"cppaddict" <cppaddict@yahoo.com> wrote in message
news:MoKib.892$vc4.637@newssvr27.news.prodigy.com. ..[color=blue]
> I can't figure out why this code won't work, or how to fix it:
>
> list<MyClass> l;
> int main() {
> for (int i=0;i<5;i++) {
> MyClass* myClass = new MyClass;
> myClass -> someInitMethod();
> l.push_back(myClass);
> }
> }
>
> The compiler errors on "l.push_back(myClass)", saying that it can't[/color]
convert[color=blue]
> MyClass* to const MyClass&.
> Two questions:
>
> 1. Why can't it do this conversion?[/color]
Because you are the programmer.
[color=blue]
> 2. How do you make this code work?[/color]
Change your line to:
l.push_back (*myClass);
You dereference the pointer and have the actual object (and not just the
pointer to it). Now the compiler can create a reference to that object and
pass the reference to push_back ().
hth
--
jb
(replace y with x if you want to reply by e-mail) | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
cppaddict wrote:
nice name :)
[color=blue]
> I can't figure out why this code won't work, or how to fix it:
>
> list<MyClass> l;
> int main() {
> for (int i=0;i<5;i++) {
> MyClass* myClass = new MyClass;
> myClass -> someInitMethod();[/color]
someInitMethod() ? That is what constructor are for.
[color=blue]
> l.push_back(myClass);
> }
> }[/color]
Are you sure you want to allocate the objects dynamically? Why not
something like
for (int i=0;i<5;i++)
{
MyClass myClass;
myClass.someInitMethod();
l.push_back(myClass);
}
[color=blue]
> The compiler errors on "l.push_back(myClass)", saying that it can't
> convert MyClass* to const MyClass&.[/color]
The list you created accepts MyClass objects and you are giving it a
MyClass*, that is the problem
[color=blue]
> Two questions:
>
> 1. Why can't it do this conversion?[/color]
Because it is invalid.
[color=blue]
> 2. How do you make this code work?[/color]
Either with
list<MyClass*> l;
in which case *do not forget* to delete every pointer in the list, or with
the solution I provided.
Jonathan | 
July 19th, 2005, 07:12 PM
| | | Re: converting pointer to const ref
thank you everyone for your help.
cpp | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|