Copy Constructor and when it’s called?
Constructor which initializes its object member with another object of the same class.
Declaration :-
NO_RETURN_TYPE CLASSNAME (const CLASSNAME & CLASSNAMEObject)
clsABC is the class, I am going to use in every example listed below
Copy constructors are called in following cases:
- when you construct an object based on another object of the same class
clsABC objAbc ; clsABC objabc1 (objAbc); // Copy constructor is called clsABC objabc2 = objAbc; // Copy constructor is called clsABC objabc3; objabc3 = objAbc; // Operator Overloading is required here.
- when a function returns an object of that class by value
clsABC functionCopyContructorCalled() {} clsABC& functionCopyContructorNotCalled() { return HEAPVALUEObject;}In second example, since we returning by address, we should ensure the object whom memory allocated on heap should return, otherwise if you try to return object which is allocated in stack in function, you may receive the object, whose destructor is already being called when function ends.
- when the object of that class is passed by value as an argument to a function
void functionCopyContructorCalled(clsABC objABC) {} void functionCopyContructorNotCalled(clsABC& objABC) {} // since object is passed by address
Design Pattern Video Session : Singleton Pattern
I recently recorded my first video on Singleton design pattern, please let me know if you like it :-
- Singleton Design pattern Part 1
- Singleton Design pattern Part 2
Static Member variable (Why we need global declaration)
Let start with small code
class CStaticMember{
private:
static int s_iCallrecords;
public:
static int returnCallrecords() { return ++s_iCallrecords; }
};
int _tmain(int argc, _TCHAR* argv[])
{
int i = CStaticMember::returnCallrecords();
return 0;
}
If you see the code I have declared one static member variable (s_iCallrecords)and one static member method (returnCallrecords), however when I compile the program it’s giving me following error only for static member variable
“error LNK2001: unresolved external symbol "private: static int CStaticMember::s_iCallrecords" (?s_iCallrecords@CStaticMember@@0HA)”
Not for the static member method, you must be thinking why?, it’s because we are only declaring the member variable in CStaticMember class i.e. we are not providing it storage/memory and in case of static method, we are declaring as well as defining the member function in class itself, that’s why it’s don’t give you any link error.
Also, since static member has file scope, not the class scope, i.e. it will not get created or dissolved with each object of the class, which in case of normal variables.
So solve this problem: you have to provide global declaration of the variable, just like you do for your class methods, if giving it definition outside the class.
If C++ support in-place initialization of the variable (like .Net/Java Languages do), we can solve this problem without defining it outside the class.
This is our modified code looks like, i have include following line to make it working:-
int CStaticMember::s_iCallrecords = 0;
Modified code :-
class CStaticMember{
private:
static int s_iCallrecords;
public:
static int returnCallrecords() { return ++s_iCallrecords; }
};
int CStaticMember::s_iCallrecords = 0;
int _tmain(int argc, _TCHAR* argv[])
{
int i = CStaticMember::returnCallrecords();
return 0;
}
parallel_for
Inspiration of this article comes from this link. So I thought of finding same in the Unmanaged C++, so that we can utilized the power of parallel programming using C++. Since my machine is dual core machine, so I tried running two tasks parallel and you can see there is substantial difference between sync and parallel application call.
Here we can utilize the services of parallel_for for running task parallelly. In this program I have demonstrated the running task synchronously and parallel.
#include "stdafx.h"
#include <Windows.h>
#include <ppl.h>
using namespace Concurrency;
using namespace std;
void WaitFunction(int n)
{
Sleep(1000);
printf("\nSleeping over for conncurrent task %d",n+1);
}
int _tmain(int argc, _TCHAR* argv[])
{
// Running Task Synchronously
DWORD dwStart = GetTickCount();
for(int i=0;i<2;i++)
WaitFunction(i);
printf("\nTime Elaspsed %ul seconds \n",
(GetTickCount()-dwStart)/1000);
// Running task parallel
dwStart = GetTickCount();
parallel_for(0,2,&WaitFunction);
printf("\nTime Elaspsed %ul seconds \n",
(GetTickCount()-dwStart)/1000);
return 0;
}
Here is the output the above program. You can see substantially difference.
Small Discussion
Here, I am discussing the project situation using legacy software built in C++. One of old colleague caught in peculiar problem where he wants something which should be accessible from both dot net and VC++ applications, and the application can share data through it.
What I recommend is to create out-of-process COM component, since client can talk to single interface, data-sharing would be easy and shared memory code would be centralized at same place. Though there are number of method (Inter-process methods) to share data across different application. However everything has its own benefits and shortcoming.
Since my friend is working on prototype, so I advise him simpler method. Which are as follow:-
- Create a ATL-COM Server, which can utilize Out-of-Process benefits and data can stored in separate place, owned by its unique process.
- Add Interface, also add to two method or property to create getter/setter functionality.
- To keep data sharing simpler, we can implement singleton pattern to hold variable, which persist its values between different interface method call.
- Create dummy client to access the data between applications.
Though there are several methods of Interprocess communication and data sharing, which is beyond this post. I can discuss each of them one by one with benefits.
Note: I am not avid blogger, there is lot in my mind, however pouring that into word is something difficult for me. I am trying to improve over it. Please feel free to use comment box below to share you feeling and I will try to solve it best based on my knowledge.
