Thread: [Dev-C++] classes and references
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-05-03 22:17:19
|
Hi!
Please, C++ gurus out there - help me, I think I'm gonna get crazy soon.
Finally I have decided to learn C++. 400 pages so far from Bjarne's
book. I can tell - it is a whole bunch of crap. I'm sure Bjarne can
desing C++ programs very well, but he cannot design a book, that's sure.
You just cannot find the thing that you need at the right place, it is
spread across 1600 pages. Find it - well thanks a lot :)
So, to the point. I have a question about a thing, that works with
Borland, and MSVC, but not with g++. Here it is:
I have a class, name it "myclass".
I have a function, which return "myclass".
I have a function, which uses "myclass".
Here they are:
//The class
class myclass{
public:
static void use_myclass(myclass& mc){ //The using function
printf("Value: %d\n",mc.value);
}
int value;
};
//The returner function
myclass make_myclass(void)
{
myclass mc_tmp;
return mc_tmp;
}
And so why the hell doesn't this work?:
int main()
{
myclass::use_myclass(make_myclass());
return 0;
}
WHILE THIS DOES:
int main()
{
myclass mc_tmp = make_myclass();
myclass::use_myclass(mc_tmp);
return 0;
}
The ONLY difference is, that I put the result in a temporary variable,
then I used that.
The compiler gave this warning to the first one:
test.cpp:19: no matching function for call to
`myclass::use_myclass(myclass)'
test.cpp:5: candidates are: static void myclass::use_myclass(myclass&)
So why the hell doesn't it make a reference from it then???
Please help me with this, I am at the bounds of getting crazy... really!
Thank you!
--
Greetings,
Balázs
|
|
From: Goran K. <go...@re...> - 2003-05-03 22:52:55
|
> So why the hell doesn't it make a reference from it then???
>=20
try defining your return type as a reference with a simple and(&) after t=
he class name.
myclass& make_myclass(void)
{
myclass mc_tmp;
return mc_tmp;
}
regards,
goran
V ned, 04.05.2003 ob 00:17, je OROSZI Bal=E1zs poslal(a):
> Hi!
>=20
> Please, C++ gurus out there - help me, I think I'm gonna get 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, that's sure=
.=20
> You just cannot find the thing that you need at the right place, it is=20
> spread across 1600 pages. Find it - well thanks a lot :)
>=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 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 crazy... really=
!
>=20
> Thank you!
>=20
> --
> Greetings,
> Bal=E1zs
>=20
>=20
>=20
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
|
|
From: <or...@vp...> - 2003-05-03 23:46:42
|
Goran Kavrecic wrote:
> try defining your return type as a reference with a simple and(&) after the class name.
>
> myclass& make_myclass(void)
> {
> myclass mc_tmp;
> return mc_tmp;
> }
Well, of course, but this sounds too simple. Besides I have already
tried it, and it gives a warning: "reference returned to local variable"
or something.
I am pondering, it has something to do with const stuff.
(... 5 mins trial ...)
BINGO!!!
I have changed the use_myclass function to this:
static void use_myclass(const myclass& mc){
printf("Value: %d\n",mc.value);
}
and it worked!
(... 5 mins reading ...)
YIPEE!!! I have also found the explanation in the book!!! :)))
So: when I call
use_myclass(make_myclass());
then make_myclass() will return with a local variable, thus causing the
copy constructor to get called. It will make a copy and place into a
TEMPORARY variable, then pass THAT for use_myclass()
The book sais (rough translation):
"We may use a temporary variable to initialize a constant."
BUT "... a temporary variable *cannot* be used to initialize a
non-constant reference"
So this is why I didn't work. It tried to initialise the parameter of
use_myclass(myclass&) (a reference) with a TEMPORARY variable. This is
illegal.
MSVC and BORLAND *_SUCKS_*!!! They support hacks!!! >:(
My, thank you pal. You drived my attention to the right part! Thanks,
really! :)
Oh, BTW one should *never* return a reference to a local variable. That
is written in the book, and it is also understandable. Because it will
be deleted, before it could be used.
Well, I got cleverer. I think I will be able to handle C++ well. So,
questions are welcome, least I will look it up, so I will learn too.
And again, thanks for you swift response.
--
Greetings,
Balázs
|
|
From: Goran K. <go...@re...> - 2003-05-04 00:29:38
|
V ned, 04.05.2003 ob 01:46, je OROSZI Bal=E1zs poslal(a): > Goran Kavrecic wrote: > > try defining your return type as a reference with a simple and(&) aft= er the class name. > tried it, and it gives a warning: "reference returned to local variable= "=20 I think I've got with BC something like "...return valuse used as temporary value" or at least it sounds alike. > I am pondering, it has something to do with const stuff. >=20 > (... 5 mins trial ...) >=20 > BINGO!!! >=20 > YIPEE!!! I have also found the explanation in the book!!! :))) > The book sais (rough translation): At the end the book isn't so stupid, a? > And again, thanks for you swift response. Welcome. Obvously we are stronger when united. Good night, Goran |
|
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
|
|
From: <or...@vp...> - 2003-05-04 07:58:06
|
Ioannis Vranos wrote: > 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. I don't want to argue about this, but I have *exact* examples. The book places stuff in the place, where they absolutely don't belong. For example: templates are first *used* in the 3rd chapter (and from then continuously) and described in the 12th... Trust me, learning C++ in 5 days is no easy task. I don't have years... and I don't need years. I think I am already doing well - I understand *all* of the const things now, the static stuff, classes, inheritance, overloading (operator and function), templates, exceptions, references etc. I'm a fast learner :) Oh, and about random - reading from page 1 to 400 fast or not fast is not random. ;) So, I think I have a good knowledge of C++ now, so questions are welcome - but still expect some from me. > Because as BS says in the book you can't pass a reference to a temporary > object, but you can pass a const reference. That's what I said, no? ;) -- Greets, Balázs P.S.: BTW, I have TC++PLSE (Special Edition) - and it is not meant for programmers of C++ - it is meant for starter C++ programmers. |
|
From: Ioannis V. <iv...@at...> - 2003-05-04 13:30:42
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of OROSZI Bal?zs > Sent: Sunday, May 04, 2003 10:58 AM > To: dev...@li... > Subject: Re: [Dev-C++] classes and references >=20 >=20 >=20 > I don't want to argue about this, but I have *exact*=20 > examples. The book=20 > places stuff in the place, where they absolutely don't belong. > For example: templates are first *used* in the 3rd chapter (and from=20 > then continuously) and described in the 12th... But he explicitly mentions that the 3 first chapters are a quick tour of = the language and he explicitly mentions that everything will become clear = later. The learning process begins from chapter 4. =20 > Trust me, learning C++ in 5 days is no easy task. > I don't have years... and I don't need years. I do not like it too, but it is what it takes to become a good = programmer. That said you do not need to know all the features provided by C++ to = do programs, but it takes years to learn it all (assuming you also have = other things to do daily). > I think I am already doing well - I understand *all* of the=20 > const things=20 > now, the static stuff, classes, inheritance, overloading=20 > (operator and=20 > function), templates, exceptions, references etc. I'm a fast=20 > learner :) >=20 > Oh, and about random - reading from page 1 to 400 fast or not fast is=20 > not random. ;) >=20 > So, I think I have a good knowledge of C++ now, so questions=20 > are welcome=20 > - but still expect some from me. I shall say that I will have a good knowledge of C++ after I have = finished the book inclduing the appendices. :) =20 > P.S.: BTW, I have TC++PLSE (Special Edition) - and it is not=20 > meant for=20 > programmers of C++ - it is meant for starter C++ programmers. Well it is aimed towards itermediate programmers. However my belief is = that every programmmer of C++ should have read this book. Regards, 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 |
|
From: <or...@vp...> - 2003-05-04 16:46:32
|
Ioannis Vranos wrote: > But he explicitly mentions that the 3 first chapters are a quick tour of the > language and he explicitly mentions that everything will become clear later. > The learning process begins from chapter 4. For example: templates are first *used* in the 3rd chapter (___*****and from then continuously*****___, i.e. in the 4th and 5th and so on) and described in the 12th... This caused me a lot of headache, he used it a lot of times, and I had absolutely no idea what hell is vector<xxx> for example... > I do not like it too, but it is what it takes to become a good programmer. > That said you do not need to know all the features provided by C++ to do > programs, but it takes years to learn it all (assuming you also have other > things to do daily). Well, that is called _experience_. I think it is way different from knowing, what a code part from another project does... > I shall say that I will have a good knowledge of C++ after I have finished > the book inclduing the appendices. :) No. Then you will have absolute knowledge. A C++ guru in other words. :) > Well it is aimed towards itermediate programmers. However my belief is that > every programmmer of C++ should have read this book. Yes, *should have*... -- Greets, Balázs |
|
From: Ioannis V. <iv...@at...> - 2003-05-04 18:16:10
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of OROSZI Bal?zs > Sent: Sunday, May 04, 2003 7:46 PM > To: dev...@li... > Subject: Re: [Dev-C++] classes and references > > > > I shall say that I will have a good knowledge of C++ after > I have finished > > the book inclduing the appendices. :) > > No. Then you will have absolute knowledge. A C++ guru in > other words. :) Ah yes. Well it is my style to state that I know something when I know it completely... All the people that I know behave differently. :) Ioannis Vranos * Programming pages: http://www.noicys.freeurl.com * Alternative URL 1: http://run.to/noicys * Alternative URL 2: http://www.noicys.cjb.net |
|
From: <cg...@wo...> - 2003-05-05 12:35:51
|
OROSZI Bal=E1zs wrote: > Ioannis Vranos wrote: > >> But he explicitly mentions that the 3 first chapters are a quick tour=20 >> of the >> language and he explicitly mentions that everything will become clear=20 >> later. >> The learning process begins from chapter 4. > > > For example: templates are first *used* in the 3rd chapter > (___*****and from then continuously*****___, i.e. in the 4th and 5th=20 > and so on) and described in the 12th... > This caused me a lot of headache, he used it a lot of times, and I had=20 > absolutely no idea what hell is vector<xxx> for example...=20 How in the hell can anyone understand the examples given for features if=20 the example program is full of unexplained features??. Well, one can=20 understand some of such kind of things in the very beginning of the=20 book. Well, I hope it has at least the currently feature which is being=20 explained highlighted. But it's like say to kids," hey: we'll start to=20 learn multiplication... let's work with Schr=F6dinger formulae." Certainly sometimes it can be needed or justified, but I doubt very much=20 using templates or vectors is absolutely needed before explaining them.=20 Then or you explain them at begin (at least the minimum you use and=20 then, the 12th chapter in detail or you explain it along the book, the=20 features as they are being used in the next chapters and then at the end=20 a chapter explaining all about them a a reminder) or you shall try to=20 avoid examples which need them. And vectors have nothing to do with templates, although vectors are=20 implemented as templates. Vectors are a kind of "array" (forgive my not=20 technical language) which is safer and more "complex" but also very=20 powerful, mainly in the dinamic memory management. > > >> I do not like it too, but it is what it takes to become a good=20 >> programmer. >> That said you do not need to know all the features provided by C++=20 >> to do >> programs, but it takes years to learn it all (assuming you also have=20 >> other >> things to do daily). > > > Well, that is called _experience_. I think it is way different from=20 > knowing, what a code part from another project does... Yeah, the minimum is explaining what that code which uses features which=20 are not been explained does, and the one explained too. Experience, or code, code and more code. But for having enough for that=20 experience being useful I lasted six months.(I had still some stupid=20 errors, but they were corrected more or less easily. Now, after a year=20 and a half I can write program without errors (not big programs) and=20 they do what I want. However, I don't consider me a guru yet, I'm still=20 far, but not too much). > >> I shall say that I will have a good knowledge of C++ after I have=20 >> finished >> the book inclduing the appendices. :) > > > No. Then you will have absolute knowledge. A C++ guru in other words. := )=20 It depends on the level of the book. But for being a guru you need end,=20 including appendices, some (many) books, or many, many * 5 practicing. > > >> Well it is aimed towards itermediate programmers. However my belief=20 >> is that >> every programmmer of C++ should have read this book. > > > Yes, *should have*...=20 Which book? :-D Yeah TC++PLSE. > > > --=20 > Greets, > Bal=E1zs > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ioannis V. <iv...@at...> - 2003-05-05 19:49:50
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Carlos Garc?a del Monte > Sent: Monday, May 05, 2003 3:28 PM > To: dev-cpp-users-list > Subject: Re: [Dev-C++] classes and references >=20 >=20 > How in the hell can anyone understand the examples given for=20 > features if=20 > the example program is full of unexplained features??. Well, one can=20 > understand some of such kind of things in the very beginning of the=20 > book. Well, I hope it has at least the currently feature=20 > which is being=20 > explained highlighted. But it's like say to kids," hey: we'll=20 > start to=20 > learn multiplication... let's work with Schr=F6dinger formulae." > Certainly sometimes it can be needed or justified, but I=20 > doubt very much=20 > using templates or vectors is absolutely needed before=20 > explaining them.=20 > Then or you explain them at begin (at least the minimum you use and=20 > then, the 12th chapter in detail or you explain it along the=20 > book, the=20 > features as they are being used in the next chapters and then=20 > at the end=20 > a chapter explaining all about them a a reminder) or you shall try to=20 > avoid examples which need them. Yes you have some right with your side here, but myself what I did not understand in a given example I did not bother about it and waited = patiently until I reach the chapter where it is explained. I did not met many = things like this though. As I said before reading TC++PL you must have finished some introductory book.first. The book is aimed at intermediate programmers. Before = getting to TC++PL I had finished "Teach Yourself C++ in 21 Days" (and before that I = had learned C90 which is not prerequisite for C++, but still with few = exceptions (not C++ "exceptions" :-) ) C90 is subset of C++. > And vectors have nothing to do with templates, although vectors are=20 > implemented as templates. Vectors are a kind of "array"=20 > (forgive my not=20 > technical language) which is safer and more "complex" but also very=20 > powerful, mainly in the dinamic memory management. vector is a template class, part of the standard library. =20 Keep also in mind that serious programming is not easy. :) Fir simple Winblowz GUI applications you need only a small part of the language = (the equivalent of VB, C#, J# etc that is simple classes - I am refering to = .NET, this is what I am learning) but if you want to construct mission = critical code then you must learn many details, for example std::list is very = fast for sorting since when you sort it pointer values are changed, while in vector or built in arrays, the objects are copied (using the copy constructor). The pain is in the details. :) Regards, 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 |