App* value vs. App* entity

1. I get an error for the line in the try block. It is:

a value of type "App *" cannot be assigned to an entity of type "App *"

2. Secondarily, for the catch() line, I am getting:

'std::exception::what': non-standard syntax; use '&' to create a pointer to member

What am a missing here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

App *g_pApp;

int
main() {
	try {
		g_pApp = new App;
	}
	catch (exception const &e) {
		cout << "main(): new App() memory allocation failed: " << e.what;
		exit(1);
	}
	int iRetVal = g_pApp->Run();

	delete g_pApp;

	return iRetVal;
}


I am using this approach to make App variables and constructs available to the whole program. Please let me know if there is a better way.
'std::exception::what': non-standard syntax; use '&' to create a pointer to member

You forgot the the parentheses after the function name.

 
cout << "main(): new App() memory allocation failed: " << e.what();
Thanks, that fixes the second error. I hadn't realized it was a function. (I've never thought much of the OO concept of hiding data types and name my variables to indicate its type. e.g., "g_" for globals, "p" for pointers, etc.)

I still need help on the 1st error.
If the requirement is that App is a polymorphic singleton, consider doing something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <stdexcept>

struct App // may be in app.h
{
    int run() { return do_run() ; }

    static App& this_app()
    {
        if( ptr_instance == nullptr )
            throw std::logic_error( "no App object has been created!" ) ;

        return *ptr_instance ;
    }

    protected:

        App()
        {
            // do not allow a second instance
            if( ptr_instance != nullptr )
                throw std::logic_error( "a second instance of App is not alowed!" ) ;

            else ptr_instance = this ;
        }

        virtual int do_run() = 0 ;

    private:

        static App* ptr_instance ;

        App( const App& ) = delete ; // non-copyable
        App( App&& ) = delete ; // and non-moveable
};

App* App::ptr_instance = nullptr ; // may be in app.cpp

int main()
{
    // illustrative:may be implemented as a component (my_app.h/my_app.cpp)
    struct my_app : App
    {
        protected: virtual int do_run() override
        { std::cout << "running my_app\n" ; return 0 ; }
    };

    static my_app this_app ; // this object is available till end of program

    // illustrative: App::this_app() may be called from anywhere in the program
    return App::this_app().run() ;
}

http://coliru.stacked-crooked.com/a/fa31c20765c45851
Ah, thanks for your thought-out post. I would learn some new C++ idioms if I studied your code, but before I invest the non-trivial (for me) time, I want to make sure it is directed toward my need. Class App is not provided by any library. It is not polymorphic. I simply want to have a way to have application-wide access to application-wide properties/variables (hence the globally-scoped pointer g_pApp to the only instance of App there will be at runtime). It's an old-style way to handle it that I used to use. If there is a more modern/acceptable way, please do tell. I was proficient/advanced in C++, but a 15 year break does violence to one's memory.
Something simple like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

struct App
{
    int run() { std::cout << "running app\n" ; /* ... */ return 0 ; }

    static App& this_app() { return *ptr_instance ; } // ideally: add check for nullptr

    App() { ptr_instance = this ; /* ... */ } // ideally: prevent second instance

    private: static App* ptr_instance ;

    // ideally: make non-copyable, non-moveable
};

App* App::ptr_instance = nullptr ;

int main()
{
    static App my_app ; // this object is available till end of program

    // illustrative: App::this_app() may be called from anywhere in the program
    return App::this_app().run() ;
}
Topic archived. No new replies allowed.