Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

basic oop question

137 views
Skip to first unread message

fir

unread,
Feb 4, 2020, 11:47:42 AM2/4/20
to
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

unread,
Feb 4, 2020, 11:59:36 AM2/4/20
to
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

unread,
Feb 4, 2020, 12:00:16 PM2/4/20
to
On 04.02.2020 17:59, Alf P. Steinbach wrote:
> On 04.02.2020 17:47, 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
>>
>> 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?
>
> 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

unread,
Feb 4, 2020, 12:12:27 PM2/4/20
to
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

unread,
Feb 4, 2020, 2:34:11 PM2/4/20
to
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

unread,
Feb 4, 2020, 3:04:31 PM2/4/20
to
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

unread,
Feb 4, 2020, 3:35:12 PM2/4/20
to
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

unread,
Feb 4, 2020, 4:15:31 PM2/4/20
to
class GameWindow
{
GameWindow();

RenderWindow window;
Food food;
Snake snake;
};

GameWindow::GameWindow() :
food{window}, snake{window, food}
{}

fir

unread,
Feb 4, 2020, 4:53:17 PM2/4/20
to
so it is said to be posibie? i will see, byr later

now im to frustratet forgor wht ** this langiege is

seeplus

unread,
Feb 4, 2020, 5:40:35 PM2/4/20
to
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

unread,
Feb 4, 2020, 6:22:31 PM2/4/20
to
snake is nota porblem, problem is this insanelly vrappy language which disallows
even poting this 3 things (window, food, snake) niecly put together

fir

unread,
Feb 4, 2020, 6:55:09 PM2/4/20
to
when i get back to it once a 10 years it is again only like "only for idiots"

fir

unread,
Feb 5, 2020, 10:15:33 AM2/5/20
to
extremally frustrating youre just like cant do it well (by "it" i mean the think i wrote initially, or at least something close

what goals are

1) i want to have objects connected by reference (thise references are obiect fields
like
a) Snake has a field Window& window
Snake has a field Food& food
b) Food has a field Window& window
c) Window has a field Snake& snake
Window has a field Food& food

i also want this references to be initialised in a poblem-lacking way,
this thing is problem lackng when
those objects take those refrences in a momnet fo creation, as this guarantees
evereything must be ok

2) this seems that this crappy language doeas not allow that
3) and a tries used to find something close arso failed (though something more or less crappy probably cant be found
4) more to say my all tries needed a
numerious tries when i changed the presence of those object from one place to another changed also places where those references were passed 9from constructors to functions), also changed it from references to pointers and vice versa, which involved idiotc changes in
biodies of those object codes..those changes was so utelly crapopy activiry normal language like c dont even know,
thios is simply 'sceptic tank' (pool/pond od shit) for years of c i managed to forget how this shit stinks



Bo Persson

unread,
Feb 5, 2020, 10:58:27 AM2/5/20
to
Ok...

So to be able to create a Food object, you first need to create a
Window. But before you can create the Window, you first have to create
the Food. Oh, and the Snake. But before you create the Snake...


Perhaps it is time to consider that some statements cannot be
formulated, however powerful a logic system you have. Not limited to
programming languages.

For example, the liar paradox https://en.wikipedia.org/wiki/Liar_paradox

A: This statement (A) is false.

If (A) is true, then "This statement is false" is true. Therefore, (A)
must be false. The hypothesis that (A) is true leads to the conclusion
that (A) is false, a contradiction.

If (A) is false, then "This statement is false" is false. Therefore, (A)
must be true. The hypothesis that (A) is false leads to the conclusion
that (A) is true, another contradiction. Either way, (A) is both true
and false, which is a paradox.


Not even C++ can solve this. :-)

fir

unread,
Feb 5, 2020, 11:17:59 AM2/5/20
to
atatemant A has not much to it

as to those references its physically totally able to get, you simpl may think you create a thing in one step or simply
cretere part of each (lke place them in ram so they got adresses) then you set the references

so its doable (this reference is mere a pointer anywey) but my question is fdifferent; if this crappy language dont allows me to do it (or maybe it does? by some trick or what) how the most close thing i can obtain?

(and note i get extrelmly frustrated yesterday by drowning in a swamp of deep shit trials of it,, as it was real swampshit..i got no strength to try)

so? maybe somebody is able to answer?

fibble said something but i dont see it cleraly so maybe some eleboration?

Jorgen Grahn

unread,
Feb 5, 2020, 2:18:04 PM2/5/20
to
On Wed, 2020-02-05, fir wrote:
> W dniu środa, 5 lutego 2020 00:55:09 UTC+1 użytkownik fir napisał:
>> W dniu środa, 5 lutego 2020 00:22:31 UTC+1 użytkownik fir napisał:
>> > W dniu wtorek, 4 lutego 2020 23:40:35 UTC+1 użytkownik seeplus napisał:
>> > > 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
...

> biodies of those object codes..those changes was so utelly crapopy
> activiry normal language like c dont even know, thios is simply
> 'sceptic tank' (pool/pond od shit) for years of c i managed to
> forget how this shit stinks

I see deciding not to try to help you was a wise decision.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Juha Nieminen

unread,
Feb 5, 2020, 2:23:56 PM2/5/20
to
fir <profes...@gmail.com> wrote:
> 2) this seems that this crappy language doeas not allow that

If you really want help to whatever problem you are having,
then perhaps it would be a better idea to be a bit more polite.

Of course putting some effort into writing something that's easier
to understand also helps.

fir

unread,
Feb 5, 2020, 2:36:41 PM2/5/20
to
but do informing me on that was?
if you se word help better stay away..
im pryty sure anybody on usenet who names an answer as an "help" is realy heavy idiot, much below the IQ level i could tolerate

same kind of idiot who could name a help really anything - were in fact exchanging messages with idiots is rather annoying ;c

so tnx much and keep away

Alf P. Steinbach

unread,
Feb 5, 2020, 3:08:50 PM2/5/20
to
On 05.02.2020 16:58, Bo Persson wrote:
> Either way, (A) is both true and false

No, A is neither true nor false.

You have just proved it can't be either. So it's neither.

:-)

- Alf

Chris M. Thomasson

unread,
Feb 5, 2020, 10:52:31 PM2/5/20
to
Y tells A the truth about X, how it has major problems... Then Y tells B
a lie about X, it works fine. Relative to B, X is true. However, wrt A's
perspective X is false.

X is both true and false wrt A and B's own personal perspectives due to
Y's deception.

OR:


Y tells A and B a lie about X. Now, both A and B think this false
deception is true.

Öö Tiib

unread,
Feb 6, 2020, 2:27:12 AM2/6/20
to
That design does not make sense. It is because we do
not want to draw only snake or only food, we want to draw
everything, empty grass as well.
* Snake game is in window,
* window has some grid in it,
* grid has cells,
* cells have states what is there, grass, food or some snake body part.
* body parts have direction and orientation.
Then there is score that is related to snake's body length
and difficulty of game that is related to game's tick speed. Each
tick some grid cells transform between states, the transformations
may be drawn immediately where those happen or done and then whole
grid is drawn. Rest of time game just waits for next tick or user's
key press.

>
> i also want this references to be initialised in a poblem-lacking way,
> this thing is problem lackng when
> those objects take those refrences in a momnet fo creation, as this guarantees
> evereything must be ok
>
> 2) this seems that this crappy language doeas not allow that
> 3) and a tries used to find something close arso failed (though something more or less crappy probably cant be found
> 4) more to say my all tries needed a
> numerious tries when i changed the presence of those object from one place to another changed also places where those references were passed 9from constructors to functions), also changed it from references to pointers and vice versa, which involved idiotc changes in
> biodies of those object codes..those changes was so utelly crapopy activiry normal language like c dont even know,
> thios is simply 'sceptic tank' (pool/pond od shit) for years of c i managed to forget how this shit stinks

If your whole point was to complain how hard it is to use C++ then that
was waste of time. C++ is not easy language and most other languages
can be learned faster than it.

fir

unread,
Feb 6, 2020, 10:43:10 AM2/6/20
to
it was not my point, it was side observation (really side)

as to sanke it only has its snake and its food (and also a window to place it onto ) so this division hace sense , but this crap (as it showed) language dislaow me to write it relatively rigt (as far as i know)

and all the attempts was exceptional torment

Mr Flibble

unread,
Feb 6, 2020, 2:48:30 PM2/6/20
to
I showed you what to do in my first reply. The problem isn't C++, the problem is your inability to recognize a cyclic dependency.

fir

unread,
Feb 6, 2020, 3:25:12 PM2/6/20
to
yes i know but i get so medical level pgysically frustrated that i cant judge how this could work

could yopu maybe write it as a some cnippet of code, compare to my oryginal of what i wanted to have:


int main()
{
Food food(window);
Snake snake(window, food);
Window window(snake, food);
}


class Food
{
Food(Window& w): window(w) {}
Window& window;
}

class Snake
{
Snake(Window& w, Food& f): window(w), food(f) {}

Window& window;
Food& food;
}

class Window
{
Window(Snake& w, Food& f): snake(s), food(f) {}

Snake& snake;
Food& food;
}


tis if work woulnt be yet such bad (not saing it woyld be specially good either) (but as i said this crpappy langage seems to force it only bad frustrating way)

Paavo Helde

unread,
Feb 6, 2020, 4:49:48 PM2/6/20
to
Here you are! Compiles fine and all cyclic references are in place!


class Window;

class Food
{
public:
Food(Window& w) : window(w) {}
Window& window;
};

class Snake
{
public:
Snake(Window& w, Food& f) : window(w), food(f) {}

Window& window;
Food& food;
};

class Window
{
public:
Window() : food(*this), snake(*this, food) {}
Food food;
Snake snake;
};

int main()
{
Window window;
Food& food = window.food;
Snake& snake = window.snake;
}



fir

unread,
Feb 6, 2020, 5:14:43 PM2/6/20
to
do you checked if tis compile (i got so heavy brain damages las time i dont wona to go so close to tis and check)

also: shouldnt i worry when placing objects on stack ? if some get fields of another objects etc (like vecotrs) they may grow bigger in size (esp i hate pointers and woyld not like to use them including new ) also personally i have nothing against making so called 'global'
objects


so can i put Window window; outside the main as a global making all its succesive fields also placed in global ram?

fir

unread,
Feb 6, 2020, 5:22:42 PM2/6/20
to
W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
>
> Here you are! Compiles fine

oj sorry i didnt noticed that.. if
so i will try it toomorrow

but what with this hlobal/non stack
placing? (asking as i really dont know this various c++ rules and quirks)

could i then generalize this way by
only making more and more of such 'objects' all glued by refferences (no pointers) ?

i huess fibble gave that answer but as i said i already was to braindameged to 'embrace' it mentally ;c

Paavo Helde

unread,
Feb 6, 2020, 5:48:25 PM2/6/20
to
On 7.02.2020 0:22, fir wrote:
> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
>>
>> Here you are! Compiles fine
>
> oj sorry i didnt noticed that.. if
> so i will try it toomorrow
>
> but what with this hlobal/non stack
> placing? (asking as i really dont know this various c++ rules and quirks)

You should not worry about placing things in stack, except for large
arrays (note that std::vector is not an array).

But of course, as long as there is only a single object of its own kind,
and its initialization does not depend on program arguments, and the
object is multithread-safe or it is guaranteed to be not accessed
concurrently, and the global initialization fiasco can be avoided, and
the hidden data flow through global statics is deemed acceptable, then
of course, the object can be made a global static as well.

>
> could i then generalize this way by
> only making more and more of such 'objects' all glued by refferences (no pointers) ?

Yes, but that would be ponderous.


fir

unread,
Feb 6, 2020, 5:50:30 PM2/6/20
to
why?

fir

unread,
Feb 6, 2020, 7:01:38 PM2/6/20
to
W dniu czwartek, 6 lutego 2020 23:22:42 UTC+1 użytkownik fir napisał:
> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
> >
> > Here you are! Compiles fine
>
> oj sorry i didnt noticed that.. if
> so i will try it toomorrow
>


ok, after abother (thoug smaller dose of frustration and crashes i managed to obtain something that compiles and not crashes)

i may post it, i think

the source of this code is somewhat 'winding'

some girl coder i know took some code aof snake probably from some youtube tutorial, cutted it downe transfered in into something weird (redundant etc), then i take it to simplify it, then i take it to turn it to 3 'classes' instead of 2, but i not repaired all names so some names gere would need to be changed
(like yts Mover it should be snake, attached_window should be renamed etc)


//main.cpp

#include "mover.h"
#include "food.h"
#include "window.h"


GameWindow window;

int main()
{
window.start();
return 0;
}














#ifndef FOOD_H
#define FOOD_H
#include <SFML/Graphics.hpp>

using namespace sf;

class GameWindow;
class Mover;

class Food
{
public:
Food( GameWindow& window);

void init();
void move2RandomPosition();
void draw();

Vector2f getPosition();
private:
Vector2u size = Vector2u(20, 20);
Vector2u position = Vector2u(0, 0);

GameWindow& attached_window;

};

#endif // FOOD_H










#ifndef MOVER_H
#define MOVER_H
#include <SFML/Graphics.hpp>

using namespace sf;

class Food;
class GameWindow;

class Mover
{
public:
Mover(GameWindow& window, Food &f);

void draw();

void setDirection(int x, int y);

void checkAndResolveEdgesCollision();
void checkAndResolveFoodColision();
void checkAndResolveSelfCollision();

void update();


private:
float x, y;
float dir_x, dir_y;
float scale;
int feedSize;

std::vector<Vector2f> segments_positions;

Food& food;
GameWindow& attached_window;
};

#endif // MOVER_H






#ifndef WINDOW_H
#define WINDOW_H
#include <SFML/Graphics.hpp>

using namespace sf;

class Mover;

class GameWindow
{

public:
GameWindow();

void start();

RenderWindow window;

private:

Food food;
Mover snake;




};

#endif // WINDOW_H







//food.cpp

#include "food.h"
#include "mover.h"
#include "window.h"

#include <ctime>
#include <iostream>

using namespace sf;
using namespace std;

Food::Food(GameWindow& w): attached_window(w)
{
}

void Food::init()
{
srand(time(nullptr));
this->move2RandomPosition();
}

void Food::draw()
{
RectangleShape rec;
rec.setSize(Vector2f(size));
rec.setPosition(Vector2f(position));
rec.setOutlineColor(sf::Color::Green);
rec.setOutlineThickness(-3);

attached_window.window.draw(rec);

}

void Food::move2RandomPosition()
{
Vector2u s = attached_window.window.getSize();

position = { size.x*( rand()%(s.x/size.x) ),
size.y*( rand()%(s.y/size.y) ) };


}

Vector2f Food::getPosition()
{
return Vector2f(position);
}





//mover.cpp

#include "mover.h"
#include <iostream>
#include <algorithm>
#include "food.h"
#include "window.h"



///////////////////////////

void Mover::draw()
{
sf::RectangleShape rec;
rec.setSize(sf::Vector2f(20,20));
rec.setPosition(sf::Vector2f(x,y));
rec.setFillColor(sf::Color::Yellow);
attached_window.window.draw(rec);

attached_window.window.draw(rec);

rec.setFillColor(sf::Color::Red);

for (int i=1;i<segments_positions.size(); ++i) // cant start from 0 cause then it become part of the tail immidiatly
{
rec.setPosition(segments_positions[i]);
attached_window.window.draw(rec);
}

}
/////////////////////////////////////////////

Mover::Mover(GameWindow& w, Food& food_): attached_window(w), food(food_)
{
scale = 20;

x = 0;
y = 0;
dir_x = 1;
dir_y = 0;
segments_positions.push_back(Vector2f(x,y));

feedSize = 2;
}

void Mover::checkAndResolveEdgesCollision()
{
Vector2u s = attached_window.window.getSize();

if(x<0) x=0;
if(x>s.x-20) x-=20;
if(y<0) y=0;
if(y>s.y-20) y-=20;
}

void Mover::setDirection(int vx, int vy)
{
dir_x = vx;
dir_y = vy;
}

void Mover::checkAndResolveFoodColision()
{
if(Vector2f(x,y) == food.getPosition())
{
feedSize+=2; // make the tail longer
food.move2RandomPosition();
}
}

void Mover::checkAndResolveSelfCollision()
{
for (int i = 0; i < segments_positions.size(); ++i)
for (int j = i+1; j < segments_positions.size(); ++j)
{
if(segments_positions[i] == segments_positions[j])
{
feedSize=2;
segments_positions.resize(2);
}
}

}


void Mover::update()
{
x = x + dir_x * scale; // move by scale
y = y + dir_y * scale;

checkAndResolveEdgesCollision();

segments_positions.push_back(Vector2f(x, y));

if(segments_positions.size() > feedSize )
segments_positions.erase(segments_positions.begin());

checkAndResolveFoodColision();
checkAndResolveSelfCollision();
}






//window.cpp
#include "SFML/Graphics.hpp"
#include <iostream>
#include "mover.h"
#include "food.h"
#include "window.h"

#include <ctime>
#include <vector>


GameWindow::GameWindow(): food(*this), snake(*this, food)
{
window.create(sf::VideoMode(720, 560), "ziemo snejk");
window.setFramerateLimit(10);

}


void GameWindow::start()
{
food.init();

while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed: window.close(); break;
case sf::Event::EventType::KeyPressed:
if(sf::Keyboard::Key::Up == event.key.code) snake.setDirection(0,-1);
if(sf::Keyboard::Key::Down == event.key.code) snake.setDirection(0,1);
if(sf::Keyboard::Key::Left == event.key.code) snake.setDirection(-1,0);
if(sf::Keyboard::Key::Right == event.key.code) snake.setDirection(1,0);
if(sf::Keyboard::Key::Escape == event.key.code) window.close();

break;
}
}


snake.update();
snake.draw();
food.draw();

window.display();
window.clear();
}
}




one could take project for windows with runtime needed here

http://minddetonator.htw.pl/sfml_wonsz3.zip

only mingw is needed to be at c:\mingw afair (i used mingw 5.1 and sdml 2.5.1)

i used bat for compilation

SET PATH=c:\mingw\bin;
g++ -s -std=c++11 -c food.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
g++ -s -std=c++11 -c mover.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2
g++ -s -std=c++11 -c window.cpp -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2

g++ -s -std=c++11 main.cpp -o "game" window.o food.o mover.o -Wl,-subsystem,windows -I"." -L"." -lsfml-system-2 -lsfml-window-2 -lsfml-graphics-2

pause


would you change somethinh except those some names i would also change but am to tired?

fir

unread,
Feb 6, 2020, 7:24:05 PM2/6/20
to
W dniu piątek, 7 lutego 2020 01:01:38 UTC+1 użytkownik fir napisał:
>
> would you change somethinh except those some names i would also change but am to tired?

i think i should rethink how to place thiose livrary (sfml) 'objects' cleaner

(i may ad i decided to learn/study this topic (of clean 'intermodular' design) a bit not becouse i want to learn c++ or opp but becuse i found some sense of that intermodular design 'on a side' (but not how c++ people (crowd) do that [primarly all pointers out, inheritance out too] but more focused on some idea of 'modular' (intermodular) composition itself (mor info on this was on comp.lang.c)

Chris M. Thomasson

unread,
Feb 6, 2020, 8:29:41 PM2/6/20
to
^^^^^^^^^^^^^

Sorry for the double negative wrt "false deception".


Y tells A and B a lie about X. Now, both A and B think Y's deception is
true.

fir

unread,
Feb 7, 2020, 11:02:55 AM2/7/20
to
would someone here improved something 'architectural' in that code above ? (if so why?) (im asking except names as names i am aware could be improved)

i wonder if not to put all 3 headers into one, and possibly all 4 cpp files also into one cpp file except i would divide it on cpp files thay i would simply include into on like text files - this seems probably simpler to me

Mr Flibble

unread,
Feb 7, 2020, 11:50:22 AM2/7/20
to
On 06/02/2020 22:48, Paavo Helde wrote:
> On 7.02.2020 0:22, fir wrote:
>> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
>>>
>>> Here you are! Compiles fine
>>
>> oj sorry i didnt noticed that.. if
>> so i will try it toomorrow
>>
>> but what with this hlobal/non stack
>> placing? (asking as i really dont know this various c++ rules and quirks)
>
> You should not worry about placing things in stack, except for large arrays (note that std::vector is not an array).
>
> But of course, as long as there is only a single object of its own kind, and its initialization does not depend on program arguments, and the object is multithread-safe or it is guaranteed to be not accessed concurrently, and the global initialization fiasco can be avoided, and the hidden data flow through global statics is deemed acceptable, then of course, the object can be made a global static as well.

No, just no. Global objects == global variables == bad. Stop breaking Law of Demeter. The only acceptable objects at global scope (in a namespace) are CONSTANTS.

My article on the subject: http://i42.co.uk/stuff/spaghetti.htm

fir

unread,
Feb 7, 2020, 12:15:13 PM2/7/20
to
W dniu piątek, 7 lutego 2020 17:50:22 UTC+1 użytkownik Mr Flibble napisał:
> On 06/02/2020 22:48, Paavo Helde wrote:
> > On 7.02.2020 0:22, fir wrote:
> >> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
> >>>
> >>> Here you are! Compiles fine
> >>
> >> oj sorry i didnt noticed that.. if
> >> so i will try it toomorrow
> >>
> >> but what with this hlobal/non stack
> >> placing? (asking as i really dont know this various c++ rules and quirks)
> >
> > You should not worry about placing things in stack, except for large arrays (note that std::vector is not an array).
> >
> > But of course, as long as there is only a single object of its own kind, and its initialization does not depend on program arguments, and the object is multithread-safe or it is guaranteed to be not accessed concurrently, and the global initialization fiasco can be avoided, and the hidden data flow through global statics is deemed acceptable, then of course, the object can be made a global static as well.
>
> No, just no. Global objects == global variables == bad. Stop breaking Law of Demeter. The only acceptable objects at global scope (in a namespace) are CONSTANTS.
>
> My article on the subject: http://i42.co.uk/stuff/spaghetti.htm
>


no fella
i dont know c++ but i know c a lot and may say someting (based on my years of
dealing with c)

1) what you call global are in fact not hlobal, this is "module level" variable
(when this module is usually a .dll)

2) what is comonly called by global is not bad, bad is a code that reaches/uses (reads/writes) those globals in special
haotic way (way which is clearly identified when you discover swamp of troubles related to dhat but is a bit hard to formally define, at least for me*)

(* it may be something related to a fact that c programs by nature are linear, becoyse if you make even (i forgot a word here not incuctive, not factal, not repetitive, say repetitive) repetitice stubs in that (which make tree-like structure of functions) it is still linear

- and if there is some global array or even set of arays (like say input geometry of trangles and iutpyt set of framebuffers) and your code work on it in
linear way, something lke stream of transformations (or maybe even broder set of well defined flows ) there is nothing wrong with it)

anyway tnx for that 2 answers about the same, was quite usable

fir

unread,
Feb 7, 2020, 12:27:36 PM2/7/20
to
W dniu piątek, 7 lutego 2020 18:15:13 UTC+1 użytkownik fir napisał:
> haotic way (way which is clearly identified when you discover swamp of troubles

same or close swamp is btw related to some troubles with events (but only sometimes also) it may be also related to linearity got broken

some is alos related to hidden states of some apis (maybe. i dont wont toi get my head back to it)

note c++ may increase it (which is kinde new as usualy it was api who brougt that swamp to people), as some oices of linear code are break down on opices in some classes..probablu those constructors and their limits bring that swamp, or maybe yet something other)

Bo Persson

unread,
Feb 7, 2020, 12:43:13 PM2/7/20
to
On 2020-02-07 at 18:14, fir wrote:
> W dniu piątek, 7 lutego 2020 17:50:22 UTC+1 użytkownik Mr Flibble napisał:
>> On 06/02/2020 22:48, Paavo Helde wrote:
>>> On 7.02.2020 0:22, fir wrote:
>>>> W dniu czwartek, 6 lutego 2020 22:49:48 UTC+1 użytkownik Paavo Helde napisał:
>>>>>
>>>>> Here you are! Compiles fine
>>>>
>>>> oj sorry i didnt noticed that.. if
>>>> so i will try it toomorrow
>>>>
>>>> but what with this hlobal/non stack
>>>> placing? (asking as i really dont know this various c++ rules and quirks)
>>>
>>> You should not worry about placing things in stack, except for large arrays (note that std::vector is not an array).
>>>
>>> But of course, as long as there is only a single object of its own kind, and its initialization does not depend on program arguments, and the object is multithread-safe or it is guaranteed to be not accessed concurrently, and the global initialization fiasco can be avoided, and the hidden data flow through global statics is deemed acceptable, then of course, the object can be made a global static as well.
>>
>> No, just no. Global objects == global variables == bad. Stop breaking Law of Demeter. The only acceptable objects at global scope (in a namespace) are CONSTANTS.
>>
>> My article on the subject: http://i42.co.uk/stuff/spaghetti.htm
>>
>
>
> no fella
> i dont know c++ but i know c a lot and may say someting (based on my years of
> dealing with c)
>
> 1) what you call global are in fact not hlobal, this is "module level" variable
> (when this module is usually a .dll)
>
> 2) what is comonly called by global is not bad, bad is a code that reaches/uses (reads/writes) those globals in special
> haotic way (way which is clearly identified when you discover swamp of troubles related to dhat but is a bit hard to formally define, at least for me*)
>

And one easy way to totally avoid these problems is to not have any
global variables. As soon as you have "some", you have one foot close to
the swamp.


fir

unread,
Feb 7, 2020, 12:54:32 PM2/7/20
to
no this way you will drown in c++ swamp

good way to aboid it simply not use this haotic acceses (mostly haotic writes

some examle imagine i habe a game that has a variable "background_color" that is uset then in some point of game loop to clear the screen and fill the vachrount with color of this variable holds

the "space" between this place when you can assign a variable and a place where it is in fact used to fill the screen seem to be a source of that swamp

(it is becouse you may set it in many points of your program, then reread it to (like for example you want to know what current bacgrond color is) giving false information even to yourself, or making false non existant operations etc

this is real swamp, but imagine you use framebuffer immediatelly (which is also 'global'), even if you fill it with green in one place then fill it with red in another place youre not in that swamp coz you make it physically (you may waste cpu power but that is side story and not related)

so this is not a globals who are bad but globals who are some kinda of 'contract' which might be broken

fir

unread,
Feb 7, 2020, 1:02:36 PM2/7/20
to
note it is based on my own thinking, and on my own experiance and thinking on it, not on what i read ober the net, this make it a bit more valuable

also note i mentioned ehose vents becouse
they also seem to define that kind of breakable contracts sometimes, so not using 'global' wariables dont make you safe

you may provide that breakable contract even by some method or functions and it still my probably put you into that swamp

Paavo Helde

unread,
Feb 7, 2020, 2:17:50 PM2/7/20
to
On 7.02.2020 18:50, Mr Flibble wrote:
> On 06/02/2020 22:48, Paavo Helde wrote:

>> But of course, as long as there is only a single object of its own
>> kind, and its initialization does not depend on program arguments, and
>> the object is multithread-safe or it is guaranteed to be not accessed
>> concurrently, and the global initialization fiasco can be avoided, and
>> the hidden data flow through global statics is deemed acceptable, then
>> of course, the object can be made a global static as well.
>
> No, just no. Global objects == global variables == bad. Stop breaking
> Law of Demeter. The only acceptable objects at global scope (in a
> namespace) are CONSTANTS.

Hmm, I guess my position was not really obvious. My bad, will add more
<sarcasm> tags next time ;-),

Mr Flibble

unread,
Feb 7, 2020, 2:48:38 PM2/7/20
to
Law of Poe probably applies in this case. Also I am a bit tired.

Richard Damon

unread,
Feb 11, 2020, 9:53:44 PM2/11/20
to
On 2/4/20 11:47 AM, 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
>
> 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
>

Simple:

extern Window window;
Food food(window);
Snake snake(window, food);
Window window(snake, food);

Food and Snake must not actual use the window object in their
constructors, but only use its address, so they can initialize a
reference member but not much more

fir

unread,
Feb 12, 2020, 4:39:06 AM2/12/20
to
hmm, interesting.. i will try next time as it came to late and now im doing something quite other, but good to know if it really works (if so lots of answer on stack overflow (trashbin itself) seem to be a trash), tnx

fir

unread,
Feb 12, 2020, 7:31:43 AM2/12/20
to
i must say again stack overflow is a place for extreme idiots - and probably anybody should know that, this shit is so unfair as mingw download page is - it is like made to aggreviate people

close to this is also a comp.lang.asm.x86 where there must be some hidden idiot as i wrote x86 assembler myself and cant post on normal assembly topics as some hiden moron deletes this for no reason

free usenet stil being so much better, only if extreme trols fortunatelly moved out
0 new messages