Defining component scoped variables in ColdFusion - coldfusion

How may I define a variable in component's "init" function which I am able to access across all the component functions.

use the variables scope, this is private to the component and available inside any function within the component
function init(somevalue) {
variables.somevalue = arguments.somevalue
}
function getSomeValue() {
return variables.somevalue;
}

Related

Accessing private member function via Lambda

In a project, I have a C-API which uses C-style function pointers as callbacks. In one of those callbacks, I need to access a private function of an object Foo. Note that the API-call is done within a function of my class. Because of not shown code, I have a handle to my object as a void* accessible to me.
Things aside that the construct is prone to errors, by passing a lambda as callback I am able to access the private function of my object.
This is somewhat unexpected but welcome to me. I haven't found anything regarding this behavior, so I would kindly ask if somebody could shed some light why this is actual working. I am not aware that the lambda is catching something, but it appears that is has something to do with the actual scoping.
I am using C++17 and gcc 10.2.
Here's a sample block of code:
void TestFunc(void (*)(void*)) {
}
class Foo {
public:
Foo() {
TestFunc([](void* handle){
auto foo = reinterpret_cast<Foo*>(handle);
foo->BarFunc();
});
};
private:
void BarFunc() {};
};
Your code is working because Standard allows it. So first we have this (C++20 [expr.prim.lambda.closure]p2):
The closure type is declared in the smallest block scope, class scope,
or namespace scope that contains the corresponding lambda-expression.
<...>
And in your example we have a block scope so in the end we have an unnamed local class declaration and according to [class.local]p1:
A class can be declared within a function definition; such a class is
called a local class. The name of a local class is local to its
enclosing scope. The local class is in the scope of the enclosing
scope, and has the same access to names outside the function as does
the enclosing function

Calling private function in lambda from outside the class

In my current project I am trying to pass a private member function as parameter to another function. In my code, the other function is a member function of a different class, but for keeping it simple, here it is a free function.
void outside_function(std::function<void(int)> func) {
// do something with func
}
class MyClass {
public:
void run();
private:
bool my_func(double); // defined in source file
};
Now, from inside run I want to put my_func into outside_function as argument. Since the signature of run does not fit the requirements for the parameter, I cannot simply pass it. Using the adapter pattern was my first try, which was when I was reminded that member functions implicitly take the this pointer as first argument. This answer suggests using a lambda expression, which is what I did.
void MyClass::run() {
outside_function([this](int a) -> void {
double d = static_cast<double>(a);
this->my_func(d);
});
}
Why does this work? From my point of understanding, outside_function does not have access to my_func. Why don't I have to make my_func public first? Is it something the compiler does or some C++ rule I don't know?
Additionally, are there any catches to this approach that might break my code?
private access specifier only restrict an object name to be visible (it cannot be looked up) outside the class. The object itself is like any other member.
[class.access]/1
A member of a class can be
(1.1) private; that is, its name can be used only by members and friends of the class in which it is declared.
(1.2) protected; that is, its name can be used only by members and friends of the class in which it is declared, by classes derived from that class, and by their friends (see [class.protected]).
(1.3) public; that is, its name can be used anywhere without access restriction.
outside_function never accesses the member (it only knows the type of the func parameter and can't possibly care about how it's defined) -- only the lambda function does.
Since you're defining the lambda function inside MyClass, you can access all the private names within it.
outside_function calls the provided function, but if you weren't allowed to call a function that uses any private members, you couldn't do much.

Why is it allowed to call a private static method when initializing a private static member?

This code compiles and works the way I want it, but why?
#include <iostream>
class Test {
private:
static bool _inited;
static bool _init() {
std::cout << "init!" << std::endl;
return true;
}
};
bool Test::_inited = Test::_init();
int main(int argc, char** argv) {
}
And if I make what I think is a unrelated change:
bool _inited = Test::_init();
it no longer compiles, giving me the expected error about trying to call a private method.
This code compiles and works the way I want it, but why?
Because where you are using it you are operating at class scope, as part of the class.
It would be the same if you were to write:
static void Test::foo() {
Test::_init(); // or just _init();
}
Here, the function foo is obviously a part of the class, so you can access every member of Test (be it private or public) in it.
You can even remove the Test::, it is redundant because the compiler will already be looking in the scope of Test for _init(), when _inited is initialized (because it's part of Test).
The answer is simple. When you write
bool Test::_inited = Test::_init();
it means that the private static variable of class Test has a value equal to the value that _init() function returns. It is completely valid as you are not trying to access a private function from outside its scope. The Class_Name:: prefix puts them inside the class. They are just not in the class declaration. So in a way its like putting that whole statement inside the class.
Now when you write-
bool _inited = Test::_init();
the variable _inited is different from that of class Test. So it's taking its value from a private data method of a class which is forbidden unless a getter function is used for the assignment.
I think the following sums it up pretty good. Taken form http://www.cplusplus.com/doc/tutorial/classes/
The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition.
I think the same is applicable to everything within the Class, including the static field.

My non-member static function allows changing and creating of non static variables

this is the code snippet
static chck()//tracking how many times main has been called
{
static a=0;
int y=0;//this is not supposed to work
cout<<"Total time main has been called: ";
a++;
return a;
}
our teacher told us that static functions can't change or create non static variables
but it works for me ,why?
In this case "y" is a stack variable which this function can access.
The theory is static member functions (static Methods in a class) cannot access non static member variables (non static variables in the class) as there is no object as "this" inside a static member function.
static may well be the most overused keyword in c++. Your first use of it refers to the linkage of chck(), i.e. it has internal linkage. Your second use makes a static with respect to calls to ckch(), i.e. it's value will be preserved between calls. You are think of static member functions of a class that can't access non-static data members, i.e. those data members that are created per object instance.

Declaring a global variable in a non-member function

Can I declare a global variable inside a non-member function?
Or in other words, a static equivalent for a non-member function?
And I'd like the variable to not be const - e.g. modifiable...
You can declare a global variable inside a function:
void f() {
extern int i;
}
But you'll also need to define it in the surrounding namespace, if you want to use it.
Being global, the definition doesn't have to be the same translation unit, but is subject to the One Definition Rule.
If, as indicated in the comments, you actually want a persistent local variable, initialised the first time the function is called, then that's exactly how a local static variable behaves:
void f() {
static int i = whatever(); // initialised the first time
i = something_else(); // the new value is preserved for next time
}
Can I declare a global variable inside a non-member function?
You can provide a declaration for a namespace level global variable inside a function. But I have the feeling that you are looking for something else and did not get the wording quite right:
Or in other words, a static equivalent for a non-member function?
You can declare (and define) a local static variable in a function. The lifetime of which will extend beyond the execution of the function (i.e. the variable will be there for the next function execution and so on).
int nextValue() {
static int counter = 0;
return ++counter;
}
Note that this is not a global, as global implies accessibility from any context and this variable is only accessible within nextValue.
And I'd like the variable to not be const - e.g. modifiable...
This is completely orthogonal to where you declare/define a variable.