C++ : can i create an empty function for then i redefine it?

Pages: 123
imagine i have a list of prototype functions on a macro(like the event list). i must add them on class.
1
2
3
4
5
6
7
8
class Foo {
  std::vector<Bar> fubar;
};

int main(){
  Foo foo;
  // How many Bars are in foo now?
}

If a Bar is a function object, then how many functions does the foo "define" right now?

0
none

Every function that could possibly be in foo is defined somewhere, but the foo has none.


You are not the first to write an event handling system with C++. I bet they did not resort to 'weak'.


I could perhaps understand the "not defined" in an app that explicitly loads a shared object during runtime and maps to its symbols. In that case the app itself compiles and can run fine way before anyone even considers writing (excuse me, "defining") a plug-in function.
the big problem with a vector is that it can't be defined on Global Scope:
1
2
3
4
5
6
7
8
9
10
class Foo {
  std::vector<Bar> fubar;
}Foo;
Foo::fubar.push(); //the compiler will give me an error, because i can't change variables values on these section

int main(){
  Foo foo;
  // How many Bars are in foo now?
  return 0;
}


i need like these:

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
class base
{
    public:
    //or i will continue with a pointer between these 2 class's
    virtual void hello()=0;//i must use virtual pure or define it
    virtual void hello()=0;//i must use virtual pure or define it
};

class Foo:base 
{
  public:
  void hello();//now i must define it or i will get errors :(
  void hey(); 
}Foo;

void Foo::hello()
{
     std::cout << "hello world";
}

//using the same prototype functions list:
(i will create a general macro avoid avoid re-write all prototype functions, instead repeat only what i will need)
class Foo2:base 
{
  public:
  void hello();//now i must define it or i will get errors, if i call it on base class :(
  void hey(); 
}Foo;

void Foo2::hey()
{
     std::cout << "hello world hey";
}



int main()
{
   //Foo.hey(); //error.. here it's normal
    Foo::hello();
    Foo2::hey(); //here was defined
   //true the hello() isn't defined, but i don't need call it
  return 0;
}

heres another sample that i'm using it:
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
class test
{

public:
    virtual void MouseClick(){;};
    virtual void Move(){;};
	test()
	{
			MouseClick();
			Move();
	}

	void call()
	{
	    MouseClick();
        Move();
	}
};

class InstanceName : public test
{
    void MouseClick();
    void Move();
} InstanceName;

//if i comment these function i will get a compiler error
//i need be free and comment, if i don't need it
void InstanceName::MouseClick()
{
    std::cout << "Mouse click\n";
}

void InstanceName::Move()
{
   std::cout << "Move\n";
}

int main()
{
    InstanceName.call();
    std::cin.get(); //press enter before exit
    return 0;
}

theres no error... until here fine.
but imagine if i comment the MouseClick(), i will get a compiler error, because was not defined... here it's my problem :(
that's why i used the 'weak' for avoid these error.
(now i'm not using the pointer between the class's for simplificate it and show you my problem)
only the static std::function can be changed on Global Scope. but if i use the static i must give it a value. that's why i don't use it.
No, you don't have to set the std::function to anything.

1
2
3
4
5
6
7
8
9
10
11
12
class A{
public:
    static std::function<void(A &)> MouseClick;
};

std::function<void(A &)> A::MouseClick;

int main(){
    A a;
    if (A::MouseClick)
        A::MouseClick(a); // This will never run.
}
helios: if you comment the line 6, the compiler will give you an error... is what i mean
Why can't you define the static?

This back-and-forth is frustrating. Why can't you just tell us all the requirements and restrictions of your problem? As it is we're just talking shots in the dark and you keep coming back with "no, that doesn't work" without ever explaining why it doesn't work.
Last edited on
i'm so sorry.
but from several time i'm trying explain and seems failling... so i'm so sorry.
on a class the number of prototypes functions must be a number of definition functions. it's a C\C++ rule.
it's the way, that i'm trying:
- define a function on Global Scope;
- define only the function that i need.
maybe i need redefine my macro for not repeat, always the functions prototypes and only what i need define them.
i'm sorry, if i'm not good to explain. but is like asking these: if the virtual function is for be polymorphims from a child class, why we must use a prototype for define it outside de class!?!
- define a function on Global Scope;
- define only the function that i need.
A static std::function is compatible with these requirements.
So, I'll ask again: what's the problem with defining the static separately?

i'm sorry, if i'm not good to explain. but is like asking these: if the virtual function is for be polymorphims from a child class, why we must use a prototype for define it outside de class!?!
You don't have to.
1
2
3
4
5
6
7
8
9
class A{
public:
    virtual void foo(){} //<- definition inside the class definition
};

class A{
public:
    virtual void foo() = 0; //<- abstract function, so non-abstract derived classes must override
};
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
class test
{

public:
    virtual void MouseClick(){;};
    virtual void Move(){;};
	test()
	{
        MouseClick();
        Move();
	}

	void call()
	{
	    MouseClick();
        Move();
	}
};

class InstanceName : public test
{
    public:
    virtual void MouseClick()=0;
    virtual void Move()=0;
}InstanceName;

1st error: "cannot declare variable 'InstanceName' to be of abstract type 'InstanceName'"
why these error? i can't use a class without instances?
You can't instantiate a class that declares abstract functions.
You need to grab a book on OOP. This is basic stuff.

If you're not going to answer my questions I'm just going to stop replying. This is a waste of both our times.
"A static std::function is compatible with these requirements.
So, I'll ask again: what's the problem with defining the static separately?"
the problem is that i must define it... i must... it's a C++ rule. i'm not free to choose, i must define it.
you will 'kill me', but i will be honest ;)
you have right these is C++ and not Visual Basic. the events, on Visual Basic, was defined outside the class. it can be only my head, but i loved the way how the events functions was defined outside the class
Last edited on
You're not answering the question. The problem with defining the static is that you must define it? What does that mean??
You say you love that event handlers can be defined outside the class. Well, a static std::function lets you do that and more, you nincompoop!

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
class A{
public:
    static std::function<void(A &)> MouseClick;
};

std::function<void(A &)> A::MouseClick;

void handler1(A &){
    std::cout << "from handler1!\n";
}

void handler1(A &){
    std::cout << "from handler2!\n";
}
int main(){
    if (rand() % 2)
        A::MouseClick = [](A&){ std::cout << "from the lambda!\n"; };
    if (rand() % 3)
        A::MouseClick = handler1;
    if (rand() % 5)
        A::MouseClick = handler2;
    A a;
    if (A::MouseClick)
        A::MouseClick(a);
}
Last edited on
the problem is: i must give it a value... it's an order rule. i can't have an empty function using std::function, on a static way
When you say
it's an order rule. i can't have an empty function using std::function, on a static way
do you mean that it's a rule from a code guideline? If that's the case, then you wouldn't have been able to use gnu::weak either. gnu::weak works equivalently to a static function pointer.
Last edited on
the problem is: i must give it a value

And that value is effectively nullptr. If you add null to the things that you "cannot use", what is there left?
helios:
1
2
3
4
5
6
7
8
class InstanceName : public test
{
    public:
    void MouseClick() final=0;
    void Move() final=0;
    static std::function<void(InstanceName &)> MouseClick2; //like you see not a value
}InstanceName; //here i use the class name for not use instances
why these way? because it's the way that i know for define the functions on Global Scope 

error: "cannot declare variable 'InstanceName' to be of abstract type 'InstanceName'"
Sorry, I have no idea what you're trying to tell me. The code you posted is completely meaningless.

Lines 4 and 5: These functions are not virtual. The = 0 syntax is only for virtual functions.
Line 7: The class InstanceName contains some abstract functions (MouseClick and Move, perhaps more in test), so you can't instantiate it. Either way you can't name an instance with the same name as its type. That's nonsense. Imagine creating an int named 'int'.
helios i must use the class for don't use instances for these class.
Last edited on
Do you mean "I must use the class [which one?] without using instances of the class [again, which one?]"?

So, earlier when I asked what you were trying to do and you said
- define a function on Global Scope;
- define only the function that i need.
you were lying, because now you've added another restriction to the problem.

This is the last time I'm going ask this: What are you trying to do? What problem are you trying to solve? What restrictions do you have to get around to solve it?
Last edited on
objectives:
1 - avoiding the function prototype, but isn't impossible;
2 - the function is defined outside the class(Global Scope);
3 - the class will not have instances;
4 - if possible, is using the virtual functions.

what we can't do, because of C++ rules:
1 - the virtual function must be declated(prototype) on chil class;
2 - if i don't define the function, i will get compiler errors;
3 - if i change the class, with abstract, functions for not have instances, i will get errors :(
so what you can tell me?

i'm not lying... i'm trying test your(both) ideais for what i need
Last edited on
C++ rules are not restrictions. Forget the language details for now. Your objective(s) should be what you need to achieve, to do, to solve. They should not be about C++, unless you are writing the next version of the standard.

An example of objective is: "Sort personnel data into descending order by IQ."
Pages: 123