Sunday, November 23, 2014

Digest for comp.lang.c++@googlegroups.com - 25 updates in 6 topics

comp.lang.c++@googlegroups.com Google Groups
Unsure why you received this message? You previously subscribed to digests from this group, but we haven't been sending them for a while. We fixed that, but if you don't want to get these messages, send an email to comp.lang.c+++unsubscribe@googlegroups.com.
JiiPee <no@notvalid.com>: Nov 23 01:46PM

Hi, Just making a practice project (learning graphics, sockets,
wxWidgets). Its a ping pong game: playing against an opponent via
internet. It all works as fine/intended. But I am more interested in
making it professionally structured (not one of those beginner
structured ones again :)), as am trying to learn/practice C++ more.
The code is now (as you can see below) structured so that the
wxWidget window class has everything in it (PingPongFrame). Obviously
only window-based things should really be there, isn't it? I guess all
the graphics (using SFML library) should have their own class (Graphics)
- so all drawing and things like that should be there. All the game
related things (like moving objects etc) should be in a new class: Game.
Then I have the socket programming. But its code is very small so I
guess it does not need a class- I can just use it inside Game class.
 
So all of this I think I understand. My main question is that should I
put all the functions to be as member functions of classes or global
functions inside corresponding class header? I know we should avoid
member functions, but what would be a good rule here which ones to make
global functions and which ones members? Its easier to make them
members... Almost all of them are using private members, so is it just
best to make them members even though then I get a lot of member
functions, like 10-15? Also as you can see there are very many member
variables as well... should I put all of them inside Game and Graphics
classes, or group them into new structures?
 
The header of the game (window) class (I know its a bit mess, but I am
not sure yet how to group them so did not change much. Plus I know the
names are not well done, but I will fix it later...). sf:: refers to
SFML graphics library:
 
enum class Player {LEFT, RIGHT};
 
class PingPongFrame: public wxFrame
{
public:
void drawPanelMouseClick(float x, float y, bool right=false);
// called from a child window to pass mouse msg
PingPongFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~PingPongFrame();
private:
sf::Text pauseMessage;
sf::Text scoreMessage;
sf::Font font;
 
sf::RenderWindow m_sfmlWindow; // sfml window
sf::Texture GroundTexture;
sf::Texture BoxTexture;
 
sf::String m_tcp;
void runTcpServer(unsigned short port);
void runTcpClient(unsigned short port);
 
bool m_isServer_ = false;
 
sf::String m_message;
 
bool connected;
sf::TcpListener m_listener;
sf::TcpSocket m_socket;
void TcpListenClientMessages(unsigned short port);
void TcpServerStart(unsigned short port);
void TcpClientConnect(unsigned short port);
// paddless
sf::RectangleShape leftPaddle;
sf::RectangleShape rightPaddle;
sf::CircleShape ball;
sf::Sound ballSound;
sf::Sound ballSoundHit;
sf::SoundBuffer ballSoundBuffer;
sf::SoundBuffer ballSoundBufferHit;
 
std::vector<std::pair<sf::SoundBuffer, sf::Sound>> m_applauseSound;
std::vector<std::pair<sf::SoundBuffer, sf::Sound>> m_ohhSound;
void addSound(const char* file,
std::vector<std::pair<sf::SoundBuffer, sf::Sound>>& soundVec);
void playRandomSound(std::vector<std::pair<sf::SoundBuffer,
sf::Sound>>& soundVec);
 
float paddleSpeed = 400.f;
float ballSpeed = 120.f;
sf::Clock clock;
const float pi = 3.14159f;
int gameWidth = 800;
int gameHeight = 600;
sf::Vector2f paddleSize = sf::Vector2f(25, 100);
float ballRadius = 10.f;
float deltaTime = 0.0;
bool isPlaying = false;
float ballAngle = 0.f;
int m_scoreServer = 0, m_scoreClient = 0;
 
void drawField();
void startGame();
void updateSceen();
void moveBall();
void sendGameInfoToServer(int rightPaddleX, int rightPaddleY);
void sendGameInfoToClient();
bool parseMsgFromServer(const std::string& msg);
bool parseMsgFromClient(const std::string& msg);
 
void sendMsgToClient(const std::string& msg);
void doWin(Player winner, const char* msg);
void drawScore();
 
bool m_isSceenChanged = true;
bool m_serverWaitingForPositionResponce = false;
 
float scalX, scalY;
float sizeBoxX = 32.0;
float sizeBoxY = 32.0;
float sizeGroundX = 150.0;
float sizeGroundY = 50.0;
void initWorld();
void debugInt(int i);
void DrawPanel();
}
"Öö Tiib" <ootiib@hot.ee>: Nov 23 08:34AM -0800

On Sunday, 23 November 2014 15:46:22 UTC+2, JiiPee wrote:
> The code is now (as you can see below) structured so that the
> wxWidget window class has everything in it (PingPongFrame). Obviously
> only window-based things should really be there, isn't it?
 
It is art to achieve clear program structure ... but the basics are
simple.
 
If to look at your function names then those almost always contain
a verb and a noun (like "DrawPanel", "InitWorld", "DrawScore",
"MoveBall", etc.).
 
So in your mind there are things (indicated by these nouns
like "panel", "world", "score", "ball" etc.) whose responsibilities
(and data) have been given to frame.
 
You may want to move the responsibilities and data of a "ball" to a
class named "ball". Then you have a class of object that has knowledge
and responsibilities to look, behave, sound and interact with other
objects in role of a ball.
 
> the graphics (using SFML library) should have their own class (Graphics)
> - so all drawing and things like that should be there. All the game
> related things (like moving objects etc) should be in a new class: Game.
 
"Graphics" does not feel like a class of object ... notice that it is
plural. It is more likely a group of classes. Same is with "game objects".
Same is with the "communications" (servers and clients).
Wouter van Ooijen <wouter@voti.nl>: Nov 23 10:05AM +0100

Anyone knows a free C++ obfuscator that can do a single .cpp file?
 
It does not need to be CIA-proof, just mangling the internal literals
and names and removing the whitespace is probably enough.
 
Wouter van Ooijen
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 23 03:41PM

On 23/11/2014 09:05, Wouter van Ooijen wrote:
> Anyone knows a free C++ obfuscator that can do a single .cpp file?
 
> It does not need to be CIA-proof, just mangling the internal literals
> and names and removing the whitespace is probably enough.
 
What is the point of using such a tool? Either you are closed source in
which you don't need it or you are open source in which case you want
your code to be legible.
 
/Flibble
Wouter van Ooijen <wouter@voti.nl>: Nov 23 05:05PM +0100

Mr Flibble schreef op 23-Nov-14 4:41 PM:
 
> What is the point of using such a tool? Either you are closed source in
> which you don't need it or you are open source in which case you want
> your code to be legible.
 
The question remains whether the use has a point or not, but FYI: I am a
teacher and I want to give my studenst 10 versions of the implementation
of a class, one (hopefully) correct, and 9 each with one bug. Their task
is to write a set of tests for the class, and find the one correct
implementation, and for each bug the nature of the bug. Giving the plain
source would make a text diff approach too easy.
 
My students use different compilers (some of which I don't have), so it
is not practical for me to give them the object files.
 
Wouter
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 23 04:17PM

On 23/11/2014 16:05, Wouter van Ooijen wrote:
> source would make a text diff approach too easy.
 
> My students use different compilers (some of which I don't have), so it
> is not practical for me to give them the object files.
 
If your students are unable to determine what the code is supposed to do
due to obfuscation then how can they determine which has a "bug" or not?
Writing unit tests for obfuscated code sounds almost impossible. Your
assignment is both unrealistic and pointless.
 
/Flibble.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 23 04:19PM

On 23/11/2014 16:17, Mr Flibble wrote:
> due to obfuscation then how can they determine which has a "bug" or not?
> Writing unit tests for obfuscated code sounds almost impossible. Your
> assignment is both unrealistic and pointless.
 
Additionally using a diff tool shows initiative and is what your
students would use in the real world anyway.
 
/Flibble
Wouter van Ooijen <wouter@voti.nl>: Nov 23 05:33PM +0100

Mr Flibble schreef op 23-Nov-14 5:17 PM:
>> is not practical for me to give them the object files.
 
> If your students are unable to determine what the code is supposed to do
> due to obfuscation
 
What this code is *supposed* to do is of course provided by a
specification.
 
But again, that does not matter much for the original question. Do you
have anything meaningfull to say on that?
 
Wouter
Chicken Mcnuggets <chicken@mcnuggets.com>: Nov 23 08:35AM

I'm currently learning C++ from the following books:
 
The C++ Programming Language 4th Edition
The C++ Standard Library 2nd Edition
 
and am thinking about buying:
 
Effective C++ 3rd Edition
More Effective C++ 1st Edition
Effective Modern C++ 1st Edition.
 
Are these books worth getting? I've read some good things about them and
they seem to come highly recommended. Or are there any other books I
should be looking at instead?
 
Anything that will increase my skill at C++ is what I am after. I have a
project I am working on which I'd really like to get finished using C++
and I want to make sure I'm not making any mistakes with it.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 23 08:54AM

On Sun, 2014-11-23, Chicken Mcnuggets wrote:
 
> Are these books worth getting? I've read some good things about them and
> they seem to come highly recommended. Or are there any other books I
> should be looking at instead?
 
I can't comment -- TC++PL is the only book I've really read. I do
agree you need another book to really grasp the standard library, and
grasping the standard library is crucial.
 
> Anything that will increase my skill at C++ is what I am after. I have a
> project I am working on which I'd really like to get finished using C++
> and I want to make sure I'm not making any mistakes with it.
 
Mostly because I'm curious: what things are you comfortable with now,
and which areas do you find problematic? When you're programming in
C++, I mean.
 
I can imagine knowing Python being a problem: it's in some ways the
opposite of C++, and in other ways quite similar.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Chicken Mcnuggets <chicken@mcnuggets.com>: Nov 23 09:05AM

On 23/11/14 08:54, Jorgen Grahn wrote:
 
> Mostly because I'm curious: what things are you comfortable with now,
> and which areas do you find problematic? When you're programming in
> C++, I mean.
 
The big problem area for me with C++ is templates and how to incorporate
them into my code (and whether I should do or if I should use another
solution instead). I guess the problem is I've never used something like
C++ template system. From what I have read they are Turing complete so
theoretically I should be able to make really good use of them but I'm
stumped on when and how.
 
To be fair I haven't read the detailed section on them in The C++
Programming Language yet but yeah, that is my main problem area.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 23 09:55AM

On Sun, 2014-11-23, Chicken Mcnuggets wrote:
> them into my code (and whether I should do or if I should use another
> solution instead). I guess the problem is I've never used something like
> C++ template system.
 
The way I use them is I look for repetition and duplication in my code
-- two functions that are the same but operate on two different types,
or call different helper functions. Or a function which takes a
vector<Foo> but really just needs a [begin, end) pair of iterators to
Foo. Then I rewrite it with a template.
 
It helps if you already structure your code so similar things are done
in similar ways, and similar things have similar names. E.g. much of
the standard library would be useless if there weren't common patterns
in the naming and behavior of different containers and iterators. But
you may be familiar with that idea from Python already ...
 
> From what I have read they are Turing complete so
> theoretically I should be able to make really good use of them but I'm
> stumped on when and how.
 
That's the metaprogramming aspect of templates. It's probably useful
some of the time (definitely useful for library writers), but don't
let it stand in the way of everyday template usage.
 
> To be fair I haven't read the detailed section on them in The C++
> Programming Language yet but yeah, that is my main problem area.
 
Are you referring to the section on metaprogramming? Again, I
recommend using templates, but skipping TMP.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Wouter van Ooijen <wouter@voti.nl>: Nov 23 11:09AM +0100

Jorgen Grahn schreef op 23-Nov-14 10:55 AM:
>> Programming Language yet but yeah, that is my main problem area.
 
> Are you referring to the section on metaprogramming? Again, I
> recommend using templates, but skipping TMP.
 
I second that. I use templates a lot, but no metaprogramming. If you
need compile-time calculations use constexpr.
 
But for me it is important to understand SFINAE and how to use it to
select between alternative template specializations.
 
Wouter
 
Chicken Mcnuggets <chicken@mcnuggets.com>: Nov 23 01:48PM

On 23/11/14 10:09, Wouter van Ooijen wrote:
 
> Wouter
 
>> /Jorgen
 
Cool, thanks for the tips. I read a bit about SFINAE but my knowledge of
templates isn't really good enough at the moment to grasp what it is all
about. I'll do some more reading and come back to it.
 
My big problem at the moment is writing C++ code as if it were C code so
I have a tendency to use the C standard library more than the C++
standard library. I guess this is just down to ignorance of the C++
standard library and is considered poor practice for C++ code. I
certainly want to rewrite bits of my code to take full advantage of C++.
 
On the other hand some "bad practices" in C++ I have read about such as
using C style arrays and raw pointers rather than references seem to
make some things a little lighter when a full on C++ solution might add
extra overhead. Is it really considered that bad to have these things in
your code? The project I am working on is going to be open source, am I
going to turn off C++ programmers if I have to much C style stuff in the
code even if it is abstracted away behind classes?
JiiPee <no@notvalid.com>: Nov 23 02:09PM

On 23/11/2014 13:48, Chicken Mcnuggets wrote:
> to make some things a little lighter when a full on C++ solution might
> add extra overhead. Is it really considered that bad to have these
> things in your code?
 
But that depends on how effiecient/speed -crucial code it is. Most of my
code I have done it does not matter a bit if there is one cycle more
than using a C-version or more simple structure. So then it is better to
always use C++ structures/ways. It's a trade off between a good code
structure and efficiency. Plus many times C++ code is surely faster than
similar C- structure done by a programmer (I guess std::vector being one
of them compared to dynamic C-arrays ... not sure about this though but
my understanding). So I guess we would like to see an example where you
think C would be better.
But yes, if I find that some C-array or bitmask is faster than C++
and I have to loop it 10000000 times a second, then of course I use
C-type of version.
 
But I have understood (and its in the books as well) that do not try to
optimize too much first. And optimize only if it gives significant
difference after *tests*.
 
> The project I am working on is going to be open source, am I going to
> turn off C++ programmers if I have to much C style stuff in the code
> even if it is abstracted away behind classes?
 
I would personally not mix C and C++ styles. Its good to have same style
everywhere imo. But again, if its a *proven* clear efficiency issue,
then I would use C.
 
And yes, I do not like to see C code if I know it can be done with C++
as well :). Just my taste :).
Richard Damon <Richard@Damon-Family.org>: Nov 23 09:23AM -0500

On 11/23/14, 4:05 AM, Chicken Mcnuggets wrote:
> stumped on when and how.
 
> To be fair I haven't read the detailed section on them in The C++
> Programming Language yet but yeah, that is my main problem area.
 
The first thing to note is that just because you can do something,
doesn't mean you should. Templates allow you to implement some very
powerful techniques, but when not used well can make you code an
unreadable mess.
 
My opinion is that the more complicated the meta-programming, the
simpler the interface should be (and the better the documentation should
be).
 
Templates are very good at providing generic operations that can be done
to a wide variety of types (that meet a common interface).
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 23 02:53PM

On Sun, 2014-11-23, Chicken Mcnuggets wrote:
...
> I have a tendency to use the C standard library more than the C++
> standard library. I guess this is just down to ignorance of the C++
> standard library and is considered poor practice for C++ code.
 
Depends on which part of the library you're talking about. The most
important parts of the C++ library don't exist in C: the containers
and algorithms.
 
> using C style arrays and raw pointers rather than references seem to
> make some things a little lighter when a full on C++ solution might add
> extra overhead.
 
There's no performance penalty for references or std::array. For
other things ... one of the revelations I had when I started using C++
was when I realized that:
 
(a) A lot of the containers (string, vector, map ...) imply dynamic memory
allocation, when in C I would have used an array and hoped it
would be large enough.
(b) I could never or almost never see this cause any practical problems!
 
It's worth it.
 
> Is it really considered that bad to have these things in
> your code?
 
It depends ... they /do/ have their uses, but not where C++ has better
alternatives.
 
> The project I am working on is going to be open source, am I
> going to turn off C++ programmers if I have to much C style stuff in the
> code even if it is abstracted away behind classes?
 
Personally I can tolerate almost anything, if it's encapsulated in a
small class or function, and if that class or function as a whole
behaves normally (is exception-safe, has well-defined states, can be
copied if it makes sense, cannot be copied if it doesn't, and so on).
That's one of the main reasons classes exist.
 
Speaking of small classes: one of the common beginner's mistakes seems
to be to design a few big classes. IME you gain a lot more from
designing the many small, short-lived objects. Try to do that.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Glen Stark <mail@no-spam.glenstark.net>: Nov 23 01:01PM

I would like to write a tool to determine which (if any) #include
statements are superfluous to a particular c++ file. I know that there's
a project to do this using clang, but I would like to do the same thing
for g++.
 
I'm a reasonably competent and knowledgeable c++ programmer, and it would
be my language of choice for implementing the tool.
 
As I understand it the gcc preprocessor provides a complete ast which
would allow me to do everything that's needed, but I'm having a hard time
getting started.
 
Can anyone help me out? I'm particularly interested in links to good
documentation, but if anyone has any tips how they would go about doing
something like this, I'd be pleased hear them.
 
Thanks for your time,
 
Glen
Marcel Mueller <news.5.maazl@spamgourmet.org>: Nov 23 03:51PM +0100

On 23.11.14 14.01, Glen Stark wrote:
> statements are superfluous to a particular c++ file. I know that there's
> a project to do this using clang, but I would like to do the same thing
> for g++.
 
You should know that this cannot be solved by a algorithm only. Simply
because #include 1 might depend on #include 2 and so each file that
includes #include 1 does no longer need #include 2 directly. But the
dependency of #include 1 on #include 2 might or might not be a
guaranteed property. So including #2 might be required to have portable
code.
 
Second example. Think of a .h/.cpp pair for a class. The .cpp always
include the corresponding .h, so any include dependency satisfied with
indirect include from .h need not to be repeated in the .cpp file. But
if your .h includes a .h from another class, then the indirect includes
from the other header might be removed in the other header in the future
resulting in a build break at an unexpected location. And if you are in
a library the build break may happen in another project where no build
server will check this for you.
 
> I'm a reasonably competent and knowledgeable c++ programmer, and it would
> be my language of choice for implementing the tool.
 
I would prefer a Eclipse CDT Plugin. Simply because you have a framework
there that knows about references of symbols. But be aware even CDT does
not understand all flavors of C++ dependencies. So you might remove too
much includes in rare cases.
 
> As I understand it the gcc preprocessor provides a complete ast which
> would allow me to do everything that's needed, but I'm having a hard time
> getting started.
 
This are technical dependencies. They are not portable as mentioned above.
 
> Can anyone help me out? I'm particularly interested in links to good
> documentation, but if anyone has any tips how they would go about doing
> something like this, I'd be pleased hear them.
 
You need a database with relations of standard symbols to standard
include files. Then you can check the files for this standard symbol
references and if you get a hit you know the particular include should
be referenced from that file.
 
 
Marcel
"K' Dash" <adnanrashidpk@gmail.com>: Nov 23 03:22AM -0800

void GUID_LMS_application::RemoveEntry (GUID_address GUIDaddress)
{
Entry entry;
 
for (std::vector<Entry>::iteratori=m_listentry.begin();i!=m_listentry.end ();i++)
{

if (GUIDaddress== entry.GetGUIDaddress( ))
{
//std::cout<<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n";
 
m_listentry.erase (i);
break;
}
}
}
 
 
dear all i want to delete the entry from a vector. but this piece of code is not working. The if condition is not succesful but my vecoter hold the entry. please help me
Jorgen Grahn <grahn+nntp@snipabacken.se>: Nov 23 11:57AM

On Sun, 2014-11-23, K' Dash wrote:
...
> Entry entry;
...
> if (GUIDaddress== entry.GetGUIDaddress( ))
...
> dear all i want to delete the entry from a vector. but
> this piece of code is not working. The if condition is not succesful but
> my vecoter hold the entry. please help me
 
Besides a lot of other problems which seem to come from bad copy &
paste, you never initialize 'entry'.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
JiiPee <no@notvalid.com>: Nov 23 12:03PM

On 23/11/2014 11:22, K' Dash wrote:
> }
> }
 
> dear all i want to delete the entry from a vector. but this piece of code is not working. The if condition is not succesful but my vecoter hold the entry. please help me
 
You create an entry variable but you do not fill it with anything... so
it's an empty object and then you use it in if condition, or is it
empty? Also you are looping i but you do not seem to use it in anyway in
the if-condition.
 
not sure, but I guess something like:
 
if (GUIDaddress== i->GetGUIDaddress( ))
bartekltg <bartekltg@gmail.com>: Nov 23 02:56PM +0100

On 23.11.2014 12:22, K' Dash wrote:
> ();i++) { if (GUIDaddress== entry.GetGUIDaddress( )) {
> //std::cout<<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n";
 
> m_listentry.erase (i); break; } } }
 
WTF?
 
 
> dear all i want to delete the entry from a vector. but this piece of
> code is not working. The if condition is not succesful but my vecoter
> hold the entry. please help me
 
Where is the 'i' in the condition?
If the condition is meet, you just erase the first element.
 
 
Also, be careful:
http://en.cppreference.com/w/cpp/container/vector/erase
"Invalidates iterators and references at or after the point of
the erase, including the end() iterator. "
 
Bonus
http://en.wikipedia.org/wiki/Erase-remove_idiom
 
brt
JiiPee <no@notvalid.com>: Nov 23 02:13PM

On 23/11/2014 13:56, bartekltg wrote:
 
> Bonus
> http://en.wikipedia.org/wiki/Erase-remove_idiom
 
> brt
 
But he breaks the loop after erase, so I guess its fine.
Luca Risolia <luca.risolia@linux-projects.org>: Nov 23 12:42AM +0100

Il 22/11/2014 23:25, Öö Tiib ha scritto:
>> }
 
> No. Most simple is to use standard library functions.
> Can't get simpler by doing recursion instead of it.
 
The only fact that using standard algorithms makes your code longer
should be a good a sign that they are going to be useful to solve your
problem, not to mention that they can hardly help for converting
integers of any size.
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: