Assigning out of Functions in C++
C++ Just After the Basics - Part 2
Forward: After declaring an object, you can proceed to assign a value to that object. Can you assign a value to the identifier of the object outside function definitions? I answer that question in this article.
By: Chrysanthus Date Published: 22 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Let us start by looking at initialization, since initialization and assignment are related.
Initialization of Fundamental Objects and Pointers
The following code will work:
#include <iostream>
using namespace std;
int myInt = 5;
int main()
{
cout << myInt<<"\n";
return 0;
}
Outside the main function an int object is created by initialization; the code works. The following code where a pointer is created by initialization outside of function also works:
#include <iostream>
using namespace std;
int myInt = 5;
int *ptrInt = &myInt;
int main()
{
cout << *ptrInt << "\n";
return 0;
}
Outside the main function an int pointer is created by initialization; the code works.
So creating an object or pointer by initialization outside of all functions is alright. What about assigning a value outside of all functions, after declaration?
The following code does not work:
#include <iostream>
using namespace std;
int myInt;
myInt = 5;
int main()
{
cout << myInt<<"\n";
return 0;
}
Declaring an object outside of all functions is OK. However, after declaring the object you cannot assign a value to the object outside of all functions; the above code does not work. Also, the following code with pointer assignment does not work:
#include <iostream>
using namespace std;
int myInt = 5;
int *ptrInt;
ptrInt = &myInt;
int main()
{
cout << *ptrInt << "\n";
return 0;
}
Declaring a pointer outside of all functions is OK. However, after declaring the pointer, you cannot assign a reference (value) to the pointer outside of all functions; the above code does not work.
So you cannot assign a value to an object or pointer outside of all functions. After declaring an object or pointer outside of all functions, you can assign value to the object or the pointer inside any function; not outside all the functions. You can assign a value outside of a function only during declaration (definition) in one step.
Initialization normally occurs during declaration (definition) of the object. Assignment is part of initialization as right operand. Assignment can also occur after declaration (definition) or initialization. Both features use the = operator. Re-assignment can still occur after declaration or initialization.
Conclusion
You can create an object or pointer by initialization outside of all functions. You can declare an object or pointer without initialization outside of all functions. However, you cannot assign value to an already created object or pointer declared (without initialization), outside of all functions.
We continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT