• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jeff8j

c++ class vs struct+functions

14 posts in this topic

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? 

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

Edited by Kaptein
2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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

Heh, this was my first answer-reflex, too. However, on closer inspection, that's not what the OP is asking. The question is not whether there is a difference in having member functions in a struct or class, but rather whether there is a difference between

struct foo{....};
void func1(foo* f);
...
void func2000(foo* f, type bar);

and

class foo
{
    ....
public:
    void func1();
    ...
    void func2000(type bar);
};

Jeff8j: Did I understand that you are basically trying to write a C++ decompiler? Knoweth that this is a daunting endeavour, next to impossible.

Decompiling Java bytecode or such into a human-readable source file is easy (the language is kind of specifically made for that, too). Decompiling C or C++ is harsh. While it is certainly possible to identify things as function prologs and such, there is no way of determining a meaningful variable name of any kind, or a function name (unless these are exported as symbols), no way to reproduce something like template instantiations into something that even vaguely ressembles the original, and no way to restore variables (or sub-expressions) which have been eliminated by compiler optimizations.

In one word, it's pretty much a nightmare...

Edited by samoth
2

Share this post


Link to post
Share on other sites

 

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

Heh, this was my first answer-reflex, too. However, on closer inspection, that's not what the OP is asking. The question is not whether there is a difference in having member functions in a struct or class, but rather whether there is a difference between

struct foo{....};
void func1(foo* f);
...
void func2000(foo* f, type bar);

and

class foo
{
    ....
public:
    void func1();
    ...
    void func2000(type bar);
};

Jeff8j: Did I understand that you are basically trying to write a C++ decompiler? Knoweth that this is a daunting endeavour, next to impossible.

Decompiling Java bytecode or such into a human-readable source file is easy (the language is kind of specifically made for that, too). Decompiling C or C++ is harsh. While it is certainly possible to identify things as function prologs and such, there is no way of determining a meaningful variable name of any kind, or a function name (unless these are exported as symbols), no way to reproduce something like template instantiations into something that even vaguely ressembles the original, and no way to restore variables (or sub-expressions) which have been eliminated by compiler optimizations.

In one word, it's pretty much a nightmare...

 

Still the answer to that question is no, effectively this is what a member function does, its first param is the this pointer. So it comes down to how the structure is passed in those functions. If it is by value it is more expensive, because of the copy of the structure, pass by pointer or reference on the first argument to the function it would be the same as a member function.

2

Share this post


Link to post
Share on other sites

Im just doing it to learn more if I get a hello world decompiling ill be happy lol I would like to eventually design a cpu so it would seem important to learn as low a level as possible may I could start off easier with something like a pic microcontroller.

 

I probably will break it up into differnet types/sections/classes I was just starting and was like wait a minute theres a lot of opcodes and system library calls and a world of mess so things are going to be pretty big no matter how I go about them I would think.

 

@samoth yes thats what I was talking about and it will probably only be things I compile going into it so all variables will be know atleast to start with so that should help

 

Im still debating the c route as I could make it compile it self with tiny c compiler and then disassemble it self to recompile again lol but on the downside would probably be harder to write.

 

How much overhead would I encure if I go with c structs with function pointers oviously it would need ints/pointers for each function but would that be al that it needed? Would that get optimized out with full gcc optimization?

0

Share this post


Link to post
Share on other sites

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.


Maybe it's unfair or rude to ask this question, but how are you going to write a disassembler if you do not even know how a member function call works? That seems like a basic, low-level thing that anyone working on such a project wouldn't have to ask.
0

Share this post


Link to post
Share on other sites

@L. Spiro Your absolutely right data tables looks like a much better option! Ill have to read up and start messing around that will probably save alot of work and be simpler Thanks

 

@Pink Horror Im not sure it will get done lol im familiar with how member functions work just not how they get compiled as far as overhead especially for something complicated where it might be harder to optimize down. If I dont try how am I going to learn? For most work I do I do in php that doesnt help make me become a better programmer for the most part lol.

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0