RE: [Dev-C++] classes and references
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@at...> - 2003-05-04 06:26:05
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of OROSZI Bal?zs
> Sent: Sunday, May 04, 2003 1:17 AM
> To: Dev...@li...
> Subject: [Dev-C++] classes and references
>=20
>=20
> Hi!
>=20
> Please, C++ gurus out there - help me, I think I'm gonna get=20
> crazy soon.
>=20
> Finally I have decided to learn C++. 400 pages so far from Bjarne's=20
> book. I can tell - it is a whole bunch of crap. I'm sure Bjarne can=20
> desing C++ programs very well, but he cannot design a book,=20
> that's sure.=20
> You just cannot find the thing that you need at the right=20
> place, it is=20
> spread across 1600 pages. Find it - well thanks a lot :)
TC++PL3 is meant to be read by C++ programmers who know the basics and
sequencially, that is from cover to cover. Not in random order. For =
example
how can you understand the implementations of the standard containers if =
you
haven't finished the chapter about templates. Each page must be read
thoroughly and the whole process will take years.
Apparently you tried to read it in a fast/random fashion.
>=20
> So, to the point. I have a question about a thing, that works with=20
> Borland, and MSVC, but not with g++. Here it is:
>=20
> I have a class, name it "myclass".
> I have a function, which return "myclass".
> I have a function, which uses "myclass".
> Here they are:
>=20
> //The class
> class myclass{
> public:
> static void use_myclass(myclass& mc){ //The using function
> printf("Value: %d\n",mc.value);
> }
> int value;
> };
>=20
> //The returner function
> myclass make_myclass(void)
> {
> myclass mc_tmp;
> return mc_tmp;
> }
>=20
> And so why the hell doesn't this work?:
>=20
> int main()
> {
> myclass::use_myclass(make_myclass());
> return 0;
> }
>=20
> WHILE THIS DOES:
>=20
> int main()
> {
> myclass mc_tmp =3D make_myclass();
> myclass::use_myclass(mc_tmp);
> return 0;
> }
>=20
> The ONLY difference is, that I put the result in a temporary=20
> variable,=20
> then I used that.
> The compiler gave this warning to the first one:
>=20
> test.cpp:19: no matching function for call to=20
> `myclass::use_myclass(myclass)'
> test.cpp:5: candidates are: static void myclass::use_myclass(myclass&)
>=20
> So why the hell doesn't it make a reference from it then???
>=20
> Please help me with this, I am at the bounds of getting=20
> crazy... really!
Because as BS says in the book you can't pass a reference to a temporary
object, but you can pass a const reference.
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|