|
now i have something as following:
A a* = new A();
B b* = new B();
there is a class C created in the constructor of A, but i want the b class also can use this C class. for example:
A a1* = new A(); //c class will be created (we call it c1 for differencial)
A a2* = new A(); //c class will be created (we call it c2 for differencial)
B b1* = new B(); // I want b1 share the c1 with a1, but not use c2
B b2* = new B(); // I want b2 share the c2 with a2, but not use c1
Is there anyway i can do this?
Thanks a lot in advance.
|
|
|
|
|
Class A<br />
{<br />
private:<br />
C *c1;<br />
public:<br />
c1* GetThatClass()<br />
{ <br />
return c1 <br />
};<br />
}<br />
<br />
Class B<br />
{<br />
private:<br />
A *a;<br />
C *c;<br />
Public:<br />
StoreThat(Class *a1)<br />
{<br />
a = a1;<br />
}<br />
<br />
Somefunction()<br />
{<br />
c = a->GetThatClass();<br />
c->CallMethod();<br />
}<br />
}
Something on this line.
-prakash
|
|
|
|
|
The code will work on i create the classes a1 and b1, but now they wont be created by me, it will be created by others, they only pass in one parameter, like A a1 = new A(1), C c1, B b1 = new B(1), then i should know c1 is shared by a1 and b1. that means b1 will go to find c1.
|
|
|
|
|
Who is designing the 3 classes? you or someone else?
-prakash
|
|
|
|
|
|
So you can very well lay down the rules how to use ur classes, you need to link all 3 classes somehow so that they know which object to use.
The one i showed in the earlier post is one of the methods, there could be other method.
-prakash
|
|
|
|
|
The way I use is a common header. For example, you might have classes A, B and C.
Then the header would be the following (assuming you have separate cpp and h files for each class):
<br />
class A;<br />
class B;<br />
class C;<br />
#include "A.h"<br />
#include "B.h"<br />
#include "C.h"<br />
Then you just #include this common header in each class' header and voila! And yes - don't forget to remove other includes of A, B and C in these class headers.
Works fine for me.
|
|
|
|
|
|
Sounds like you might need a factory.
class factory {
factory(int num, a *A, b *B) {
A =new a(num);
B = new b(num, a,C);
}
};
class a {
private: // THIS IS IMPORTANT!
a(...)
friend class factory
class C;
...
}
class b {
private: // AGAIN, IMPORTANT
b(...)
...
}
Note that I skiped some details, that you will need to take care of, but this should give the idea.
It occurs to me that you could do this differently with a static vector. I don't use the STL (I have to support a compiler that doesn't support modern C++), but something like this instead:
class Cfactory {
private:
static STL::vector<class c=""> Cs; // I'm not sure about this syntax! The static is critical to this though
static getSharedC(num) {
c *C;
if((C = Cs[num]) == NULL) {
C = new c(num);
Cs.add(num,C);
}
return C;
}
};
Again, I left out a lot of details, and I'm not even sure how the vector class works, but you should be able to make it work.
The last has two major problems! The first is easy to work around, but the second could be a show-stopper.
First, you need something to prevent memory leaks. Reference counting is easiest (that I know of, maybe a smart pointer would work?), something needs to make sure that c1 goes away only after both a1 and b1 is deleted.
Second, this gives you global context for all c1. You can't have different two parts of the code creating their own a1, because even though the a1 class is different, those two instance share the same c1! You need to figure out how to deal with this. I can't think of anything that I'd really trust.
|
|
|
|
|
How to list the program hooking windows?
ex: language bar in win 2000,XP, the anti virus program...
Thanks!!!
[VC++ or Delphi]
Help me, please! Thank you very much
Mail: lekhacnhu@gmail.com
|
|
|
|
|
I have a function that performs some drawing according to the bitdepth of the screen. The function is called in a loop continuously. The problem is, after a while it crashes. I have narrowed it down the problem to the HDC not being released. How can i release it?
void Plot()
{
//Obtain current screen depth
HDC hdc = ::GetDC(NULL);
int screendepth=GetDeviceCaps(hdc, BITSPIXEL);
//do something here according to the current bitdepth
//how to release here?
}
|
|
|
|
|
From the MSDN documentation on the GetDC function
After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do
not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is
limited only by available memory.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
The following code below is from a text on numerical recipes in C++...Its purpose is to evaluate the integral of a function, "func", using the Gaussian Quadrature formula, viz:
<br />
double qgaus(double func, double a, double b)<br />
{<br />
double xr,xm,dx,s;
double x[] = {0.1488743389816312,0.4333953941292472,<br />
0.6794095682990244,0.8650633666889845,0.9739065285171717};<br />
double w[] = {0.2955242247147529,0.2692667193099963,<br />
0.2190863625159821,0.1494513491505806,0.0666713443086881};<br />
<br />
xm = 0.5*(b+a);<br />
xr = 0.5*(b-a);<br />
s = 0;<br />
<br />
for(int j=0;j<5;j++)<br />
{<br />
dx = xr*x[j];<br />
s += w[j]*(func(xm+dx)+func(xm-dx));<br />
}<br />
return s *= xr;<br />
}
Im trying to adapt the code to integrate my function, "func", which returns an array of values, viz:
<br />
void function(double (*func)[nSize],CKuchukDlg *pDialog)<br />
{<br />
enum{nSize = 100};<br />
double Ip[nSize],p[nSize],gh[1][nSize];<br />
double alpha[nSize],beta[nSize];
<br />
for(int t=0;t<nSize;t++)<br />
{<br />
if(t==0)<br />
{<br />
alpha[0] = beta[0] = 1;<br />
p[0] = sqrt(pow(alpha[0],2.0)+pow(beta[0],2.0));<br />
<br />
homo(gh,nSize,pDialog);<br />
Integrals(Ip,nSize);<br />
<br />
func[0][0] = (gh[0][0] * Ip[0])/p[0];<br />
}<br />
else<br />
{<br />
alpha[t] = beta[t] = 2*t*PI;<br />
<br />
homo(gh,nSize,pDialog);<br />
Integrals(Ip,nSize);<br />
<br />
p[t] = sqrt(pow(alpha[t],2.0)+pow(beta[t],2.0));<br />
<br />
func[0][t] = (gh[0][t] * Ip[t])/p[t];<br />
}<br />
}<br />
}<br />
Can anyone please show me how I may do this? I have tried a number of ways, but I keep running into errors. Thank you!
|
|
|
|
|
I need to refer something on C++ and found this
#include <iostream>
using namespace std;
int main()
{
int foo = 4;
int bar = 16;
// ptr - non-constant pointer, non-constant data
//
int* ptr = &foo;
*ptr = 6; // OK: Data is non-constant, so it can be
// changed via ptr
ptr = &bar; // The pointer is non-constant, so it may
*ptr = 22; // point to other data And change it
// ptr_to_const - non-constant pointer, constant data
//
const int* ptr_to_const = &foo;
return 0;
}
When i read this
const int* ptr_to_const = &foo;
Non constant pointer to constant data ;
But foo isnt declared as constant .
Why does the compiler not warn me of this ?
|
|
|
|
|
|
A pointer to constant data needs to be initialized, that's what this line does, and no specification says the "initializer" must be constant. Using pointers to constant data is in essence declaring an intention not to modify that data, nothing more. Sure, if you try to modify it, no warning will be shown - it'll be an Error!
The other possible case is declaring a constant pointer. You certainly can initialize it, but not make it point anywhere else afterwards.
|
|
|
|
|
Because prt_to_const is a promise that you won't modify the data through ptr_to_const, not that the data it points to cannot change by other means!
One good reason to use const is to force other code to not change something. A class might return a ptr_to_const, that points to private internal class data. The class itself might modify the data via some function call latter, but it would be a mistake to let anything other than the class modify that internal data.
|
|
|
|
|
Hi Everyone
I have a DLL which should later monitor a specified app. But how can I notify my Main Program that something has change ?
starts Routine
-----------------------------
|
\/ ^
|------| |----------------|
| DLL | | Main Program |
|------| |----------------|
| ^
| |
--------------------------
should notify the program if something changes
So i thought about a callback function, but i don't know how i can realize this
Please help ???
MFG
RedDragon2kx
Unix and C are the ultimate computer viruses.
Visit me: www.programming.info.ms
|
|
|
|
|
maybe in your dll you need to have an function that you can call that will check your app. Pass a message to the app and have the app send a message back with it's status.
I guess you need to ask your self.
1. How is this dll going to be loaded. From another application or your main app?
2. What is it that you are monitoring. Whether the main app is still running?
3. What is the purpose of the dll? Will it tell me something about the main app that will make it (the dll) do something else?
4. Is the main app something you wrote or someone else wrote?
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
So I have 3 Apps:
The first App is the MainApp which loads the DLL
The second "App" is the DLL which montiors the third App
The third App is some app which gets monitored by the DLL
While all apps are still running the DLL monitors the third App. And when the third App for e.g. calls WM_PAINT the Main App should get notified by the DLL.
Unix and C are the ultimate computer viruses.
Visit me: www.programming.info.ms
|
|
|
|
|
Much clearer. What you want to do is create a hook in your dll, so that you can monitor any messages that are being sent to that app. There are a ton of articles on window hooks.
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Any interprocess communication technique will do. I would recommend using a memory-mapped file or thread messages, but that's up to you. For a list of effective IPC mechanisms refer to J.Richter's "Programming Windows", it's worth reading anyway.
|
|
|
|
|
I'm having a problem passing a structure with WM_COPYDATA. Can anyone give an example of how to do this on the sending side an the receiving side?
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
How are you currently doing it? Are you passing the address of your struct in the lpData member of the COPYDATASTRUCT?
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04
Within you lies the power for good - Use it!
|
|
|
|
|
yes. Here is my function that send the message from a thread I created:
<br />
typedef struct tagCONNSTATUS {<br />
int nConnID;<br />
CString sThreadID;<br />
CString sConnStatus;<br />
}CONNSTATUS;<br />
CONNSTATUS ConnStatus;<br />
<br />
ConnStatus.nConnID = ConnID;<br />
ConnStatus.sThreadID = thdID;<br />
ConnStatus.sConnStatus = "Connected";<br />
<br />
COPYDATASTRUCT myData;<br />
myData.dwData = 0;<br />
myData.cbData = sizeof(ConnStatus);<br />
myData.lpData = &ConnStatus;<br />
<br />
<br />
if (!SendMessage(myHandle->m_hWnd, WM_COPYDATA, 0, (LPARAM)&myData))<br />
MessageBox(NULL, "Error Posting Connection Message", "Error on PostMessage", MB_OK | MB_ICONEXCLAMATION);<br />
<br />
Here is the function that receives the message in my main application:
<br />
BOOL CLAMsgSwDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)<br />
{<br />
typedef struct tagCONNSTATUS {<br />
int nConnID;<br />
CString sThreadID;<br />
CString sConnStatus;<br />
}CONNSTATUS;<br />
<br />
(DWORD)((CONNSTATUS*)(pCopyDataStruct->lpData))->nConnID;<br />
(LPCSTR)((CONNSTATUS*)(pCopyDataStruct->lpData))->sThreadID;<br />
(LPCSTR)((CONNSTATUS*)(pCopyDataStruct->lpData))->sConnStatus;<br />
<br />
CONNSTATUS ConnStatus;<br />
AfxMessageBox(ConnStatus.sConnStatus);<br />
AfxMessageBox(ConnStatus.sThreadID);<br />
<br />
return 1;<br />
}<br />
<br />
When my message boxes pop up they show nothing.
Tom Wright
tawright915@yahoo.com
|
|
|
|
|