Not sure how to translate this C++ variable to D.

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 15 07:29:37 PDT 2016


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;
>
> Like in Java and C#, class variables are object references in 
> 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.


More information about the Digitalmars-d-learn mailing list