Connecting Tech Pros Worldwide Help | Site Map

converting pointer to const ref

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:12 PM
cppaddict
Guest
 
Posts: n/a
Default 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






  #2  
Old July 19th, 2005, 07:12 PM
David White
Guest
 
Posts: n/a
Default 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



  #3  
Old July 19th, 2005, 07:12 PM
John Carson
Guest
 
Posts: n/a
Default 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)

  #4  
Old July 19th, 2005, 07:12 PM
Marcin Vorbrodt
Guest
 
Posts: n/a
Default 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]


  #5  
Old July 19th, 2005, 07:12 PM
Jakob Bieling
Guest
 
Posts: n/a
Default 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)


  #6  
Old July 19th, 2005, 07:12 PM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default 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


  #7  
Old July 19th, 2005, 07:12 PM
cppaddict
Guest
 
Posts: n/a
Default Re: converting pointer to const ref

thank you everyone for your help.

cpp


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.