473,387 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Jul 19 '05 #1
6 5286
cppaddict <cp*******@yahoo.com> wrote in message
news:Mo***************@newssvr27.news.prodigy.com. ..
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

Jul 19 '05 #2
"cppaddict" <cp*******@yahoo.com> wrote in message
news:Mo***************@newssvr27.news.prodigy.com
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #3
"cppaddict" <cp*******@yahoo.com> wrote in message
news:Mo***************@newssvr27.news.prodigy.com. ..
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.

thanks,
cppaddict


Jul 19 '05 #4
"cppaddict" <cp*******@yahoo.com> wrote in message
news:Mo***************@newssvr27.news.prodigy.com. ..
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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #5
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
Jul 19 '05 #6
thank you everyone for your help.

cpp
Jul 19 '05 #7

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

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
6
by: James Aguilar | last post by:
I have a class that's holding a list of pointers to pointers. It is an implementation of an open addressed hashmap. It is homework, but I only want help on this specific issue. Please do not...
4
by: Paul Erion | last post by:
Hi, I ran across the following #define in some documentation for an embedded system (I truncated the actual define, but the general form is intact). #define PADDING \ "x-00: ab\r\n"\ "x-01:...
20
by: Nate | last post by:
I am working on an Oakley parser and want to call an fopen function to read in the log file. I can use this to read the file, but only if I pass a "const char" variable to fopen. Since I would...
7
by: jamihuq | last post by:
Hello, I would like to convert the following inline function to a macro. Can someone help? Thx Jami inline char * fromDESC(const char * &aDesC)
2
by: Kavya | last post by:
Since Circle is-a Shape we are allowed to do this Circle *c = new Circle; Shape *s = c; //Works But we can't do this Circle **cc = &c; Shape **ss = cc; //Does not works
156
by: Lame Duck | last post by:
Hi Group! I have a vector<floatvariable that I need to pass to a function, but the function takes a float * arguement. That's OK, I can convert by doing &MyVector.front(), but when I get back a...
9
by: mathieu | last post by:
Hi, I know I am doing something stupid here, but it's friday night and I cannot see what is the issue here: Thanks, -Mathieu #include <iostream>
35
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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 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.