What Is the Meaning of This Declaration

What is the difference between a definition and a declaration?

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations

A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.


Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.

At 3.1/2, C++03 says:

A declaration is a definition unless it [...] is a class name declaration [...].

3.1/3 then gives a few examples. Amongst them:


[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example

To sum it up: The C++ standard considers struct x; to be a declaration and struct x {}; a definition. (In other words, "forward declaration" a misnomer, since there are no other forms of class declarations in C++.)

Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.

What is the meaning of this declaration?

opertor << is bitwise left shift operator. Shift all the bits to left a specified number of times: (arithmetic left shift and reserves sign bit)

m << n

Shift all the bits of m to left a n number of times. (notice one shift == multiply by two).

1 << 0 means no shift so its equals to 1 only.

1 << 1 means one shift so its equals to 1*2 = 2 only.

I explain with one byte: one in one byte is like:

 MSB
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 / 0
| / 1 << 1
| |
▼ ▼
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0

Whereas 1 << 0 do nothing but its like figure one. (notice 7th bit is copied to preserve sign)

OR operator: do bit wise or

 MSB                            PKRevealControllerTypeLeft
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | == 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| | | | | | | | OR
MSB PKRevealControllerTypeRight
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | == 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0

=

MSB PKRevealControllerTypeBoth
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | == 3
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0

| is bit wise operator. in below code it or 1 | 2 == 3

PKRevealControllerTypeNone  = 0,             //  is Zero
PKRevealControllerTypeLeft = 1 << 0, // one
PKRevealControllerTypeRight = 1 << 1, // two
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft |
PKRevealControllerTypeRight) // three

There is not more technical reason to initialized values like this, defining like that makes things line up nicely read this answer:define SOMETHING (1 << 0)

compiler optimization convert them in simpler for like: (I am not sure for third one, but i think compiler will optimize that too)

PKRevealControllerTypeNone  = 0,     //  is Zero
PKRevealControllerTypeLeft = 1, // one
PKRevealControllerTypeRight = 2, // two
PKRevealControllerTypeBoth = 3, // Three

Edit: @thanks to Till.
read this answer App States with BOOL flags show the usefulness of declarations you got using bit wise operators.

What is the meaning of this type declaration?

It's a type declaration, more specifically a type definition. It creates a new type, having []byte as its underlying type:

A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.

New types are created because they can simplify using them multiple times, their identifier (their name) may be expressive in other contexts, and–most importantly–so that you can define (attach) methods to it (you can't attach methods to built-in types, nor to anonymous types or types defined in other packages).

This last part (attaching methods) is important, because even though instead of attaching methods you could just as easily create and use functions that accept the "original" type as parameter, only types with methods can implement interfaces that list ("prescribe") those methods, and as mentioned earlier, you can't attach methods to certain types unless you create a new type derived from them.

As an example, the type []int will never implement the sort.Interface required to be sortable (by the sort package), so a new type sort.IntSlice is created (which is type IntSlice []int) to which the required methods are attached, so you can pass a value of type sort.IntSlice to the sort.Sort() function but not a value of type []int. Since sort.IntSlice has []int as its underlying type, if you have a value of []int, you can simply convert it to sort.IntSlice if you want to sort it, like in this example (try it on the Go Playground):

is := []int{1,3,2}
sort.Sort(sort.IntSlice(is))
fmt.Println(is) // Prints: [1 2 3]

When you create a new type, there is no "inheritance" involved. The new type will have 0 methods. If you want "inheritance-like" functionality, you should check out embedding (in relation with struct types), in which case the embedder type will also "have" the methods of the embedded type.

What is meaning no declaration match error in my case?

The declaration return type is void.

void translation(float tX, float tY, float tZ);

 ^^^^

But your definition does not have a type.

void Object3D::translation(float tX, float tY, float tZ)

 ^^^^ is missing


When an explicit return type is missing, `int` is assumed by some compilers producing that error.

e.g. VS2019 error:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

What is the difference between implementation and definition?

implementation: if you have some pseudo-code or something like an UML-Diagram and write your code on that basis it's an implementation

declaration: a declaration is already in your code where you say the compiler/interpreter: "hey look there is this variable that I want to use but I dont want to give it any value yet"

and finally definition: definition is when you finally assign a value to your variable like x = 4. Like your defining x to be 4 (in your code)

Hope this is helpful to you

What's the purpose and meaning of this declaration

Declarations like class Node; are forward declarations. They are sometimes useful if you want to do some basic operations with your class, which don't require a full declaration.


One example of such a basic operation is declaration of methods. Suppose you have a bunch of very important methods in your Doubly_Linked_List class, and you want their declarations near the top. Because some methods mention Node in their declaration, you need some kind of declaration for Node - a forward declaration is good because it doesn't distract you from the really important stuff - your methods.

To define your methods, you have to provide a definition for Node, but you do it later in code, which helps organize it in a logical way - most important stuff first.

class Doubly_Linked_List
{
public:
class Node; // forward declaration
int very_important_method_1(Node node1, Node node2);
Node very_important_method_2();

// a virtual code separator; all code below is less important

class Node : public Link {
// Some Node code goes here.
}

private:
Link head;
}

int Doubly_Linked_List::very_important_method_1(Node node1, Node node2)
{
// code
}

Doubly_Linked_List::Node Doubly_Linked_List::very_important_method_2()
{
// code
}

This is a niche technique; very rarely useful if you define your class shortly after you forward-declare it.


Forward declarations are usually useful when the definition of your class is in another file.

You can separate your code into two files, interface and implementation. Definition of Node doesn't belong in the interface file (*.h), but it's needed to declare parts of Doubly_Linked_List class. This is the classic use of forward declarations.

// Doubly_Linked_List.h
class Doubly_Linked_List
{
public:
class Node;
int very_important_method_1(Node node1, Node node2);
Node very_important_method_2();

Node* whatever; // pointers to incomplete type are allowed here
}
// Doubly_Linked_List.cpp
class Node : public Link {
// Some Node code goes here.
}

int Doubly_Linked_List::very_important_method_1(Node node1, Node node2)
{
// code
}

Doubly_Linked_List::Node Doubly_Linked_List::very_important_method_2()
{
// code
}

Whats the meaning of the % symbol in a data declaration?

Nothing more than any other character, outside of ABAP Objects ("The name must start with a letter or an underscore (_). Only outside of ABAP objects can the name also start with a different character."). It's recommended to use ABAP objects, so % is deprecated. Historically it's also often used by SAP for naming "system" (i.e. very low-level SAP programs) variables, types, and any other kind of symbols.

What is the difference between declaration and definition in Java?

The conceptual difference is simple:

  • Declaration: You are declaring that something exists, such as a class, function or variable. You don't say anything about what that class or function looks like, you just say that it exists.

  • Definition: You define how something is implemented, such as a class, function or variable, i.e. you say what it actually is.

In Java, there is little difference between the two, and formally speaking, a declaration includes not only the identifier, but also it's definition. Here is how I personally interpret the terms in detail:

  • Classes: Java doesn't really separate declarations and definitions as C++ does (in header and cpp files). You define them at the point where you declare them.

  • Functions: When you're writing an interface (or an abstract class), you could say that you're declaring a function, without defining it. Ordinary functions however, are always defined right where they are declared. See the body of the function as its definition if you like.

  • Variables: A variable declaration could look like this:

      int x;

(you're declaring that a variable x exists and has type int) either if it's a local variable or member field. In Java, there's no information left about x to define, except possible what values it shall hold, which is determined by the assignments to it.

Here's a rough summary of how I use the terms:

abstract class SomeClass {                // class decl.
// \
int x; // variable decl. |
// |
public abstract void someMethod(); // function decl. |
// |
public int someOtherMethod() { // function decl. |
// | class
if (Math.random() > .5) // \ | def.
return x; // | function definition |
else // | |
return -x; // / |
// |
} // |
} // /


Related Topics



Leave a reply



Submit