|
In addition to what Ash said, if you are going to write a widowed application and you want it to be cross-platform, you could have a look to some cross-platform GUI library; a very good one is the Qt[^]: it has lots of classes and functionalities for GUI programming and not only (threading, XML, scripting, etc.)
|
|
|
|
|
I can only recommend boost.thread, for following reasons:
1. well documented + a lot of further resources, comments, published user experiences
2. actively maintained and extended
3. ..and in large parts compatible with c++0x
It even cooperates well (although care has to be taken) with e.g. .NET or Java multi-threading, as shortly sketched in the C++/CLI discussion thread.
|
|
|
|
|
the classwizard only allow you to handle the COMMAND, I want to implement the number wrap-around and have to handle the VSCROLL signal, how is that accomplished from the ribbon control CMFCRibbonEdit?
|
|
|
|
|
Hi all, I'm finding it difficult to assign a function that is a member of a class to a pointer to a function. I have a member function declared in a class as
bool FileMenu(void)
Then I have declared a pointer to a function as:
bool (*ptrLastMenu)(void)
But the assignment produces errors, and I don't know how to do this in C++. I don't know if it is different from the way the assignment is done in C which I am used to. Please help.
|
|
|
|
|
what have you tried? what are the symptoms?
wouldn't it just be ptrLastMenu=FileMenu; ?
|
|
|
|
|
class C1
{
void fx() { }
typedef void (C1::*memberFnPtr)();
void f2()
{
memberFnPtr g;
g = &C1::fx;
(this->*g)();
}
};
See: http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]
Use with care, because they (usually) mean that there is something about your design that you haven't thought through properly. I can't think of any situation right now where you can't use polymorphism to solve the same problem in code that you own. I haven't encountered any member function pointers in my job yet.
|
|
|
|
|
Member function pointers are really handy when you want to call the same member function on every element of a collection. It's either that or write a shim functor to do the call for you. So instead of:
class functor
{
public:
void operator()( object &obj )
{
obj.A();
}
};
std::for_each( data.begin(), data.end(), functor() );
you can use:
std::for_each( data.begin(), data.end(), std::mem_fun_ref( &object::A ) );
The first form is a real pain in the neck before C++0x as you can't define a local class or use a lamda. Your functor has to be defined "somewhere else" which can lead to a bit of flow interruption when you're reading code (for me anyway, your experience probably differs).
Cheers,
Ash
PS: Pedants please note my argument may not apply if...
- I'm not using a pointer, just taking the address of a member function
- I'm using someone else's (i.e. the standard library's)
|
|
|
|
|
I agree mostly. But I have to comment on the "flow interruption"
std::for_each( data.begin(), data.end(), increase_by_one() );
does not interrupt my reading as does this
std::for_each( data.begin(), data.end(), std::mem_fun_ref( &object::increase_by_one) );
In a code review, you would have an extra component to check of course, but that's another matter.
|
|
|
|
|
True, good naming removes most problems.
Cheers,
Ash
|
|
|
|
|
Consider the class -
class A
{
public:
void One() {}
}; Declaring and initializing a member function pointer would be -
void (A::*pmfn)() = &A::One;
Calling the method using the function pointer would be -
A obj;
(obj.*pmfn)(); OR
A* ptr = new A;
(A->*pmfn)();
|
|
|
|
|
The c++ syntax and semantics for pointers to members is admittedly confusing. You have seen the basic rules above. I cannot agree however with some of the statements posted above, especially this one:
Use with care, because they (usually) mean that there is something about your design that you haven't thought through properly. I can't think of any situation right now where you can't use polymorphism to solve the same problem in code that you own. I haven't encountered any member function pointers in my job yet
Consider the inversion of control idiom which is inevitable if designing loosely coupled component systems. Usually, you will want to implement something out of the publisher/subscriber family of patterns, like some sort of event/delegate or signal and slot mechanism. In c++, pointers to class member functions are a very handy way to do that. Fortunately, you do not have to use the "raw" c++ syntax - wise men have been working on that and gave us things like std::tr1::function (skip tr1 for c++0x), or even boost::signal and boost::signal2. Here "delegates" to c++ class member functions are quite easy to use:
namespace SubscriberContext {
class Subscriber {
int mID;
public:
bool FileMenu(void) {
std::out << "FileMenu of object " << mID << " called" << atd::endl;
return true;
}
public:
Subscriber(int pID) : mID(pID) {}
};
}
#include <functional>
#include <vector>
namespace PublisherContext {
class Publisher {
public:
typedef std::tr1::function<bool ()> Delegate;
private:
std::vector<Delegate> mSubscribers;
static void Invoke(Delegate pD) { pD(); }
public:
void Connect(Delegate pD) {
mSubsribers.push_back(pD);
}
void Disconnect() {
mSubscribers.clear();
}
void Publish() {
std::for_each(mSubscribers.begin(), mSubscribers.end(), Publisher::Invoke);
}
};
}
int main() {
using namespace std::tr1;
using namespace PublisherContext;
using namespace SubscriberContext;
Publisher tPub;
Subscriber tSubs1(1), tSubs2(2), tSubs3(3);
tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs1));
tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs2));
tPub.Connect(bind(mem_fn(&Subscriber::FileMenu), tSubs3));
tPub.Publish();
tPub.Disconnect();
}
Huh, I just wrote it down without trying to compile, so please, be tolerant... There is more to that than this, therefore I would recommend to have a look at boost::signals. With "naked" std::tr1::functions, like in the example above, you cannot, for example, disconnect one specific stub, since td::tr1::function objects are not comparable. But I hope you get the point.
Cheers,
Paul
|
|
|
|
|
I think (and I might be incorrect on this) that the poster you were originally quoting was saying that you could do the same thing using an abstract base class, one implementation per Delegate in your example. And you'd get about the same separation of concerns as well.
This sort of thing tends to be horses for courses - people who think in OO terms are likely to grab onto using interfaces for everything while people who think in compositional or functional terms will tend to reach for templates.
Cheers,
Ash
|
|
|
|
|
Aescleal wrote: This sort of thing tends to be horses for courses - people who think in OO terms are likely to grab onto using interfaces for everything while people who think in compositional or functional terms will tend to reach for templates.
It's not exactly the same, if you don't mind... With functors of that kind you get a first class object representation of a function, basically grabbing the best of both worlds. If you go the pure OO-way , you define an ISubscriber and end up with a variation of the observer pattern. Then, the components are loosely coupled (by sharing the ISubscriber definition) but they are coupled: The only way to subscribe for the Publisher service is to feed him with polymorphic ISubscriber instances.
With the approach sketched above, the Publisher is not coupled to a consumer of it's services at all - the subscriber only needs to satisfy the signature of Delegate . So the "wire up" part could look like this:
bool LooseSubscriber() {
std::out << "LooseSubscriber called" << std::endl;
return true;
}
int main() {
Publisher tPub;
Subscriber tSubs(0);
tPub.Connect
(bind(mem_fn(&Subscriber::FileMenu), tSubs));
tPub.Connect
(LooseSubscriber);
tPub.Connect
([] (void)->bool {
std::out << "lambda Subscriber called" << std::endl;
return true;
});
}
|
|
|
|
|
I didn't say it was exactly the same - you're preaching to the choir here. As I said you get almost the same separation of concerns - the difference is instead of defining an interface (which subscriber doesn't have to be derived from...) you're using a functor from the standard library.
|
|
|
|
|
Aescleal wrote: you're preaching to the choir
You've got me, I could not resist... We've probably lost the thread starter now, but I just wanted to show him/her that with a little help from the std library, pointers to member functions are easy to use.
Cheers,
Paul
|
|
|
|
|
Hi,
I am facing a problem related to window handle.
I have loaded two different OCXs in different instances of the same container application(OLE Server).
In OnCreate() method of second OCX control I am calling the AfxGetMainWnd()function.
It is called after calling the base class function of OnCreate() method.
Now trying to run both instances simultaneously.
The problem is sometimes AfxGetMainWnd()function returns the pointer to a window of first OCX control which is loaded in the first container instance just before. Why it happens?
Thankx in advance.
Regards,
SUnil
|
|
|
|
|
I have done my code to resize the controls of window when we drag the window..its working fine in normal scenario...
if we un check the option show window contents while dragging in performance settings(MyComputer->properties->advance->performance->Settings) of windows.
my window controls are not getting resized any method to over come this....thanks in advance
|
|
|
|
|
where did you place the code to resize the controls? If its is in OnSizing(), try by moving it to OnSize(). Check ::Iswindow() on any of the controls before resizing them, since OnSize will be called once before the child controls are created.
|
|
|
|
|
See if the Extras section of this article helps.
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi developers, i try to write a file in binary format. But that file is not in binary format, i dont know what's wrong in my code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("file path", ios_base::binary | ios_base::in);
char buf[] = "BinaryFile";
file.write(buf, sizeof(buf));
file.close();
}
Thanks in Advance
|
|
|
|
|
You have posted the same question twice; you already have an answer here[^]
|
|
|
|
|
Hi developers, i try to write a file in binary format. But that file is not in binary format, i dont know what's wrong in my code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file("file path", ios_base::binary | ios_base::in);
char buf[] = "BinaryFile";
file.write(buf, sizeof(buf));
file.close();
}
Thanks in Advance
|
|
|
|
|
ganesh_IT wrote: fstream file("file path", ios_base::binary | ios_base::in);
Shouldn't the above line be
fstream file("file path", ios_base::binary | ios_base::out);
?
ganesh_IT wrote: char buf[] = "BinaryFile"
You're writing a bunch of ASCII characters to a binary file, i.e. you're creating, by definition, a ASCII text file.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
ganesh_IT wrote: using namespace std;int main(){ fstream file("file path", ios_base::binary | ios_base::in); char buf[] = "BinaryFile"; file.write(buf, sizeof(buf)); file.close();}
if file exist, it will over right it.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hi!
I've to create a Menu System. I wrote a class for Initial Menu page(which contains 4 buttons) and another class for the next page(e.g when clicking the options button in the initial Menu page). I've crated an object of the second class inside the corresponding button's click event at the first class. Similarly I've created an object of the first class inside the back button's click event of the second class. I've created the device(device is necessary to display the gui elements) at each class's constructors. So my second menu page is loaded in a separate window(device). What to do to load it in the same window?
|
|
|
|