declaration

What are the advantages and disadvantages of separating declaration and definition as in C++?

为君一笑 提交于 2019-11-29 09:54:13
In C++, declaration and definition of functions, variables and constants can be separated like so: function someFunc(); function someFunc() { //Implementation. } In fact, in the definition of classes, this is often the case. A class is usually declared with it's members in a .h file, and these are then defined in a corresponding .C file. What are the advantages & disadvantages of this approach? Historically this was to help the compiler. You had to give it the list of names before it used them - whether this was the actual usage, or a forward declaration (C's default funcion prototype aside).

Can you add a condition to a variable declaration?

浪尽此生 提交于 2019-11-29 09:41:43
This doesn't make sense to me, but I have a feeling I saw a code using this: var abc = def || ghi; My question is, is this valid? Can we add a condition to a variable declaration? I imagine the answer is no but I have this at the back of my mind that I saw something similar in code once. This gives to abc the value of def if it isn't falsy (i.e. not false , null , undefined , 0 or an empty string), or the value of ghi if not. This is equivalent to: var abc; if (def) abc = def; else abc = ghi; This is commonly used for options: function myfunc (opts) { var mything = opts.mything || "aaa"; } If

Can't understand the declaration #3 in the Example of [basic.link]/6 C++14

。_饼干妹妹 提交于 2019-11-29 09:22:57
[basic.link]/6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage.[ Example:

Declaring a useless local variable

对着背影说爱祢 提交于 2019-11-29 09:17:58
So this is an odd one, I know the code itself is fairly useless, but what I'm wondering why I get the error: I was writing some code, I had written this: if(scan.hasNextInt()) int row = scan.nextInt(); Wasn't thinking about variable scope at the time, obviously this is useless because I can't use row past the if anyway. What I don't get is why I got the error I did: > javac hw.java hw.java:25: '.class' expected int row = scan.nextInt(); ^ hw.java:25: not a statement int row = scan.nextInt(); ^ hw.java:25: illegal start of expression int row = scan.nextInt(); ^ hw.java:25: ';' expected int row

Synthesize error “Missing Context for Property Implementation Declaration”

主宰稳场 提交于 2019-11-29 09:14:25
Anyone have any ideas? When I try to synthesize a property I have declared in the .h file its not letting me synthesize it. Any ideas? Thanks! This can happen when you attempt to synthesize a property outside of the scope of your class' implementation. Incorrect: @synthesize yourProperty; @implementation YourClass @end Correct: @implementation YourClass @synthesize yourProperty; @end It often happen when you use #import ,and does not on the top of file. 来源: https://stackoverflow.com/questions/7005390/synthesize-error-missing-context-for-property-implementation-declaration

Declaring variable type based on a column type

 ̄綄美尐妖づ 提交于 2019-11-29 09:07:28
In Oracle's PL-SQL, you can declare a variable and define its type based on a table column: declare var1 table.column%TYPE; Is it possible to do something similar in MS SQL Server? No you can't do this. The closest equivalent is User-Defined Data Types . This will give you a layer of abstraction that may help, but it is not the same as deriving a type from a column. It may skirt the real issue, but you can "cheat" a little bit by Select * INTO #tmp From MyTable Where 1 = 0 Will automatically create a temp table with all columns with correct data types. 来源: https://stackoverflow.com/questions

String vs string [duplicate]

我与影子孤独终老i 提交于 2019-11-29 09:05:10
In C# there are String objects and string objects. What is the difference between the two? What are the best practices regarding which to use? There is no difference. string (lower case) is just an alias for System.String. No difference. System.String is strictly identical to string . Common C# coding guidelines indicates that you should use the keyword string . They are aliases and are interchangeable. However, stylistically, for declarations, I use the lowercased string, and for the static methods, I use String. string foo = "bar"; if( foo != String.Empty ) { Console.WriteLine(String.Format(

Where can I legally declare a variable in C99?

好久不见. 提交于 2019-11-29 09:03:18
When I was first introduced to C I was told to always declare my variables at the top of the function. Now that I have a strong grasp of the language I am focusing my efforts on coding style, particularly limiting the scope of my variables. I have read about the benefits to limiting the scope and I came across an interesting example. Apparently, C99 allows you to do this... for (int i = 0; i < 10; i++) { puts("hello"); } I had thought that a variables scope was limited by the inner-most surrounding curly braces { } , but in the above example int i appears to be limited in scope by the curly

Regex for variable declaration and initialization in c#

╄→尐↘猪︶ㄣ 提交于 2019-11-29 08:53:39
I want to write a RegEx to pull out all the variable values and their names from the variable declaration statement. Say i have int i,k = 10,l=0 i want to write a regex something like int\s^,?|(^,?)* but this will also accept k = 10 i.e. (without int preceding it) Basically idea is If string starts with int then get the variable list seperated by , i know to extract csv values, but here my string has some initial value as well. How can i resolve it? Start thinking about the structure of a definition, say, (a line can start with some spaces) followed by, (Type) followed by (at least one space)

Class declaration in same scope as using declaration compiles in GCC but not MSVS

Deadly 提交于 2019-11-29 07:37:57
Is the following program well-formed according to the c++ standard? namespace X { class A; } namespace Y { using X::A; class A {}; } int main() {} I'm getting different results with different compilers: gcc compiles it without errors. visual c++ gives error C2888: 'X::A': symbol cannot be defined within namespace 'Y' I don't find any rule in the c++ standard that my program violates. If the program is well-formed, why does visual studio give an error? If the program is not well-formed, what rule in the c++ standard did it violate and why doesn't gcc give an error? I'm not trying to make my