Tuesday, February 4, 2020

Digest for comp.lang.c++@googlegroups.com - 20 updates in 2 topics

fir <profesor.fir@gmail.com>: Feb 04 08:47AM -0800

im eben somewhat experienced c coder
but got no experience in writing c++ or any OOP
 
the question is say i gare smal snake game made in sfml, i want to have 3 objects
 
GameWindow, Snake, Food
 
but those 3 are dependamt in circular weay
i mean, would like to create
 
Food food(window);
Snake snake(window, food);
Window window(snake, food);
 
but here window is obvioisly tey not created
 
and i want to use onlyt references inside to hold those conections, not pointers,
 
how ro do that?
tnx
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 04 05:59PM +0100

On 04.02.2020 17:47, fir wrote:
 
> but here window is obvioisly tey not created
 
> and i want to use onlyt references inside to hold those conections, not pointers,
 
> how ro do that?
 
In a class snake game the snake is confined to the mid points of squares
or hexagons in a lattice.
 
So, that's all you need for a snake representation: the set of points it
currently occupies, the direction it's headed in, and its current speed.
 
You can make a draw function separate. It needs the "canvas" (whatever
that's called in SFML, a "device context" in Windows API) to draw on,
and access to the the snake object's data that is relevant to drawing it.
 
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 04 06:00PM +0100

On 04.02.2020 17:59, Alf P. Steinbach wrote:
 
>> how ro do that?
 
> In a class snake game the snake is confined to the mid points of squares
> or hexagons in a lattice.
 
"classic", sorry. Microsoft keyboard.
 
 
fir <profesor.fir@gmail.com>: Feb 04 09:12AM -0800

W dniu wtorek, 4 lutego 2020 17:59:36 UTC+1 użytkownik Alf P. Steinbach napisał:
 
> You can make a draw function separate. It needs the "canvas" (whatever
> that's called in SFML, a "device context" in Windows API) to draw on,
> and access to the the snake object's data that is relevant to drawing it.
 
but i dont want te separate draw i need it divided as i said and kept tofetger by fields which are references
 
snake gas method draw that needs reference to its window (for drawing as drawing needs window handle), and update whuch also needs window (for window size)
 
food also needs those for close reason (need draw and window suze)
 
window in turn has its window loop from where it need to calls those updates and draws of snake and food
 
so in short i wand such code as i wrote
 
int main()
{
Food food(window);
Snake snake(window, food);
Window window(snake, food);

window.start();
}
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Feb 04 07:34PM

On 04/02/2020 17:12, fir wrote:
> Window window(snake, food);
 
> window.start();
> }
 
Not that I agree with your design but to avoid the circular dependency:
 
Food food;
Snake snake{ food };
Window window{ snake, food };
 
in Window class:
 
food.draw(*this);
snake.draw(*this);
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
fir <profesor.fir@gmail.com>: Feb 04 12:04PM -0800

W dniu wtorek, 4 lutego 2020 20:34:11 UTC+1 użytkownik Mr Flibble napisał:
 
> food.draw(*this);
> snake.draw(*this);
 
> /Flibble
 
i will see,
maybe someone want to see that whole snake sourcem and say what better class designe woyld be ? (i may ebentually post it) its not long main.cpp food.h food.cpp snake.h snake.cpp
fir <profesor.fir@gmail.com>: Feb 04 12:34PM -0800

W dniu wtorek, 4 lutego 2020 21:04:31 UTC+1 użytkownik fir napisał:
 
> > /Flibble
 
> i will see,
> maybe someone want to see that whole snake sourcem and say what better class designe woyld be ? (i may ebentually post it) its not long main.cpp food.h food.cpp snake.h snake.cpp
 
some other question
i wanted to pust some of those objects into anither object
 
notably somethibg like that
 
class GameWindow
{
RenderWindow window;
 
Food food(window);
Snake snake(window, food);
 
}
 
 
but it seems i cant put thiose with constructors as a fields, whot is the use of yi if i even cant put it as a fields?
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Feb 04 09:15PM

On 04/02/2020 20:34, fir wrote:
> Snake snake(window, food);
 
> }
 
> but it seems i cant put thiose with constructors as a fields, whot is the use of yi if i even cant put it as a fields?
 
class GameWindow
{
GameWindow();
 
RenderWindow window;
Food food;
Snake snake;
};
 
GameWindow::GameWindow() :
food{window}, snake{window, food}
{}
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
fir <profesor.fir@gmail.com>: Feb 04 01:53PM -0800

W dniu wtorek, 4 lutego 2020 22:15:31 UTC+1 użytkownik Mr Flibble napisał:
 
> GameWindow::GameWindow() :
> food{window}, snake{window, food}
> {}
 
so it is said to be posibie? i will see, byr later
 
now im to frustratet forgor wht ** this langiege is
seeplus <boardmounrt@gmail.com>: Feb 04 02:40PM -0800

On Wednesday, February 5, 2020 at 3:47:42 AM UTC+11, fir wrote:
> im eben somewhat experienced c coder
> but got no experience in writing c++ or any OOP
 
> the question is say i gare smal snake game made in sfml, i want to have 3 objects
 
Search !
"sfml snake game code"
OR
"c++ snake game code"
 
Either will give you 10 pages of results with every variety
of code to give you a start.
fir <profesor.fir@gmail.com>: Feb 04 03:22PM -0800

W dniu wtorek, 4 lutego 2020 23:40:35 UTC+1 użytkownik seeplus napisał:
> "c++ snake game code"
 
> Either will give you 10 pages of results with every variety
> of code to give you a start.
 
snake is nota porblem, problem is this insanelly vrappy language which disallows
even poting this 3 things (window, food, snake) niecly put together
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 04 05:35PM +0100

On 03.02.2020 11:25, Juha Nieminen wrote:
> manually writing __FILE__ and __LINE__ every single time, but why? Making it
> like this makes it easier and handier to use, and allows more easily changing
> it to do something else instead. I can't think of many drawbacks.
 
In C++20 you will get `std::source_location` to do the info collection
for you. Usage is as a defaulted argument of e.g. a `dprint` function.
 
<url: https://en.cppreference.com/w/cpp/utility/source_location>
 
Unfortunately they forgot to make it safe to copy so it can't reasonably
be used to carry information in an exception. But, since it uses the
standard library's license to employ magic outside the normal rules, it
is or will be great for doing the collecting without macros.
 
 
- Alf
Paavo Helde <myfirstname@osa.pri.ee>: Feb 04 08:12PM +0200

On 4.02.2020 18:35, Alf P. Steinbach wrote:
 
> <url: https://en.cppreference.com/w/cpp/utility/source_location>
 
> Unfortunately they forgot to make it safe to copy so it can't reasonably
> be used to carry information in an exception.
 
The above link says it is CopyConstructible and CopyAssignable, and "It
is intended that source_location has a small size and can be copied
efficiently."
 
Seems "safe to copy" for me.
James Kuyper <jameskuyper@alumni.caltech.edu>: Feb 04 04:14PM -0500

On 2/4/20 11:35 AM, Alf P. Steinbach wrote:
...
> for you. Usage is as a defaulted argument of e.g. a `dprint` function.
 
> <url: https://en.cppreference.com/w/cpp/utility/source_location>
 
> Unfortunately they forgot to make it safe to copy
 
CopyConstructible, CopyAssignable, and "can be copied efficiently" would
seem to cover all the bases. What did they leave out?
 
"It is unspecified whether the copy/move constructors and the copy/move
assignment operators of source_location are trivial and/or constexpr.",
but I don't see how that would be a big problem (it could be a small
one, in some contexts).
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 04 10:39PM +0100

On 04.02.2020 19:12, Paavo Helde wrote:
> is intended that source_location has a small size and can be copied
> efficiently."
 
> Seems "safe to copy" for me.
 
Move constructor is noexcept. Copy constructor is not. :(
 
<url:
https://en.cppreference.com/w/cpp/utility/source_location/source_location>
 
Standard exceptions guarantee noexcept copying, so one wouldn't want to
break that reasonably expected guarantee in a custom exception class.
Though it would be technically permitted.
 
 
- Alf
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Feb 04 10:42PM +0100

On 04.02.2020 22:39, Alf P. Steinbach wrote:
 
> Standard exceptions guarantee noexcept copying, so one wouldn't want to
> break that reasonably expected guarantee in a custom exception class.
> Though it would be technically permitted.
 
Forgot to mention, how do you make a noexcept copyable string value
class very easily?
 
Just use a `std::runtime_error` to carry the value.
 
It uses an otherwise very hidden safe to copy string class. I would have
preferred to have that class exposed by the standard library. Alas.
 
 
- Alf
Paavo Helde <myfirstname@osa.pri.ee>: Feb 05 12:07AM +0200

On 4.02.2020 23:39, Alf P. Steinbach wrote:
 
> Standard exceptions guarantee noexcept copying, so one wouldn't want to
> break that reasonably expected guarantee in a custom exception class.
> Though it would be technically permitted.
 
Ah, now I see what you meant by "not safe to copy". Could not figure it
out by the previous post.
 
Well, one can always add a try-catch block and fall back to not have
source_location info in the presumably *very* unlikely case the
source_location copy will throw. A bit cumbersome, yes.
Paavo Helde <myfirstname@osa.pri.ee>: Feb 05 12:26AM +0200

On 4.02.2020 23:42, Alf P. Steinbach wrote:
>> class. Though it would be technically permitted.
 
> Forgot to mention, how do you make a noexcept copyable string value
> class very easily?
 
Been there, done that. Not so easily, and not so sure if it was worth that.
 
The only exception scenario in string copying is memory exhaustion.
Nowadays I would just silence this and return a static string like
"memory exhausted" from what() if copying failed.
 
In my experience, if the computer has consumed up all its memory so that
copying of an error message is not possible, then already half of active
programs have crashed and the system has been slowed down to crawl from
which it will not recover without restart. There would not be much point
to try to copy my error message.
 
Or, with another kind of OS, the culprit program (typically mine) would
already have been killed by the OOM killer and the situation would not
occur.
Keith Thompson <Keith.S.Thompson+u@gmail.com>: Feb 04 02:39PM -0800


> The only exception scenario in string copying is memory
> exhaustion. Nowadays I would just silence this and return a static
> string like "memory exhausted" from what() if copying failed.
 
How is returning incorrect data (in this case, the string "memory
exhausted") better than, say, terminating the program?
 
[...]
 
 
> Or, with another kind of OS, the culprit program (typically mine)
> would already have been killed by the OOM killer and the situation
> would not occur.
 
The risk I have in mind is that, even though the system might be on the
verge of falling over, your program might still make use of that
meaningless "memory exhausted" string as if it were valid data, with
arbitrarily bad results.
 
As I recall, treating an error message as data is part of what led to
the Ariane 5 failure.
 
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Paavo Helde <myfirstname@osa.pri.ee>: Feb 05 01:13AM +0200

On 5.02.2020 0:39, Keith Thompson wrote:
>> string like "memory exhausted" from what() if copying failed.
 
> How is returning incorrect data (in this case, the string "memory
> exhausted") better than, say, terminating the program?
 
Not much, typically. However, most C++ code resides in various
libraries, and a library should not decide on terminating the program on
its own, this should be the privilege of the main program.
 
> arbitrarily bad results.
 
> As I recall, treating an error message as data is part of what led to
> the Ariane 5 failure.
 
Right, like the doctor said, don't do that if it hurts. I.e. do not
treat an error message as data. It might change whenever someone feels
he can word the message better, and will certainly change when the
program is translated to French.
 
If the error message is there just for human information, it can't do
much harm, especially in the stage where the machine has become
unresponsive anyway.
 
My exception classes always contain an additional numeric error code
which is meant for programmatic consumption. Copying this integer does
not involve dynamic memory allocation, so there would be no danger of
exceptions when copying that.
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: