This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Operator overloading
Is it possible you define symbols like '+, -, *, /' for objects in C++? I can't find any docs on it, and it would be useful/exciting to do this!
One thing you will have to learn when programming is that subtle distinctions matter. Often they matter a great deal. It behooves you to become familiar with the technical jargon and its precise meaning.
You called those 'symbols'. In C++, symbols are one of several different possible things:
Either you are referring to the thing the linker uses to connect parts of your program up. The names of functions and global variables become 'symbols'.
Or you are referring to a class of individual characters typically called 'symbols'. This is a very fuzzy set, but generally includes most things you would get by holding down shift and typing all the numbers on a US english keyboard. But as a symbol these are meaningless to C++.
From the context of your question, it's clear that you do not mean either of those two things when referring to the word 'symbol'. You think you're referring the second case I list above. But while it might superficially seem to be the case, it's not. What you are referring to are called 'operators'. Operators are things that tell the compiler that you're trying to operate on a value. They occur as parts of expressions. C++ has a very large number of them compared to most languages.
A symbol can be interpreted by the compiler as an operator. But a symbol is not an operator and an operator is not a symbol. For example && is an operator. But it's also two & symbols. As another example, # is a symbol, but there is no operator # in standard C++.
If you search for 'operator overloading', you will get the information you're looking for.
Related
Is there any advantages of defining variable names as __00000001, __00000002, etc.?
Example:
int __00000001, __00000002 = 0;
for (__00000001 = 0; __00000001 < 10; __00000001++) {
__00000002 = __00000002 + __00000001;
}
...
Update: this is mentioned in one of my programming classes a few years ago, and I remembered that the professor said there is some advantages of using it. However, I cannot recall any more information. Maybe I am wrong.
Those particular variable names are not available for user programs:
All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. (C11, section 7.1.3, paragraph 1)
So that's a big disadvantage.
Is obfuscating the crap out of your code worthwhile? No, not unless your goal is literally to do just that: to make your code as hard to read as possible. Trouble is, you've got to read it too.
Sometimes you'll run into code like this when somebody's "decompiled" a program — variable names do not survive the compilation process so this is sort of the best a decompiler can do when reconstructing a C++ program. Of course it cannot really reconstruct a C++ program; it can only re-spell the flattened logic in C++ syntax. Oh well.
Addressing your example specifically, it's worth noting that all identifiers beginning with two underscores are reserved to the implementation (your compiler and standard library), so your program has undefined behaviour.
This question already has answers here:
Do unused functions get optimized out?
(8 answers)
Closed 6 years ago.
I wonder if some C/C++ compilers implementing something similar to Pawn's stock keyword.
In Pawn, you can declare a function like that:
stock xfunc(a, b)
{
[...] // Bla bla
}
The stock keyword tells the compiler to simply not include the function in the compiled binary if it's not called somewhere in the code. This makes the function facultative so to speak, and it won't increase the binary size if its not used.
I would find it useful to have something like this in C/C++ because I want to include some functions I will not immediately use in the first iterations of my program/code. Of course, some people might tell me there's other ways to do this like using preprocessor macros, etc etc. I'm not asking for another way, I want something that permits me to make use of those functions later without having to uncomment them, change a macro to make them get compiled, etc (i.e. seamlessly). BUT... without compiling them and thus increasing my executable size when I don't use them!
A handy feature I would say.
Thanks!
P.S. If the answer is "it's not included in the language standards", are there any compilers that do it with specific pragmas, unofficial keywords, etc.?
EDIT: Should I mention, I'm mostly interested into this feature for virtual function. I'm aware function level linking exists for standard functions, but what about virtual ones? I think normally, if im not mistaken, virtual funcs get compiled even if not used to maintain class layout compatibe with a class prototype? Could you confirm? Thanks
Any modern optimizing compiler/linker will do "dead code elimination" and strip out not just uncalled functions, but also unused bits of called functions.
This question already has answers here:
Variable Naming Conventions in C++
(12 answers)
Closed 9 years ago.
In many examples of code that I've seen, they name their variable in a specific way.
E.g.
class obj
{
int mInt;
}
or
bool gTexture;
Questions.
Why do they name them in such way, and there are for sure more ways, I think...
How do you name them, and why?
Thank You
The m in mInt represents that the int is a member variable, while the g in gTexture denotes the variable being global.
This comes from Hungarian Notation.
http://en.wikipedia.org/wiki/Hungarian_notation
Naming is personally. To answer your second question, I don't use such a naming convention, and I append an underscore to class attributes.
Companies have often naming conventions. You may want to have alook at Google's naming conventions: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#General_Naming_Rules
The example you have given uses 'm' for member varibles and 'g' for globals. This is something that is used by some people. It makes it easy to see in a member function (when the function is a bit larger than a few lines, so you can't just look up at the top of the function to see the name of the parameters, local variables and so on), what is "local variable" and what affects "outside of the function".
If you work for a company, in a school or on an open source project, most likely, there is a coding standard that tells what the naming convention is. If it's your personal project, then decide on something you think works for you. The main point is that it's consistent. If not ALL member variables start with 'm', and not all global variables start with 'g', then it's pretty pointless to have it some places - just gives a false sense of security.
You haven't to follow a specific notation but it's cool if you do.
All is about clarity of your code, a variable without any upper case is truly less understandable than a variable with a good synthax. (At the first view, when you look quickly a part of code)
For a clear code, I can recommend the google's norme for c++ code : http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Why do they name them in such way, and there are for sure more ways, I think...
Generally it is difficult to understand other people's code; If enough time passes, it is difficult to understand your own code as well.
Because of this, software teams set up conventions to make sure the code their team writes is as similar as possible to the code they themselves would have written.
This refers to structuring code, used elements (interfaces, classes, namespaces, etc), naming functions and variables, what to document and in which format, and so on.
When done properly and consistently, it has a significant effect of shortening code maintenance time within a team.
There are a few known conventions, mostly from the conventions used in implementing large code bases and used libraries.
Java tends to use camelCaseNotation (start with small letter, use no underscores, capitalize each word).
MFC used the Hungarian notation, where variable names are prefixed with a few letters specifying scope and type of data (m_XXX for member variables, g_XXX for globals, s_XXX for statics, etc).
In particular the Hungarian convention can be gotten right (by using prefixes for semantic information) or horribly wrong (by using prefixes for syntactical information).
(MFC got it horribly wrong.)
ANSI C++ (and std:: namespace) tends to use small_letters_with_underscores for identifiers.
There are others and most software teams set up a convention that is a variation of one of the big ones.
How do you name them, and why?
These days I follow the ANSI C++ conventions, mostly because I want my code to integrate seamlessly with library code. I also think it looks simple and obvious (and this is very subjective).
I rarely use one letter variables (only when the meaning is clear) and prefer full words, to shortened ones.
Examples:
indexes: int index, line_index, col_index;
class names: class recordset; class task_details; etc.
http://en.wikipedia.org/wiki/Hungarian_notation
Not a real question. Everyone name them as they want to. You may read these guidelines, though: http://msdn.microsoft.com/en-us/library/vstudio/ms229045(v=vs.100).aspx
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
What is (double (^)(int))foofoo
I've tried searching for a definition on Google and SO and I found examples in which they are used but not clearly defined. By "block" I mean the caret symbol (^). I found it on a site where cdecl they described:
(double (^)(int, long long )) foo
as
cast foo into block(int, long long) returning double
I've never once seen this symbol used before today. Can anyone clearly describe what a block is and include with it a minimal working example? Thanks.
Blocks are a non-standard extension to the C (and not only to the Objective-C) language by Apple. They realize closures (lambda functions, etc., however you call them) - basically they're unnamed function-like entities, enclosing code that can be called. They facilitate writing for example event-driven code, where callbacks are used exhaustively.
In the C++ tag wiki, it is mentioned that
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language.
Can someone please explain the terms "statically typed" and "free-form"?
Thanks.
A statically-typed language is a language where every variable has a type assigned to it at compile-time. In C++, this means that you must tell the compiler the type of each variable - that is, whether it's an int, or a double, or a string, etc. This contrasts with dynamically-typed languages like JavaScript or PHP, where each variable can hold any type, and that type can change at runtime.
A free-form language is one where there are no requirements about where various symbols have to go with regard to one another. You can add as much whitespace as you'd like (or leave out any whitespace that you don't like). You don't need to start statements on a new line, and can put the braces around code blocks anywhere you'd like. This has led to a few holy wars about The Right Way To Write C++, but I actually like the freedom it gives you.
Hope this helps!
"Statically typed" means that the types are checked at compile-time, not run-time. For example, if you write a class that does not have a foo() method, then you'll get a compile-time error if you try to call foo() on an object of that class. In dynamically-typed languages (e.g. Ruby), you would still get an error, but only at run-time.
"Free-form" means that you can use whitespace however you want (i.e. write the whole program on one line, use uneven indenting, put lots of blank lines, etc.). This is in contrast to languages like Python where whitespace is semantically significant.
Statically typed: the compiler knows what the types of all variables are. In contrast to languages like Python and Common Lisp, where the types of variables can change at runtime.
Free-form: no specific whitespace requirements. This is in contrast to old-style FORTRAN and COBOL, so I'm not sure how useful this designation is anymore.