www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Not sure how to translate this C++ variable to D.

reply WhatMeWorry <kheaser gmail.com> writes:
-------------------- sprite_renderer.h --------------

class SpriteRenderer
{
...
};

-------------------- game.cpp ------------------------

#include "sprite_renderer.h"

SpriteRenderer  *Renderer;

Game::Game(GLuint width, GLuint height)
	: State(GAME_ACTIVE), Keys(), Width(width), Height(height)
{
   ...
}

-------------------------------------------------------

What is the best (or any) approach for translating SpriteRenderer 
*Renderer;
to D?  Thanks.

Sorry if this is common knowledge.  I tried taking due diligence 
with the documentation.
Mar 14 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/14/2016 03:14 PM, WhatMeWorry wrote:
 -------------------- sprite_renderer.h --------------

 class SpriteRenderer
 {
 ...
 };
Same thing in D without the semicolon. :)
 -------------------- game.cpp ------------------------

 #include "sprite_renderer.h"

 SpriteRenderer  *Renderer;
following is sufficient: SpriteRenderer Renderer; // Although, I would name it 'renderer' However, unlike C++, that variable is thread-local, meaning that if you have more than one thread, each will have their own variable. If you really need it, in multithreaded code you may want to define it shared: shared(SpriteRenderer) renderer; But then you will have to deal with thread synchronization.
 I tried taking due diligence with the documentation.
There is something here: http://ddili.org/ders/d.en/class.html#ix_class.variable,%20class Ali
Mar 14 2016
parent reply WhatMeWorry <kheaser gmail.com> writes:
On Monday, 14 March 2016 at 22:19:50 UTC, Ali Çehreli wrote:
 On 03/14/2016 03:14 PM, WhatMeWorry wrote:
 -------------------- sprite_renderer.h --------------

 class SpriteRenderer
 {
 ...
 };
Same thing in D without the semicolon. :)
 -------------------- game.cpp ------------------------

 #include "sprite_renderer.h"

 SpriteRenderer  *Renderer;
D. So, the following is sufficient: SpriteRenderer Renderer; // Although, I would name it 'renderer' However, unlike C++, that variable is thread-local, meaning that if you have more than one thread, each will have their own variable. If you really need it, in multithreaded code you may want to define it shared: shared(SpriteRenderer) renderer; But then you will have to deal with thread synchronization.
 I tried taking due diligence with the documentation.
There is something here: http://ddili.org/ders/d.en/class.html#ix_class.variable,%20class Ali
Ok. I was trying something more D like, by doing: SpriteRenderer Renderer = new SpriteRenderer(); I believe this won't work because I'm trying to allocate memory outside of any class, structure, or function? May I ask sort of an aside question. In large projects with 1000s of line of code and many many modules, classes, structs, and functions; is it probably true that most of the time the vast majority of variables are going to be inside one of above constructs? And is there a name for the variables that fall outside of the above constructs? I see so many tiny code snippets in books and docs, that when I do look at large dub/github projects, I'm not sure how to organize the "stuff" that slops over the edges.
Mar 15 2016
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 03/15/2016 07:29 AM, WhatMeWorry wrote:

 SpriteRenderer Renderer;  // Although, I would name it 'renderer'
 Ok. I was trying something more D like, by doing:

 SpriteRenderer Renderer = new SpriteRenderer();
That would work if the variable were const or immutable and if everything needed to execute the expression were available at compile time: const(SpriteRenderer) renderer = new SpriteRenderer(); immutable(SpriteRenderer) renderer = new SpriteRenderer(); (Note: Associative arrays literals still cannot be initialized with that syntax and arrays may have performance implications.) For mutable variables and for anything in general, module variables can be initialized in 'statit this()' and 'shared static this()' scopes: static this() { renderer = new SpriteRenderer(); } The difference is that 'static this()' is executed at runtime before any code in the module gets executed. It is like the constructor of the whole module. (You can have many disjoint 'static this()' blocks, which would all get executed.)
 In large projects with 1000s of
 line of code and many many modules, classes, structs, and functions; is
 it probably true that most of the time the vast majority of variables
 are going to be inside one of above constructs?
In D, everything will be inside one of those constructs because you included "modules". :) Like many modern languages, D lacks a global scope. Although we are advised to use the global scope much less in general, it is less applicable to D. In other words, it is fine to have module variables. For example, if a module consists of a single class, there wouldn't be any difference between making a variable class-static or module-scope. (Others, please correct me if you know any differences.)
 And is there a name for the variables that fall outside of the above
 constructs?
I think module-scope or module would work.
 I see so many tiny code snippets in books and docs, that when I do look
 at large dub/github projects, I'm not sure how to organize the "stuff"
 that slops over the edges.
That's always hard :) but "cohesion" seems to be a winning goal: program constructs should have little responsibilities. Ali
Mar 15 2016