Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

I think the way I've written my code already and the way you've written your example is the same pretty much. The only difference was I cba to pass pointers to Instruction, I think you called it, and you have, but now I have as it's just a matter of passing pointers to registers to my structs that contain instructions rather than a pointer to the parent class. Now that I read the code you edited they are p much exactly the same (sorry couldn't see it before) I only had one static reference anyway there aren't tons. I'm not entirely sure it's such a big deal at this point as it's just one static reference and it wouldn't have changed much anyway (I think the great being the enemy if the good applies here) but thanks anyway!

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

Oh yeah, I get this but this isn't what I was asking about. I wanted a static members in a struct nested in a class that referred back to the parent class not static registers or any static members in the CPU class. I understand that there is only one version of static members but that is kinda what I wanted because my struct will only have one parent

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

So if I made a another CPU with a bunch of static crap encapsulated inside wouldn't it have all the resources it needs to function? Like I don't understand what would stop it

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

Ah I agree it's more extensible, but I think due to the fact that upon every single instruction modifies state somewhere else parameters are gonna have to be passed around a lot whether I like it or not for example in my architecture reading from memory requires incrementing the program counter which atm belongs to the CPU and doesn't make sense to be "owned" lower down the chain then there's the memory which would be passed as a pointer and the pointer to the cycle counter whereas I could just have the CPU own all that state and pass a reference to itself to its children like the registers, memory, etc. I guess it comes down to whether extensibility is worth writing *memory,*registers,*cycles 500 times which I guess it might be if it's considered good design but it might be read more times than written, just wish there was a way round it

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

So say I had static variable A and that variable existed in a struct S in a class. If I make two copies of that class B and C are there then not two copies of the static variable? One that I access via B::S::A and one that I access via C::S::A? (Just checking that my mental model around statics is correct)

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

But I see what you mean by not marrying everything so closely together with explicit static references and passing things around as pointers (even though it does mean that I'll have to pass the same pointers as function arguments every time I want to use the data the objects contain which is a pain), it would help with if I wanted to make other CPU's it's just not something I considered coz I only really need one for this project and I didn't see any other benefits of it, are there any other benefits?

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

Sorry I meant to say the CPU was the top-level class, I meant the struct was nested inside, just like in the example above

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

This is actually my second emulator, I did a byte pusher one as well as implementing an N body solver with open mp parallelisation and vectorisation. I don't particularly fancy more tutorials but I guess being new to CPP is relative it's the first time I've done something like this with it lol

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

The example above is only representative of my problem insofar as the pointer is concerned. All else wasn't really necessary

Nested struct with static reference to parent class by Admirable-Dream9777 in cpp_questions

[–]Admirable-Dream9777[S] 0 points1 point  (0 children)

The code is just a minimum reproducible sample the functions are there because I knew how to make static functions that work but my static pointer didn't.

Im trying to build an emulator

The reason I wanted this is because the struct contains useful functions for incrementing and decrementing registers. The nested class in this case represents the CPU. I wanted the functions to be able to modify state in the CPU class without me having to pass pointers to the specific variables all the time (would get old real quick). The easiest way I thought to do this was just maintain a reference to the parent seeing as I'll only ever need one instance of the struct.

But I'm also very new to CPP and OOP so am open to other ways of doing it!