Solved

starting a function from a menu

Posted on 1998-09-28
7
270 Views
Last Modified: 2010-04-01
I want to activate a function from a menu.
The function is a member of a different class from the one that is invoked when going to the menu.
In the message pump I write:
BEGIN_MESSAGE_MAP(CMyNewApp, CWinApp)
      ON_COMMAND(ID_HELP, CWinApp::OnHelp)
      ON_COMMAND(ID_INIT, MyApp::Init())
END_MESSAGE_MAP()
As I wrote, MyApp is a different class and I get the compiling error:
'MyApp::Init' : illegal call of non-static member function.
I do not want to make Init a static function because this causes problems later on.
What solves this?
Thanks!!
0
Comment
Question by:gilbert_chang
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1173891
1. Illegal call to non-static because the way you wrote it doesn't say which CWinApp object (I know you know there's only one of these, but the compiler doesn't)

2. (CMyNewApp *)AfxGetApp())->Init() ;

and ((CMyNewApp *)AfxGetApp())->OnHelp() ;

3. It would be _best_ to put in the functions in the right place (e.g. in the doc or view or frame) and then call using tip #2

e.g.
void CMyView::OnHelp( ..etc .. )
{
((CMyNewApp *)AfxGetApp())->OnHelp() ;
}

0
 

Author Comment

by:gilbert_chang
ID: 1173892
Note that Init is not a member of CMyNewApp:
ON_COMMAND(ID_INIT, MyApp::Init())
it is a member of MyApp which is a totaly independent class.
Thanks.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1173893
Sorry I missed understood

Ok then

MyApp::Init() still means a call to a static as it doesn't specify which object (it only specifies MyApp class, there could be more than one object of this class)

(A) EITHER

1. Put a pointer to a MyApp object in the CMyNewApp object

class
{
//etc
private:
  MyApp * m_pMyApp ;

 //etc
} ;

2. Initialize m_pMyApp with a pointer to the MyApp object - by
(a) If MyApp object exists, simply assign m_pMyApp to point to this object (e.g. in constructor)
or
(b) create MyApp object in your constructor, and delete it in your constructor
CMyNewApp::CMyNewApp( ... etc )
{
m_pMyApp = new MyApp ;
//etc
}

CMyNewApp::~CMyNewApp()
{
delete m_pMyApp ;
}

3. Add OnInit menu item to CMyNewApp class, in handler call :-

void CMyNewApp::OnInit()
{
  m_pMyApp->Init() ;
}


(B) OR

1. Modify MyApp to be a singleton class ( or derive class from it which is singleton if you don't/can't modify source for MyApp).  My favorite way to do this is :

in a .h

class MyAppObject : public MyApp
{
private:
  static MyAppObject m_This ;
 
  MyAppObject() { }

public:
  ~MyAppObject() { }
 
  MyAppObject& Get() { return *this ; }
}

In a .cpp

MyAppObject MyAppObject::m_This

2. Add OnInit handler to CMyAppObject

void CMyNewApp::OnInit()
{
  MyAppObject::Get().Init() ;
}

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:gilbert_chang
ID: 1173894
Thanks a lot!!
Option (A)->2.(b) did it!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173895
Note answer2000's option (A) is a more detailed version of my suggestion.  I was assuming that the MyApp object was global, so you wouldn't need a pointer to it (In my example I declared a global object of type MyApp).  Other than that, it was the same idea.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1173896
nietod - scrolling back I don't see any comments from you on this Q - except the one made on 28 Sept (beginning "Note...")

0
 
LVL 22

Expert Comment

by:nietod
ID: 1173897
This was the question that was asked twice.  My answer was on the other question.  Gilbert_chang didn't think my answer worked, but he must have made a mistake because it was fine and was the same as yours with the exception that I assumed the objects was a global variable.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now