Thursday, June 8, 2017

Digest for comp.lang.c++@googlegroups.com - 18 updates in 4 topics

JiiPee <no@notvalid.com>: Jun 08 05:27AM +0100

Say I have this kind of class structure:
 
class Game
{
 
...
 
private:
Player m_player;
bool m_pointsChanged{false};
};
 
class Player
{
 
...
 
private:
int points;
};
 
When some function in the Game class modifies players points the
m_pointsChanged needs to change to true.
 
What would be the best way to do this? I have this kind of situation
many times. I would like it so it is not possible to modify points alone
without m_pointsChanged being also updated. So for example:
 
class Player
{
 
void setPoint(int pt) { points = pt; }
 
private:
int points;
};
 
is not acceptable because then we could (accidentally) change points
from Game class doing:
 
m_player.setPoint(24);
 
and m_pointsChanged would not be updated (thus having a wrong value).
 
I was thinking these things:
 
1) should Player be inside the Game class if the points are so closely
linked with the modified flag?
 
2) This is one solution:
 
class Player;
 
class Game
{
private:
 
static void setPlayerPoints(Game& game, Player& player);
 
Player m_player;
bool m_pointsChanged{false};
};
 
class Player
{
private:
 
friend static void Game::setPlayerPoints(Game& game, Player& player);
 
int points;
};
 
and then player's points can only be set by using this setPlayerPoints
function (Player class does not have any setters for points). But the
problem is then that Player does not have setters.... is this a good idea?
 
3) I could create a setter in Player class like this:
 
class Player
{
 
public:
 
void setPoints(WordScrambleGame& game, int newPoints);
 
...
 
};
 
and then make Player a friend of Game. but the problem with this is that
then Player gets access to many private members of Game class it does
not need to get access. I wish I could limit the friend access to only
one variable... but its not in C++ i guess?
 
 
Any ideas? I have this situation in other projects as well... always
thinking about this ;).
Jerry Stuckle <jstucklex@attglobal.net>: Jun 08 08:55AM -0400

On 6/8/2017 12:27 AM, JiiPee wrote:
> one variable... but its not in C++ i guess?
 
> Any ideas? I have this situation in other projects as well... always
> thinking about this ;).
 
class Player
{
public:
void setPoints(int pt) {
if (points != pt) [
points = pt;
points_modified = true;
}
}
 
private:
int points;
bool points_modified;
};
 
And ensure the only change to points in the Player class is through the
setPoints method.
 
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
JiiPee <no@notvalid.com>: Jun 08 04:24PM +0100

On 08/06/2017 13:55, Jerry Stuckle wrote:
> };
 
> And ensure the only change to points in the Player class is through the
> setPoints method.
 
True this works (and might be the solution... did not even think about
this), but a small problem here is that now I need to loop all the
players always to know if one of them is changed. In my version the Game
class keeps record if one is changed so no need to loop the players.
Although in real situation there will not be more than 100 players. so
doing the loop always if very fast. You dont see this a problem that I
always have to loop the 100 players to know if one is changed? If the
Game class keeps the record of this then would not need to loop.
JiiPee <no@notvalid.com>: Jun 08 04:27PM +0100

On 08/06/2017 13:55, Jerry Stuckle wrote:
> };
 
> And ensure the only change to points in the Player class is through the
> setPoints method.
 
The small downside is that if I have 100 players I always have to loop
all the players to know if one is changed. Also when I want to set them
"unchanged" again I have to loop 100 players to do that. so 2 x 100
operations. But is this the best way still to do this?
scott@slp53.sl.home (Scott Lurndal): Jun 08 03:30PM

>all the players to know if one is changed. Also when I want to set them
>"unchanged" again I have to loop 100 players to do that. so 2 x 100
>operations. But is this the best way still to do this?
 
I'd probably put a callback into the setPoints method that would
call a std::function and that function would handle whatever you
need to do when the points value changes.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jun 08 06:30PM +0200

On 08-Jun-17 6:27 AM, JiiPee wrote:
 
> ...
 
> private:
> Player m_player;
 
Are you sure there is only a single player?
 
That's a somewhat rhetorical question because in the later discussion
you talk about looping over 100 players. So let's assume you meant
 
vector<Player> m_players;
 
 
> };
 
> When some function in the Game class modifies players points the
> m_pointsChanged needs to change to true.
 
Oh, like a dirty-flag.
 
That's simple: let each player know which Game it belongs to, e.g by
having a pointer to that Game instance.
 
 
> What would be the best way to do this? I have this kind of situation
> many times.
 
You need to define "best" for THAT question to make sense.
 
And I think that by properly defining what you mean by "best", you will
possibly have answered your own question already. ;-)
 
 
[snip]
 
 
Cheers & hth.,
 
- Alf
"Öö Tiib" <ootiib@hot.ee>: Jun 08 09:32AM -0700

On Thursday, 8 June 2017 18:27:28 UTC+3, JiiPee wrote:
> all the players to know if one is changed. Also when I want to set them
> "unchanged" again I have to loop 100 players to do that. so 2 x 100
> operations. But is this the best way still to do this?
 
In your initial code post you had "Game" with "Player m_player;" member.
That means Game has one and only one Player component.
From above sentence "if I have 100 players" we can however conclude
that now your Game can have big amount of Players. That is entirely
different situation. The "best" for these two situations is different.
 
Have reference to Game in Player. Then when Player's points
will change then it can inform Game about it that sets its
points_modified.
JiiPee <no@notvalid.com>: Jun 08 05:54PM +0100

On 08/06/2017 16:30, Scott Lurndal wrote:
> I'd probably put a callback into the setPoints method that would
> call a std::function and that function would handle whatever you
> need to do when the points value changes.
 
ok, one possiblitiy.. have to think about this
JiiPee <no@notvalid.com>: Jun 08 05:59PM +0100

On 08/06/2017 17:30, Alf P. Steinbach wrote:
 
> Are you sure there is only a single player?
 
no sorry, obviously in a game many players :). Just wanted to keep it
simple. Ye could be even like 200 players. a vector of players
 
 
> Oh, like a dirty-flag.
 
> That's simple: let each player know which Game it belongs to, e.g by
> having a pointer to that Game instance.
 
oh ok, the other person also said this.... ok I ll think about this next.
 
 
>> What would be the best way to do this? I have this kind of situation
>> many times.
 
> You need to define "best" for THAT question to make sense.
 
in a way of keeping things "private"/secure. so that when ever points
are changed then absolutely surely the flag is also modified.. and
points cannot be changed without the flag to be modified. That was my
aim. But also I dont want player class getting access to all private
members of the Game. Speed is not an issue necessary (although I dont
like so much looping for nothing really?).
 
 
 
> And I think that by properly defining what you mean by "best", you
> will possibly have answered your own question already. ;-)
 
hehe.
Paavo Helde <myfirstname@osa.pri.ee>: Jun 08 08:08PM +0300

On 8.06.2017 7:27, JiiPee wrote:
 
> private:
> int points;
> };
 
One option:
 
class Player {
public:
Player(bool& modifiedRef): modified_(modifiedRef) {}
void ChangePoint(int x) {
points = x;
modified_ = true;
}
private:
bool& modified_;
int points;
};
 
Game::Game(): m_player(m_pointsChanged) {}
 
This is basically a callback mechanism without over-engineering.
JiiPee <no@notvalid.com>: Jun 08 06:17PM +0100

On 08/06/2017 18:08, Paavo Helde wrote:
> };
 
> Game::Game(): m_player(m_pointsChanged) {}
 
> This is basically a callback mechanism without over-engineering.
 
 
oh thanks, one more solution :). I already got some other good ones..
but good to know all this. You guys know these things for sure :).
 
Interesting.... so i would pass the Game modified-flag to player by
reference... hmm. This might actually be the best if I understood
correctly, but have to first think.
 
scott@slp53.sl.home (Scott Lurndal): Jun 08 05:32PM


>Interesting.... so i would pass the Game modified-flag to player by
>reference... hmm. This might actually be the best if I understood
>correctly, but have to first think.
 
Inheritance may not be the correct approach if there is an
1:N relationship between game and player.
JiiPee <no@notvalid.com>: Jun 08 07:38PM +0100

On 08/06/2017 18:32, Scott Lurndal wrote:
>> correctly, but have to first think.
> Inheritance may not be the correct approach if there is an
> 1:N relationship between game and player.
 
but there is no inheritance here
Bilal <bilalcisco@googlemail.com>: Jun 08 12:08AM -0700

Dear group members,
 
I am newbie and learning c++ so bear with my posts.
 
I found a code on internet regarding inserting a node on the linked list. I ran it and it works perfectly. In this code the function 'Insert(data,n)' creates a node in the nth position with a payload 'data'. My question is why the memory allocated on the heap is not freed before the control is returned to the main function.
Here is the 'Insert' function below:
 
------------------------------
void Insert(int data, int n)
{
int i;
struct Node* temp1 = new Node;
temp1->data = data;
temp1->next = NULL;
if (n == 1)
{
temp1->next = head;
head = temp1;
return;
}
struct Node* temp2 = head; /*Another pointer to Node named temp2. Pointing to head first node*/
for (i = 0; i < n - 2; i++)
{
temp2 = temp2->next;
}
 
temp1->next = temp2->next;/*set the next or the link of newly created node as the link field of this (n-1) then we can adjust the link of this (n-1) the node point to our newly created node */
 
temp2->next = temp1;
}
-------------------------------
 
Thanks in advance.
-Bilal
Ian Collins <ian-news@hotmail.com>: Jun 08 07:58PM +1200

On 06/ 8/17 07:08 PM, Bilal wrote:
> 'Insert(data,n)' creates a node in the nth position with a payload
> 'data'. My question is why the memory allocated on the heap is not
> freed before the control is returned to the main function.
 
Simply because you don't explicitly free it!
 
Dynamic memory has to be manually allocated and freed. More often than
not dynamically allocated memory has a lifetime greater than the
function where it is originally allocated.
 
--
Ian
Barry Schwarz <schwarzb@dqel.com>: Jun 08 01:29AM -0700

On Thu, 8 Jun 2017 00:08:43 -0700 (PDT), Bilal
 
>------------------------------
>void Insert(int data, int n)
>{
<snip>
>}
 
If you are asking why the memory was not freed as the function
returns, the same way automatic variables are destroyed at that time,
it is because memory allocated with new remains allocated until it is
specifically freed with delete.
 
If you are asking why the function does not delete the allocated
memory, it is because then the linked list would be broken. Any code
stepping through the list would cause undefined behavior as soon as it
tried to evaluate or dereference the address of the deleted memory.
 
--
Remove del for email
killet@killetsoft.de: Jun 07 11:13PM -0700

Hello,
who develops programs with geodetic functionality like coordinate
transformations or distance calculations, can use geodetic functions
of my GeoDLL. The Dynamic Link Library can easily be used with most
of the modern programming languages like C, C++, C#, Basic, Delphi,
Pascal, Java, Fortran, Visual-Objects and others or as ANSI C source
code.
 
As most of the programs with geodesic functions are developed in C,
C++ and C#, I've written a template for direct use in Microsoft
Visual Studio, which can be downloaded with all required files from
the URL http://www.killetsoft.de/zip/GeoTestCpp.zip. In the template
some coordinate transformations with GeoDLL functions are performed.
I hope this template can help with the geodetic development work.
 
By the way, the new GeoDLL version 17.17 now provides support of
Polygonal Validity Scopes in NTv2 files and EPSG compatibility. Just
google for GeoDLL!
 
Fred
Email: info_at_killetsoft.de
woodbrian77@gmail.com: Jun 07 06:57PM -0700

This video:
 
https://duckduckgo.com/?q=c%2B%2Bnow+2017++%22type+safe+programming%22&t=qupzilla&iar=videos
 
was really impressive to me. The creativity and ingenuity that
he brings to the topic is great.
 
On one of his slides he writes:
 
Define. More. Types.
 
(And don't do implicit conversions.)
-----------------------------------------------------------------
 
I'm thankful to G-d for this breath of fresh air.
 
 
Brian
Ebenezer Enterprises - Enjoying programming again.
http://webEbenezer.net
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: