c++ class vs struct+functions

Started by
13 comments, last by user0 9 years, 11 months ago

Im writing a class with a little over 2000 functions associated with it they are pretty small but for this question im asking more in general rather than my use current use case.

Would a class having lots of functions in it occur more overhead than a struct+functions that do work on that struct?

Advertisement

Assuming the member functions are non-virtual they'll compile into essentially identical machine code as functions that accept structs as pointer or reference arguments.

Thanks thats exactly what I was wondering!

Now that the problem is solved, can I ask what on earth design has led you to have a class with 2000 methods?

I'm quite interested in this 2k function class too...

http://en.wikipedia.org/wiki/Single_responsibility_principle

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

lol well I know it probably not the right way of doing this but im making a x86-64 disassembler to c++/other languages and first it has alot of instruction codes then it will need to convert to a byte code then im shooting for c++ and javascript output c++ will be one thing but javascript is a completely different story.

I will do like swiftcoder mentioned and break it into different classes but just wanted to know what would happen with alot of functions in one class even say the disassembler to bytecode class will need a big switch and I was wanting to handle them in different functions so they can be over ridden and easily find able in a ide. I dont expect this to go anywhere just having my hand at learning more deeply about how everyting on the cpu gets done.

Actually, if there are 2000 different instruction codes and each method is distinct, that's one of the few cases where that could be acceptable. At least then you can map codes to function pointers rather than having a gargantuan switch lookup.

That said, I'd suspect there was a lot of shared functionality on the methods that could've factored down a bit but who knows.

lol well I know it probably not the right way of doing this but im making a x86-64 disassembler to c++/other languages and first it has alot of instruction codes then it will need to convert to a byte code then im shooting for c++ and javascript output c++ will be one thing but javascript is a completely different story.

I will do like swiftcoder mentioned and break it into different classes but just wanted to know what would happen with alot of functions in one class even say the disassembler to bytecode class will need a big switch and I was wanting to handle them in different functions so they can be over ridden and easily find able in a ide. I dont expect this to go anywhere just having my hand at learning more deeply about how everyting on the cpu gets done.

Considering all this, you may want to constantly reference a profiler for any future changes you make. Identify bottlenecks, evaluate if your code is too complex and figure out where you can make the code easier to read vs where you have to apply performance magic.

If you have to really go all out on a function to make it faster, you can put the old easy-to-understand version of the function in a comment, as sort of pseudo-code and go from there.

Some search words and references:

http://en.wikipedia.org/wiki/Branch_misprediction

http://www.ece.unm.edu/~jimp/611/slides/chap5_3.html

http://bigocheatsheet.com/

https://isocpp.org/wiki/faq/inline-functions

Ask if you need more specific help, eg with a function that is using too much time.

So just as a reminder to the OP, but a struct is a class in C++, only difference is default visibility level, struct = public, class = private. If those functions are small, the compiler might just end up inlining most of the code anyway in a release build.

Still you should probably break this out a bit 2000 functions seems overkill for a class, although it could happen if you have a lot of one or two line functions.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement