Declare class member outside of constructor.

Basically, I need to set a variable outside of the constructor and make it accessible to the entire class.

It would need to work 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
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>

template <typename MT> class CallbackFunction
{
	void (*func)(MT);
	MT *data;
	public:
		void SetCallbackData (void (*f)(MT), MT *d)
		{
			func = f;
			data = d;
		}
		void Call()
		{
			func(data);
		}
};

class Callback
{
	public:
		template <typename T> void SetCallback(CallbackFunction <T> *func)
		{
			// Need to make this a class member ¬
			CallbackFunction <T> *CallbackClass = func;
		}
		void Call()
		{
			CallbackClass->Call();
		}
};

template <typename CT> Callback *NewCallback(void (*func)(CT), CT *data)
{
	Callback *cb;
	CallbackFunction <CT> *cf;
	cf->SetCallbackData(func, data);
	cb->SetCallback <CT> (cf);
	return cb;
};

void Call(Callback *CallbackFunc)
{
	CallbackFunc->Call();
}

void foo(std::string str)
{
	std::cout << str << "\n";
}

int main()
{
	std::string *str;
	str->append("Hello, World!");
	Call( NewCallback(foo, str) );
	return 0;
}


Thanks!
1
2
	std::string *str;
	str->append("Hello, World!");


Note that str points to some random place in memory and you're treating that memory as if it is a string. It's not.

Perhaps something like the following?

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
#include <functional>
#include <iostream>
#include <string>

struct functor_with_string_data
{
    std::string s ;

    functor_with_string_data( const std::string & str ) : s(str) {}

    void operator()() { std::cout << s << '\n' ; }
};

struct functor_with_int_data
{
    int data ;

    functor_with_int_data( int num ) : data(num) {}

    void operator()() {std::cout << data << '\n' ;}
};


class myclass
{
public:
    myclass( std::function<void()> f = nullptr ) : _func(f) {}
    void setCallback( std::function<void()> f ) { _func = f ; }

    void doCallback() const { _func(); }


private:
    std::function<void()> _func ;
};

int main()
{
    myclass obj ;

    obj.setCallback(functor_with_string_data("Hello, world!")) ;
    obj.doCallback() ;

    obj.setCallback(functor_with_int_data(42)) ;
    obj.doCallback() ;
}


http://ideone.com/MpuRRH
Last edited on
Hi, thanks for replying, I'm doing this simply for practise, I'd rather not use <functional>. And plus it needs to be able to support ANY time of data, not just int and strings.

All I need to do is simply make a local variable declared inside a class member function accessible to the entire class without declaring it in the class itself.

Thanks.
Hi, thanks for replying, I'm doing this simply for practise, I'd rather not use <functional>. And plus it needs to be able to support ANY time of data, not just int and strings.


Then don't use functional. But, seeing how it works may help you figure out how you want yours to work. It does support ANY kind of data. It supports any functor/function/lambda with the right prototype, and those may deal with any kind of data they desire.

For instance:
http://ideone.com/d2MU6l
The problem is, the class doesn't yet know the type of CallbackFunction <SomeType> so I cannot declare it in the class, I need to declare it in the template function, and then make it a class member from there.
You cannot define a class data member from a function.
Topic archived. No new replies allowed.