|
The key point here is the extern qualifier. That states that the variable can be referred to from any module in the final linked executable, but only one instance of it will actually exist.
|
|
|
|
|
C++ has very specific way / syntax to instantiate a class with parameters,
so basic that Google can't find it for me .
I need this for implementing multiple inheritance.
I can instantiate the "top class" in multiple inheritance hierarchy , but it uses system generated default constructors, without parameters of course.
My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes.
Do I make sense? I hope so.
Thanks
Vaclav
|
|
|
|
|
|
Quote: but it uses system generated default constructors, without parameters of course Only if you don't provide an implementation of (with or without parameters) the class contructor.
Quote: My goal is to create an instance of the top class with parameters passed to the hierarchy lower chain classes.
Do I make sense? Before attempting the multiple-inheritance step, make sure you understand how class constructor/destructor work.
|
|
|
|
|
class A
{
public :
A( int p ) : a( p )
{
}
protected:
int a;
};
class B
{
public:
B( int q ) : b( q )
{
}
protected:
int b;
};
class C : public A, B
{
public :
C( int x, int y, int z ) : A( x ), B( y), c( z )
{
a = x;
}
private:
int c;
};
int _tmain(int argc, _TCHAR* argv[])
{
C test( 1, 2, 3 );
return 0;
}
Will this help?
|
|
|
|
|
Well, it does and does not help.
I am probably wrong , but I was looking for something like this
class TestClass : paramaterX = "Text"
{
}
I am looking into multiple inheritance and was trying to verify that the classes in the scheme were actually constructed.
Simple
class A : public B,C,D
setup.
So I implemented default constructor and have it to output some text to the console, sort of. It does work.
Now I am trying to find a way to instantiate the scheme with parameters passed to some of the hierarchy classes.
something like
class A : ( class A parameter list ), public B : ( class b parameter list)...
Is that possible ?
BTW I cannot figure out the real syntax / usage of the basic syntax
class A : ?
For example
class A : text
{
};
the compiler wants to see class definition before "text", text is class A variable.
|
|
|
|
|
You could be trying to do several things with the multiple inheritance you need to be very precise here.
You give us "parameterX" which is sort of labelled as text but that could mean a string class, a c string,
or a static const array. You also don't tell us whether the place you want to store the passed text is
in the object new to this class or part of one of the inherited classes, it may matter .. depends.
Do you see how many variations there are on what your simple description says? This is what makes multiple
inheritance tricky and you need to be very clear.
If you search the words "C++ composite class" is that what you are trying to do?
It appears to me you have jumped in at the deep end with C++ classes and trying to do the most complex
without first understanding the basics. I would highly recommend you might like to also do a search of
"C++ Parameterized Constructor" and review it given the code you have written above is nonsense.
I would also recommend you look at
Constructors (C++)[^]
That covers the basics of construction order and explicit and derived constructors in multiple inheritance.
My best guess you are trying to derive the base of a composite class and it would be this
class TestClass {
public:
TestClass(const string& InitText) { text = InitText; };
string text;
};
In vino veritas
modified 3-Oct-16 23:05pm.
|
|
|
|
|
Hi,
Vaclav_Sal wrote: something like class A : ( class A parameter list ), public B : ( class b parameter list)...Is that possible ?
As a direct answer, I would say that, it is not possible and instead, we can use constructors with parameters.
Vaclav_Sal wrote: BTW I cannot figure out the real syntax / usage of the basic syntax class A : ? For example class A : text{};
Please have a look at Richard's link too.
|
|
|
|
|
I just twigged what he is trying to do via you answer, he is trying to make a static composite class isn't he.
Like when beginners to C, try to create a struct with constants within the struct definition.
In vino veritas
|
|
|
|
|
Let me try it again.
I have an inheritance hierarchy
class A : public B, C
When I instantiate class A - A a; - all classes get constructed via default constructor provided by the language if I do not specify them.
Now this is not much over my head , I don't see need to further educate myself in this basic.
This inheritance scheme at that point works just fine.
Next step - class B needs to be constructed using real constructor. Again I know how to pass parameters to class constructor.
<b>I just do not know how to do it , or can it be done during the construction of the class A.
</b>
As far as taking on more than I can chew, that's my business.
If you do not want to participate, that is your choice.
No "google it" remarks are worthy of your and my time.
I am sorry about the above remarks , but I am getting tired of folks with " my grandma can beat up yours " attitude. I just hate to see it popping up on this forum too.
All I actually need to know is "composite class", and I can take it from there.
Thanks
-- modified 4-Oct-16 9:26am.
|
|
|
|
|
Vaclav_Sal wrote: I am getting tired of folks with " my grandma can beat up yours " attitude. Much as we feel about you.
|
|
|
|
|
Sorry my mind reading was a bit off, I am however glad you worked out you were trying to build a composite class.
The forum can help with specific small problems but it's not something that can teach complex techniques in C++.
In vino veritas
|
|
|
|
|
Yes, this time using correct terminology was the ticket. I am not sure why the OP was judged as too complex to ask here about. I am still puzzled why I did not get a clear example of syntax passing list of parameters to class directly, not via constructor. The link posted was not that clear how to name / use the list.
It really does matter to me how I get the parameters to correct class in the hierarchy.
The example of composite class suggest I have to follow the hierarchy and pass the parameters thru each constructor to the next one.
I suppose when I get the code to play I could post it here for everybody can see it is really not that complex.
Cheers
|
|
|
|
|
Correct
The problem as you will find is hierarchy order as you will find. Sometimes the order will not
be the way you want in different functions and you will need to control it and that gets down
to some very nitty details.
The question we will still always pose back to you is do you really need the hierarchy at all.
I can't stress enough to read one article from wikipedia it can save you alot of heartache
https://en.wikipedia.org/wiki/Composition_over_inheritance[^]
In vino veritas
|
|
|
|
|
I am writing standard C programming language for an ARM processor and I would like to combine a handle (a pointer to a struct with state variables) with an enum like this:
struct MyStateStruct_t {
int a;
};
const struct MyStateStruct_t myStates [2];
typedef enum
{
MY_SELECTOR_0 = (const int)(&myStates[0]),
MY_SELECTOR_1 = (const int)(&myStates[1])
} MyEnum_e;
When I try to compile, I get the following errors:
enumerator value for 'MY_SELECTOR_0' is not an integer constant
enumerator value for 'MY_SELECTOR_1' is not an integer constant
Does anybody know a workaround? Of course, I could use a look-up table array to convert from the enum-values to the addresses, but if possible I would like to avoid that. Thanks for any help!
|
|
|
|
|
Enums must be defined integer constants at compile time.
|
|
|
|
|
As Richard said enum is
1.) Restricted to a 16 bit integer constant
2.) Must be defined at compile time
Your pointer is longer than 16 bits and the address is only available at runtime, so it double fails.
You also haven't typedef the structure to a name.
I suspect this is what you are trying to do
typedef struct MyStateStruct_t {
int a;
} MYSTATE;
typedef enum {
MY_SELECTOR_0 = 0,
MY_SELECTOR_1 = 1
} MyEnum_e;
const MYSTATE myStates[2];
enum MyEnum_e selector = MY_SELECTOR_0;
int result1 = myStates[selector].a;
selector = MY_SELECTOR_1;
int result2 = myStates[selector].a;
In vino veritas
|
|
|
|
|
Hi
I am looking to be Notified When something is downloaded to my FTP folder
towards that end I have implemented FindFirstChangeNotification being a mainframe programmer its taken a while to get used that when I do a Wait I stop the entire thread
So I have 2 options put the wait in a different thread or.... code a timer value other then infinite and in the case of WAIT_TIMEOUT loop "while(TRUE)" and Only "break when
the object is signaled
|
|
|
|
|
Usually you don't stop an entire thread while waiting you are forced to do it with MFC, so I am guessing this is your MFC program again.
Within MFC personally I would create a thread which just spins it wheels checking whatever and the thread simply posts a message back when an interesting event occurs. I wouldn't be putting the wait I would be putting the whole code that requires the wait in a thread and use messages to communicate with the MFC code. So make a proper worker thread.
In vino veritas
|
|
|
|
|
So you are saying creating a new thread to wait for notification is a better choice then an ifinate loop with say
A 1000 millisecond in the WaitForSingleObject only breaking out of the loop when the event is signaled
Truth is a new thread leaves the CPU time distribution up to Windows
Thanks
|
|
|
|
|
It's a low priority Hotwatch .. it will always be better to do as a thread and it isn't hard
typedef struct _MYWATCHERDATA {
HWND AppWindow;
UINT MsgId;
WPARAM CmdId;
char* FolderToWatch;
HANDLE dwChangeHandles[2];
BOOL threadExitComplete;
} WATCHERDATA;
DWORD WINAPI NotifyMeOfFolderChangeThread(LPVOID lpParam)
{
BOOL exitSignal = FALSE;
WATCHERDATA* watchData = (WATCHERDATA*)lpParam;
watchData->dwChangeHandles[0] =
FindFirstChangeNotification(watchData->FolderToWatch,
FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME);
if (INVALID_HANDLE_VALUE == watchData->dwChangeHandles[0])
{
DWORD dwError = ::GetLastError();
return dwError;
}
while (exitSignal == FALSE) {
DWORD dwWaitStatus = WaitForMultipleObjects(2,
watchData->dwChangeHandles, FALSE, INFINITE);
switch (dwWaitStatus) {
case WAIT_OBJECT_0:
PostMessage(watchData->AppWindow, watchData->MsgId,
watchData->CmdId, (LPARAM)watchData->FolderToWatch);
FindNextChangeNotification(watchData->dwChangeHandles[0]);
break;
case WAIT_OBJECT_0 + 1:
exitSignal = TRUE;
break;
}
}
watchData->threadExitComplete = TRUE;
return 0;
}
The use of the thread involves, initialize, use, wait for thread to exit .. cleanup
char* watchDirectory = "c:\\temp\\";
WATCHERDATA* myWatch = (WATCHERDATA*) malloc(sizeof(WATCHERDATA));
myWatch->AppWindow = myAppHandle;
myWatch->MsgId = WM_APP + 100;
myWatch->CmdId = 1;
int len = strlen(watchDirectory) + 1;
myWatch->FolderToWatch = (char*) malloc(len);
strcpy_s(myWatch->FolderToWatch, len, watchDirectory);
myWatch->dwChangeHandles[0] = 0;
myWatch->dwChangeHandles[1] = CreateEvent(0, FALSE, FALSE, "EXIT");
myWatch->threadExitComplete = FALSE;
HANDLE workerThread = CreateThread(NULL, 0, NotifyMeOfFolderChangeThread, myWatch, 0, NULL);
SetThreadPriority(workerThread, THREAD_PRIORITY_LOWEST);
Sleep(10000);
SetEvent(myWatch->dwChangeHandles[1]);
while (myWatch->threadExitComplete == FALSE) {};
free(myWatch->FolderToWatch);
free(myWatch);
In vino veritas
|
|
|
|
|
|
Leon
Just wanted to tell you again you are correct I have Sockets in my code
So Typically after I do CAsynSocket connect I do the send after I get the OnSend Notification
From The FrameWork
My Sockets has a CWinThread Wrapper, However the notification like OnSend comes in
The Context of the Main Thread
Since I put WaitFor...Object api (In The Main Thread) even though I didn't have INFINITE
it messed up the Windows Dispatcher and I wasn't getting the notification
I am going to try your code out Shortly
Thanks again
|
|
|
|
|
Yes that is a generic schema for not doing that sort of thing ... I hacked the MSDN thread sample.
When I do socket apps I do exactly the same schema I have a write thread and a read thread of exactly
the form above. The socket IO structures and objects are held in the memory block.
Basically you want your app just dealing with messages not sitting around doing things .. bring up task
manager and look at the number of threads running normally. It really expects and wants this sort of
behaviour, it's designed around it.
Lifetime of app threads in particular are extremely easy because you can launch them on the WM_CREATE
of the main app window and dispose of them in the WM_DESTROY. You don't have the problem of who is
responsible for the shared data and memory (and it's cleanup) that you have with intermittent start
and stop threads. The app window handle to pass to the thread to message you also can't get wrong
They give you huge blocks of message ids at WM_APP and WM_USER to allow you to do it. All those ID's
you are free to use but you might want to check MFC might use a few (that is why I jumped 100) and
they may restrict the range down a little.
In vino veritas
|
|
|
|
|
Present for you as I checked my code putting it into an application
Code: Folder watcher code[^]
Compiled EXE:Folder HotWatcher EXE[^]
It's a full folder watcher app with a basic watcher thread and detailed watcher thread.
Basic just tells you if the folder changed, Detailed tells you file changes and what action was done.
Basically run the app select an unimportant directory like c:\temp and then copy, delete, rename files to the directory and you will see the messages reported to the app.
In vino veritas
|
|
|
|