converting pointer to const ref

C

cppaddict

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
 
M

Marcin Vorbrodt

cppaddict said:
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);
l.push_back(*myClass); // dereference the pointer
}
}

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?

Because there is a type missmatch. Pointer is not a reference. Function
expects a const reference, but instead it gets a pointer... bad.
2. How do you make this code work?

Look above.
 
J

John Carson

cppaddict said:
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?

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.
2. How do you make this code work?

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.
 
D

David White

cppaddict said:
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?

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?
2. How do you make this code work?

l.push_back(*myClass);

DW
 
J

Jakob Bieling

cppaddict said:
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?

Because you are the programmer.
2. How do you make this code work?

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
 
J

Jonathan Mcdougall

cppaddict wrote:

nice name :)
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();

someInitMethod() ? That is what constructor are for.
l.push_back(myClass);
}
}

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);
}
The compiler errors on "l.push_back(myClass)", saying that it can't
convert MyClass* to const MyClass&.

The list you created accepts MyClass objects and you are giving it a
MyClass*, that is the problem
Two questions:

1. Why can't it do this conversion?

Because it is invalid.
2. How do you make this code work?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top