It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Taking this as an example:
I have 20 structs. I access all of the struct's fields directly, getting values off them, pointers to other sub-structs they might have, etc.
Now, I restructure the program:
Instead of acessing structs's fields DIRECTLY, I've encapsuled all of the struct into their own respective classes, and have functions for all the possible get(), and set(x).
The Question: Is there a performance impact for using methods/functions, instead of acessing structs directly?
Some sort of estimated % would be great, or some explanation.
In general, there shouldn't be a performance difference if the getters and setters are defined inline within the class. For virtually any contemporary compiler, the function call will be expanded inline, leaving no overhead. This will often be true for various small inline functions. (See, for example, http://www.gotw.ca/publications/mill18.htm, where Herb Sutter discusses why making most/all virtual functions nonpublic adds no overhead to the resulting code.)
Note that if you do define the function inline in the class, any client code will need to be recompiled if the definition ever changes. But that applies to most/all changes in header files.
I haven't heard of performance as a considerable difference between structs and classes, so it is your call.
I googled these pages below, and they say there shouldn't be a difference when it comes to performance.
http://www.gamedev.net/topic/115725-is-a-struct-faster-than-a-class/
http://forums.codeguru.com/showthread.php?332571-Struct-vs-Class
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C and C++, what is the advantage of making a local const variable static? Assuming the initialization does not use other variables, is there any difference between preserving the value between calls, and setting the same constant value each call?
Could a valid C compiler ignore the static?
In C++, it avoids the construction/destruction between calls, but could there be any other benefit?
It doesn't take up stack-space may be a benefit if you have something like:
static const double table[fairly_large_number] = { .... };
Obviously, cost of construction can also be substantial enough that if the function is called a lot, there's good value in only constructing the object once.
Yes, and it is huge: a semantic benefit.
When you put const, you don't just mean the compiler shouldn't let you modify the variable. You make a bolder statement to whoever reads the code later: this won't ever change. Not even by a side effect where you give this variable as a pointer to another function.
Also, the compiler can take advantage of that new information and optimize it away in some situations, depending on the specific type you are dealing with.
(to be clear, I'm speaking here about const vs. non-const, not static vs. non-static.)
Edit: This SO answer is very informative too.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm writing an LLVM pass which works on the LLVM IR code. A part of the objective is to read the initial values of global variables, where global variables could be of any type, from basic types to structs and classes. How do I achieve that?
If you invoke getInitializer on a GlobalVariable instance you get the initializer, of type Constant (though make sure you call hasInitializer or one of its sister methods first, to verify there's an initializer at all). Also IIRC global variables and constant are implemented as the same thing, so use isConstant to filter out the constants if you don't want them.
Of course, a Constant is the abstract base class; the actual type will be one of its children, which you can see in the diagram presented on the documentation page. You can query and get the actual constant type in the usual way, via isa / cast / dyn_cast, or you can use getType on it (a constant is a Value after all) and work from there.
Finally, to get all the global variables from a Module use either the global_begin/global_end iterators, or just use getGlobalList on it (it has its own iterator).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
What does it mean "register a class", if we are talking about C++ and OOP, not about register, COM or some other libraries?
In the context of pure C++, without respect to any specific platoform or library, "register a class" has no meaning. This is not a language concept, nor is it any design pattern I am aware of.
It does however have at least two meanings the Windows world. For posterity and future readers:
You can register a Window class. A Window class contains some basic functionality and other parameters that effect how specific windows behave on-screen.
You can register a COM class so that specific instances of that COM class can be instantiated by clients.
"context was in articles about OOD, and it was said something like it is good practice to plan your classes in the way that for changing some aspect of program it will be enough to write new class, register it and you get new staff"
Now it seems to me you are talking about the publisher/subscriber pattern. It allows loose coupling between caller and callee.
You can find a C++ implementation everywhere, in the GoF book, or here:
http://rtmatheson.com/2010/03/working-on-the-subject-observer-pattern/
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm on Linux, and trying to write cross-platform code. I know that templates are difficult to use properly on Windows, but I don't remember the specifics.
I do know that one problem has to do with template debugging and templates that cross dll boundaries, but not how to work around it - or if a workaround is even possible.
I believe that there are other issues, but I've only found vague references to them.
So: What are the problems with C++ templates on Windows? Aside from not using templates, what are the solutions?
Edit: Maybe the problems are only with the STL, and not with templates in general?
Two quotes:
You cannot expose STL calls across library boundaries on windows without significant headaches.
and
Encapsulation is a must, I have done some projects with stl containers over dll boundaries as well as static libraries on windows and it's not possible to use without encapsulation.
I know that templates are difficult to use properly on Windows.
I've never heard such a thing. Sounds like baseless FUD, which I suggest you completely ignore.
Here's one problem, from the MS KB (edit: this one only pertains to MSVC 5 and 6; I have no intentions of supporting such an ancient compiler, so it's irrelevant to me):
Some STL classes contain nested classes. These classes can not be exported. For instance, deque contains a nested class deque::iterator. If you export deque, you will get a warning that you must export deque::iterator. If you export deque::iterator, you get a warning that you must export deque. This is caused by a designed limitation that once a template class is instantiated, it can not be re-instantiated and exported. The only STL container that can currently be exported is vector. The other containers (that is, map, set, queue, list, deque) all contain nested classes and cannot be exported.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
When reading the documentation of one or another boost library, I encountered some statements giving a hint that the virtual keyword is kind of evil. See http://www.boost.org/doc/libs/1_46_1/libs/msm/doc/HTML/ch03s05.html, for example:
It will not be said that MSM forces the virtual keyword down your throat!
According to http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.4, the virtual keyword is really not that bad, and my feeling about it is the same.
Why do some of the boost people regard virtual function calls as the worst thing ever? I have the impression that the boost guys are really the experts on C++, so there must be something about it.
there is case where static polymorphism is preferred to dynamic one. That's what Christophe states here. Nothing more.
Runtime polymorphism has an extra cost, namely the vtable. Once the vtable is added in a type, it can't be removed. One of the core strengths of C++ is that "you only pay for what you use". Therefore, to keep objects as lean as possible, several libraries avoid virtual functions when possible. Not because it is evil, but because you may not want it.
I think the inference is that MSM does not, through its inner workings or structure, force you to declare members of your own code as virtual, or to override theirs. There are libraries that require this, for instance libraries that dynamically create "proxies" of your classes such as mocks or lazy-loaders.