|
They're all pointers, and pointers are simply an address in memory.
Everyone is different, but for me, the best way to really understand a C++ concept is to write a small console program and experiment using the scientific method--that is, I hypothesize how something should work, write simple code to test that hypothesis and refine. (Sometimes, I open a disassembly view in debug mode, but that may understandably be even more confusing for many.)
Edit: A word of warning. Don't return a pointer to an object allocated on the called function's stack. While the data it points to may be valid immediately after the call, it will likely become corrupt with the next function call.
|
|
|
|
|
Hello,
Can you guide me though simple steps to create a console application which can also handle windows events?
|
|
|
|
|
A console app is not a Windows app, so it does not receive Windows events.
|
|
|
|
|
My mean to say...
Can I create windows console based app which can receive events?
I think in Windows service we can receive events, same way I want. Is that possible?
|
|
|
|
|
Fedrer wrote: windows console based app What does that mean? An app is either Windows based or Console based, it cannot be both.
|
|
|
|
|
Not true for a long time .. there is no such thing as a "real console" anymore the whole DOS kick has been dead since Windows 7.
The console is entirely emulated now on windows, they just use a different message handler by default.
In vino veritas
|
|
|
|
|
leon de boer wrote: there is no such thing as a "real console" I never claimed that there was. I merely stated that the two application types are quite different.
|
|
|
|
|
Fedrer wrote: Can I create windows console based app which can receive events?
I think in Windows service we can receive events, same way I want. Is that possible?
What do you mean by "events"?
Is it what we see in the Windows Event Viewer?
Or you meant the Windows Messages?
If you mean a kind a "messaging" for services then you should choose some of the IPC mechanism such as Named Pipes.
See also winapi - Receive Windows Messages in a Service - Stack Overflow
|
|
|
|
|
If you mean by events Windows messages, you just have to implement a message loop. But a console application does not have a window that is required as message recipient. So you can only receive messages posted to threads. See How to post a thread message to a console application[^] for an example.
Alternatively you can create a (optionally hidden) window that receives the messages. Or create a GUI application (usually dialog based) and hide the window.
|
|
|
|
|
Ok so the whole console now is full emulated since Windows 7 it is just a normal window.
However it has a special message handler which filters out the normal windows messages and it doesn't have a proper message queue setup. It's rather technical to go thru and add the steps to convert it when there is a much simpler way.
For the record WM_TIMERTICK and a few other windows message do get passed thru (you can use timers now on a console app) .. This will work as illustration there is a normal windows queue present on a console window however expanding the queue and removing the filter is far more problematic
int main()
{
SetTimer(0, 1, 1000, 0);
SetTimer(0, 2, 750, 0);
MSG Message;
while(GetMessage(&Message, 0, 0, 0))
{
if(Message.message == WM_TIMER)
{
std::cout << "Timer" << std::endl;
}
}
}
By far the easiest way to do what you want is actually setup for a proper windows app but then create and attach a console to it which is the new fancy feature.
AllocConsole function - Windows Console | Microsoft Docs[^]
AttachConsole function - Windows Console | Microsoft Docs[^]
So it basically does exactly what you are after you have a full windows message queue underneath and you can pump whatever message you want to the console. The actual GUI window can remain hidden by simply never setting the WS_VISIBLE flag or attaching it to the taskbar in minimized form.
Your whole app is basically a minimal hidden standard window throwing messages up to a console which is I believe what you are after.
In vino veritas
modified 4-Feb-18 19:53pm.
|
|
|
|
|
Solution:
To arrive all the priests and devils to the other side, 6 steps are to be followed:-
1) Firstly one devil and one priest will go to the other side. Devil will stay to the other side while priest will come back with the boat.
2) Remaining two devils will go to the other side.
One devil will stay to the other side while one devil will come again with the boat.
So total two devils are on the another side of river and 3 priests and 1 devil is on the one side of the river.
3) Now two priests will go. One priest will stay while 1 priest and 1 devil come back with the boat.
4) Now remaining two priests will go to the other side of the river.
Total 3 priests and 1 devil is on the one side while 2 devils is on the another side of the river.
1 devil will come back with the boat.
5) 2 devils will go to the other side of the river. 1 devil will stay and 1 devil will again go back with the boat.
6) Now the remaining 2 devils will come to the other side of the river.
All the 3 priests and 3 devils arrive safely.
kindly help this project
|
|
|
|
|
Help how? Since you have the solution you know what code to write.
|
|
|
|
|
At the top of this forum is a topic "How to ask a question", please read and pay particular attention to #2.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Member 13655542 wrote:
kindly help this project Help or Do? If the former, what have you done (for us to help you with)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
So, I finally decided to dive into C++ world, trying to adapt some C# code I've been working on lately.
So here's what I have.
bin.h file:
#pragma once
namespace Maths {
class Bin
{
public:
template <class T> static const T GetMask(int);
};
}
bin.cpp file:
#include "bin.h"
namespace Maths {
template <class T> const T Bin::GetMask(int count) {
return (T)0; }
}
This compiles just fine. However, in the same console project, when I write:
#include "bin.h"
using namespace Maths;
int main() {
unsigned char value = Bin::GetMask<unsigned char>(0);
}
I get a nasty LNK2019 linkage error on compilation:
Error LNK2019 unresolved external symbol "public: static unsigned char const __cdecl Maths::Bin::GetMask<unsigned char>(int)" (??$GetMask@E@Bin@Maths@@SA?BEH@Z) referenced in function "int __cdecl main(void)" (?main@@YAXXZ)
In the Bin class, I have a couple of other (non-generic) methods, which I can use just fine. I suspect a problem with static templated methods specifically, but I can't understand the core problem. Maybe I just try to do something which is not possible (using a static generic method), but I cannot figure out why it would not be possible.
Anyone could give me a hint on this?
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
The Maths namespace does not contain a definition/declaration of Bin::GetMask<T>(int); only one of Bin::GetMask(int); . Just remove the <unsigned char> piece and it should be fine.
|
|
|
|
|
Thank you Richard for answering.
However, if I try to remove the <unsigned char> piece (namely: unsigned char value = Bin::GetMask(0); ), the editor itself complains about the fact that no instance of function template "Maths::Bin::GetMask" matches the argument list.
Moreover, the Bin::GetMask<T>(int) is actually declared in Maths namespace. Problem seems to be with the .cpp part: the editor/IDE can see the definition of the generic method (since the editor does not complain when I try to use it, and the object explorer clearly displays the method with correct signature), but, on compilation, the linker cannot make a link to the actual implementation.
How can I correctly declare a static, generic method inside a non-generic class, then?
Thanks for your time
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
phil.o wrote: the Bin::GetMask<T>(int) is actually declared in Maths namespace. I don't see it anywhere, unless that is somehow implied by template <class T> static const T GetMask(int); .
Hmm, I though I understood templates.
|
|
|
|
|
Anyway, I found the issue. I cannot implement a template method in the .cpp file, like I did with other non generic methods. I moved the implementation of the generic method in the .h header file, and now everything is ok.
I guess I have to dig deeper in the translation unit concept
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
Yes, that's it. Template method definitions should be in header files.
|
|
|
|
|
phil.o wrote: the translation unit concept The only reason for creating header files is when you have a set of common definitions that need to be shared by multiple translation units. If you only have a single .cpp file then all the definitions can safely be put in there. The #include statement is recognised by the preprocessor which reads all the include files and creates a new text file containing all the included code followed by the user written C/C++ code, and passes that new composite to the actual compiler.
|
|
|
|
|
I have learnt it the hard way This explains why everything was fine as far as the editor/IDE were concerned, and why the issue only occurred on the linking phase. That's quite confusing for a beginner.
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
phil.o wrote: quite confusing for a beginner. and sometimes for the rest of us.
|
|
|
|
|
Hello,
I want to create application like windows explorer. I think I can use builtin tree and builtin list view.
I think there are some interface IShellFolderTree or IShellFolderList. Can you please guide me with any example/article?
|
|
|
|
|