Questions tagged [declaration]

Declaration is the part of the subprogram (procedure or function) which provides the protocol(header), but not the body of the subprogram.

3407 questions
108
votes
5 answers

When to use an object instance variable versus passing an argument to the method

How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods? I prefer keeping instance variables in a list at the end of the Class, but this list…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
106
votes
8 answers

Declaring variables without var keyword

At w3schools there is written: If you declare a variable, without using "var", the variable always becomes GLOBAL. Is it useful to declare global variable inside the function? I can imagine to declare some global variables in some event handler,…
xralf
  • 3,312
  • 45
  • 129
  • 200
103
votes
3 answers

How should I use the "my" keyword in Perl?

I keep seeing the my keyword in front of variable names in example Perl scripts online, but I have no idea what it means. I tried reading the manual pages and other sites online, but I'm having difficulty discerning what it is for, given the…
FistOfFury
  • 6,735
  • 7
  • 49
  • 57
100
votes
1 answer

What's the purpose of this [1] at the end of struct declaration?

I was snooping through my MSP430 microcontroller's header files, and I ran into this in : /* r3 does not have to be saved */ typedef struct { uint32_t __j_pc; /* return address */ uint32_t __j_sp; /* r1 stack pointer */ …
Alexander
  • 59,041
  • 12
  • 98
  • 151
96
votes
8 answers

Semicolon after class declaration braces

In C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do people really do things like: class…
SmacL
  • 22,555
  • 12
  • 95
  • 149
90
votes
3 answers

Has the new C++11 member initialization feature at declaration made initialization lists obsolete?

With C++11, we now have the ability to initialize class members in a header declaration: class aClass { private: int mInt{100}; public: aClass(); ~aClass(); }; So I'm a bit confused. Traditionally initialization…
Vector
  • 10,879
  • 12
  • 61
  • 101
89
votes
11 answers

Declaring an object before initializing it in c++

Is it possible to declare a variable in c++ without instantiating it? I want to do something like this: Animal a; if( happyDay() ) a( "puppies" ); //constructor call else a( "toads" ); Basially, I just want to declare a outside of the…
Quantum7
  • 3,165
  • 3
  • 34
  • 45
88
votes
7 answers

Where you can and cannot declare new variables in C?

I heard (probably from a teacher) that one should declare all variables on top of the program/function, and that declaring new ones among the statements could cause problems. But then I was reading K&R and I came across this sentence: "Declarations…
Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
88
votes
3 answers

__attribute__ in GNU C

Why and how is __attribute__ used in GNU C programs?
sud03r
  • 19,109
  • 16
  • 77
  • 96
86
votes
2 answers

Can a JavaScript object property refer to another property of the same object?

I recently tried to create an object like this: var carousel = { $slider: $('#carousel1 .slider'), panes: carousel.$slider.children().length }; My intentions were to improve jQuery's selector performance by caching the results of…
Bungle
  • 19,392
  • 24
  • 79
  • 106
84
votes
4 answers

Add keys/values to Dictionary at declaration

Very easy today, I think. In C#, its: Dictionary dict = new Dictionary() { { "", "" } }; But in vb, the following doesn't work. Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String)…
XstreamINsanity
  • 4,176
  • 10
  • 46
  • 59
81
votes
3 answers

I do not understand why this compiles

I'm certainly missing something, but I do not understand why this compiles (with both g++ & clang++): struct A { }; struct B { }; int main() { A a(B); } First of all, B is a type... not a value. How should I interpret this code?
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
78
votes
7 answers

Where to declare/define class scope constants in C++?

I'm curious about the benefits/detriments of different constant declaration and definition options in C++. For the longest time, I've just been declaring them at the top of the header file before the class definition: //.h const int MyConst =…
bsruth
  • 5,372
  • 6
  • 35
  • 44
75
votes
9 answers

Why is adding attributes to an already instantiated object allowed?

I am studying python, and although I think I get the whole concept and notion of Python, today I stumbled upon a piece of code that I did not fully understand: Say I have a class that is supposed to define Circles but lacks a body: class Circle(): …
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
74
votes
8 answers

What’s the difference between “{}” and “[]” while declaring a JavaScript array?

What’s the difference between “{}” and “[]” while declaring a JavaScript array? Normally I declare like var a=[]; What is the meaning of declaring the array as var a={}
Venkat
  • 2,549
  • 2
  • 28
  • 61