Declaring instance without variable name c++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
After asking C++ Error linking in consumer file caused by static data field, I tried two different declarations for an instance of StateConservator:
StateConservator cs(*pContainer, pDoc->GetConfiguration());
and
StateConservator(*pContainer, pDoc->GetConfiguration());
The first did what I want, it only passes on the destructor only after the end of the scope. The second passes on the destructor in the own line of the declaration itself.
Is the compiler behaving correctly? If it is the correct behavior what is the way to declare an anonymous variable in that line?

You cannot have "unnamed" objects in C++. In the second case, the object is created and destroyed instantaneously because it is not associated with a name. The association with a name (e.g., variable) gives an object scope which controls its lifetime. By not naming an object, it's lifetime is bound to the statement. If it is given a name, then it is bound to the scope that the name is declared in.

Related

How to find the total numbers of object created of my class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
For example, I have my class employee. I want to keep record how many employees till date have worked for me. I can make static count variable and add 1 in my constructor. But whenever my temporary object will be created when we pass object in parameters or return object of our class it will add for them too.
Static class member is the right way to go. A few things to be careful about:
Make sure you overload all constructors. The ones you don't want to support you should explicitly delete.
Don't forget to decrement in destructor.
If this program of yours is multithreaded then use atomic_uint or provide locking mechanism of your own.

Are cfc's safe in the application scope [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm kind of new with cfml and I am trying to figure out if putting components in the application scope is okay, if everything in the component is var'd.
I know that scope is shared by all users but if all the variables are var'd in the functions would that prevent people from seeing each others data and/or race conditions?
Thanks
If the data you store in the component belongs to the whole application, like e.g. some page layout data, it is fine to store it in the application scope.
Also, if the component is stateless, i.e. all the variables in it are in the local scope, it is safe to store objects created from it in that scope.
Though if the component is stateful, i.e. it stores data from previous requests or user specific data in it, it is not safe to store objects created from it in the application scope, because this may cause data leakage and other unexpected and unwanted results due to race conditions or incorrect access.
In that case you should rather store your component in the session scope.

Scopes in blank function c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hy, my question is simple..
I have this function.
CPythonMessenger::CPythonMessenger(): m_poMessengerHandler(NULL)
{
}
What scope have and why is there since constructor is empty and also is not used m_poMessengerHandler(NULL) i want to say the function is not used anywhere is constructor.
I guess you are examining (or maybe it is your code) THIS.
CPythonMessenger is the default constructor for the class CPythonMessenger as you can see in the relative header file HERE. After the : you can invoke a method (or another constructor) that must be run when creating an object of CPythonMessenger type. In particular, it creates an instance of m_poMessengerHandler that is then used in several place in the other class methods.

Creating Variable or Object with Names from text file c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm wondering if you can set or create a Variable or Objects name from text file.
example: text file("Iron Copper Steel")
read the file and create a new Variable or Object with the name "Iron" then continue reading and create a new object or variable named "Copper", etc.
No you cannot.
Object names/variable names are fixed before compiling the code.
You want to use an interpreted language
A variable is a data item, which needs to be stored in memory, and the name is mapped to that memory location. Each variable name must be given at compile time (except the temporaries, which have no name). If you want to give anything a name which you will use for some reason, you can create a class which can have a string for name and you can set it after reading the file. But this is totally different than the concept of variable name in C++.

Understanding Constructors in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I don't understand the purpose of constructors and how they function in C++. I understand that a constructor a is a function within a class that has the same name as the class but does not have a return type.
The purpose of a constructor in C++ is to reliably initialize raw memory, turning it into a useful object by establishing the chosen class invariant (what you can always assume about the object between calls of its member functions).
In contrast, an assignment has to change the value of an already initialized object, and may for example have to deallocate buffers that already have been established.
As long as you don't use very low level features of the language there is effectively a constructor call guarantee: that every object instantiated from a type T that has at least one user defined constructor, gets a single external call to a T constructor, which happens before anything else. Conversely, when you call a constructor by using the type name as a pseudo function name, T(), or with arguments, a T object is created. So the guarantee works both ways, and means that object creation involves constructor call and vice versa.